数据库的操作
链接数据库
mysql -u root -proot -- 不推荐 密码直接展示
mysql -uroot -p
退出数据库
exit/quit
sql语句最后需要有分号;结尾
显示数据库版本
select version();
显示时间
select now();
查看所有数据库
show databases;
DDL(数据定义语句)
创建数据库
create database database_name;
create database database_name charset=utf8;
查看创建数据库的语句
show create database database_name;--`` 有特殊字符 ''
查看当前使用的数据库
select database();
使用数据库
use database_name;
删除数据库
drop database database_name;
数据表的操作
查看当前数据库中所有表
show tables;
创建表
auto_increment表示自动增长
not null 表示不能为空
primary key 表示主键
default 默认值
create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);
create table table1(
id int primary key not null auto_increment,
name varchar(30) not null
);
查看表结构
desc table1;--desc 表名;
创建students表(id、name、age、high、gender、cls_id)
create table students(
id int primary key not null auto_increment,
name varchar(30),
age tinyint unsigned default 18,
high decimal(5,2) unsigned,
gender enum('男','女','保密') default '保密',
cls_id int
);
创建classes表(id、name)
create table classes(
id int primary key not null auto_increment,
name varchar(30)
);
查看表的创建语句
show create table students;--show create table 表名;
修改表-添加字段
alter table students add birthdat date;--alter table 表名 add 列名 类型;
修改表-修改字段:不重命名版
alter table students modify birthday date default'1990-1-1';--alter table 表名 modify 列名 类型 约束;
修改表-修改字段:重命名版
alter table students change birthday birth data default '1990-1-1';--alter table 表名 change 原名 新名 类型 约束;
修改表-删除字段
alter table students drop high;--alter table 表名 drop 列名;
删除表
drop table 表名;
drop database 数据库名;
增删改查(curd)
增加:
全列插入
insert into classes values(1,'一班');--insert [into] 表名 values(...);
向students表插入 一个学生信息
+--------+-------------------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------------------------+------+-----+------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
| gender | enum('男','女','保密') | YES | | 保密 | |
| cls_id | int(10) unsigned | YES | | NULL | |
| birth | date | YES | | 1997-01-01 | |
+--------+-------------------------------+------+-----+------------+----------------+
主键字段可以使用 0、null以及default 来占位
insert into table values(0,'dog',18,'男',1,'1990-1-1');
insert into table values(null,'dog',18,'男',1,'1990-1-1');
insert into table values(default,'dog',18,'男',1,'1990-1-1');
枚举类型插入 下标是从1开始的
insert into students values(0,'cat',18,1,1,'1990-1-1');--即代表枚举类型选择男
部分插入
-- insert into 表名(列1,...) values(值1,...) -- 非空字段
insert into students(`gender`) values(2);
insert into students(`name`,`gender`) values('pig',2)
多行插入
insert into students values
(default,'sheep',19,1,'1990-1-1')
(default,'tiger',20,2,'1991-1-1')
;
修改
--update 表名 set 列1=值1,列2=值2... where 条件;
update students set name='Dog' where name='dog'; -- 不加where 全部修改
-- update 表名 set 列1=值1,列2=值2... where 条件;
删除:
物理删除
delete from students where id = 1; -- delete from 表名 where 条件
delete from students;--全删除;
这样删除的话删除的直接是数据,是不可恢复的!!
逻辑删除
设置一个名为is_delete的列,里面的值为0,1,0代表未删除,1代表删除。
update students set is_delect=1 where id = 1;
查询基本使用
select 去重选项 字段列表[as 字段名] from 数据表 where [group by 子句] [having子句] [order by子句] [limit子句];
查询所有列
select * from students;
去除重复字段的查询 distinct 整个查询的行是否重复
select distinct name from students;
查询指定列
select name,age from students; -- select 列1,列2,... from 表名;
可以使用as为列或表指定别名
select name as 名字,gender from students;