文章目录
- 一、DDL
- 二、DML
- 三、约束
-
- 3.1 主键
-
- (1)primary key
-
- ① 语法
- (2)自增
-
- ① 语法
- ② 触发条件
- ③ delete 和 truncate
- 3.2 其他约束
-
- (1)非空
- (2)唯一
- (3)默认
一、DDL
- 库
- 表create table 表名(
id int,
name varchar(32)
) - 列
- alter table 表名
- add 列名 类型 约束 (添加列)
- change 旧列名 新列名 类型 约束 (修改列)
- drop 列名 删除列
- alter table 表名 change 旧列名 新列名 类型 约束;
use test;# 选取使用的库,如果本身就是这个库,可以省略
# 添加字段
# 关键字,不能作为表字段名或表名,否则后续会引发bug!# 如果强行使用关键字,可以通过反引号来解决:`列名`
alter table categories add `desc` varchar(32) not null; # 虽然但是,强烈建议禁止使用关键字作为名称# 修改列 alter + change
alter table categories change desc1 describe2 varchar(100);# 删除列 alter + drop
alter table categories drop describe2; - alter table 表名
二、DML
- 操作
- modify
- 行操作
- 增insert into 表名(列名1, 列名2, ......)
values (1,'张三',......),(2,'李四',......),......
;
# 这里不写s,写value也行 - 删delete from 表名 where id =1;
- 改update 表名 set 列名1 = 值, 列名2 = 值,......
where id =1
;
- 增insert into 表名(列名1, 列名2, ......)
三、约束
3.1 主键
(1)primary key
- 非空
- 唯一(不能重复)
① 语法
- 1、建表时,指定主键# (1)
create table test01(
id int primary key,
name varchar(32),
age int
);
# (2)
create table test02(
id int,
name varchar(32),
age int,
primary key (id)
); - 2、建表后,指定主键create table test03(
id int,
name varchar(32),
age int
);
alter table test03 add primary key (id); - 3、删除主键alter table test02 drop primary key;
(2)自增
- 主键 或 唯一(unique)列 自动累加1
- 只能作用在主键、唯一
- Mysql一个表只能有一个自增(其他Oracle不是)
① 语法
- 1、建表时,指定自增
drop table test01;
# 注意:自增只能在 主键 或 unique 列 上生效
create table test01(
id int primary key auto_increment,
name varchar(32)
);
- 2、建表后,指定自增
alter table test01 change id id int auto_increment;
# 第二个id可以是任意别名
② 触发条件
- 0
- null
- insert 不指定此列
# 1、0
insert into test01 value (0,'张三');
# 2、null
insert into test01 value (null,'李四');
# 3、insert 不指定此列
insert into test01 (name) value ('王五');
- 不触发
insert into test01 value (188,'赵六');
insert into test01 value (10,'赵六');
③ delete 和 truncate
- 1、delete 清空表,按行删,自增不会清零;(DML) delete-DML 会受到事务的控制
- 2、truncate 清空表,删表重建表,自增会清零;(DDL) truncate-DDL 不会
# 先清空,然后插入,看自增id值
delete from test01;
# 假如删之前已经有了4组数据
insert into test01 value (0,'张三');
# 结果:5,'张三'
truncate table test01;
# 假如删之前已经有了4组数据
insert into test01 value (0,'张三');
# 结果:1,'张三'
3.2 其他约束
-
非空:不能 insert null 值
-
唯一:unique,此列的值,不能重复
-
默认值(缺省值):default,不传值时使用的默认值
有两种方式: 建表时,跟在 数据类型后面; 建表后,alter table xxx change 旧列名 新列名 类型 约束;
(1)非空
- not null
- 代码
- 建表时create table test03(
id int primary key,
stu_no varchar(32) unique not null,
name varchar(32) not null,
gander varchar(32) default '未知'
); - 建表后# 添加 唯一列
alter table test03 add phone_num varchar(11) unique;
alter table test03 change id id int auto_increment;
- 建表时create table test03(
(2)唯一
- unique
- 注意! null 值,代表真空,无法比较,unique 无法限制 null 值。
- 测试# 唯一:unique,此列的值,不能重复
insert into test03 value (1,'s001','张三','man','12312312312');insert into test03 value (2,'s001','李四','woman','45645645645');
# Duplicate entry 's001' for key 'stu_no'
# 主键'stu_no'出现重复条目's001'
# null 值,代表真空,无法比较,unique 无法限制 null 值。
insert into test03 value (0,'s006','路人甲','woman',null);
insert into test03 value (0,'s007','路人乙','man',null);

(3)默认
- default
- 默认值(缺省值):default,不传值时使用默认值
- 自增:1、0;2、null;3、不传此列
- 默认值触发:1、不传此列!
- 测试insert into test03 value (2,'s003','王五','man',null);
insert into test03 value (8,'s002','李四','woman',0);
insert into test03 (id,stu_no,name) value (5,'s005','赵六');

-
外键 foreign key




