约束:
1) not null (非空), 只能用于列级约束
2) unique(唯一)
3) primary key(主键--非空且唯一) 表中只允许一个主键
4) foreign key(外键--引用别的表的数据)
5) check, 只能用于列级约束
6) default 缺省值
语法描述:
对于列级约束,直接写在列定义的后边就行。
列定义 not null (非空约束)
表级约束,在列定义完后,使用关键字
约束关键字(列名)
当主键约束为两个列时只有两个列连接值一样的时候才会违反约束。
小例子:
create table test(
id int auto_increment,
name varchar(50) not null,
idcard varchar(20),
address varchar(50),
unique(idcard),
primary key(id, name)
);
当违反约束的时候插入不成功,会报错。
测试插入情况:
insert into test(
id,
name,
idcard,
address
) values (
1,
'aaa',
'12213123',
'beijing'
), (
1,
'bbbb',
'232423',
'shanghai'
);
下面的记录无法插入,违反主键约束
insert into test(
id,
name,
idcard,
address
) values (
1,
'aaa',
'12213123',
'beijing'
);
违反的是非空约束
insert into test(
id,
name,
idcard,
address
) values (
2,
null,
'12213123',
'beijing'
);
违反唯一约束
insert into test(
id,
name,
idcard,
address
) values (
2,
'cccc',
'12213123',
'beijing'
);
在非定义的时候给某表添加,丢弃表级的唯一约束
语法描述:
添加
alter table 表名
add unique(列名);
丢弃
alter table 表名
drop index `列名`;
或者
drop key `列名`;
使用modify不能丢弃表级约束
小例子:
为teachers表的name列添加表级的唯一约束
alter table
teachers
add unique(name);
丢弃表级唯一约束
alter table
teachers
drop index `name`;
和
alter table
teachers
drop key `name`;
以下语句无法丢弃表级约束
alter table
teachers
modify
name varchar(25);
在非定义的时候给某列添加列级的非空约束
语法描述:
添加
alter table 表名
change
列定义 设置约束;(not null)
丢弃
alter table 表名
modify
列定义 设置约束;(null)
语法描述:
添加
alter table 表名
change
列定义 设置约束;(not null)
丢弃
alter table 表名
modify
列定义 设置约束;(null)
小例子:
为phone列添加列级的非空约束
alter table
teachers
change
phone phone varchar(15) not null;
丢弃列级约束
alter table
teachers
modify
phone varchar(15) null;
外键约束
保证子表中的记录中的某列要和母表中的某列一致。
+----------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
| gender | enum('男','女') | YES | | 男 | |
| age | int(11) | YES | | NULL | |
| phone | varchar(15) | YES | | NULL | |
| birthday | datetime | YES | | NULL | |
| home | varchar(200) | YES | | NULL | |
+----------+-----------------+------+-----+---------+----------------+
语法描述:
[constraint 外键名] foreign key(子表的引用列) references 母表(母表的主键);
小例子:
教师表,班主任要引用老师表的主键。
create table classes (
id int auto_increment,
name varchar(20) unique,
master int,
classroom int not null,
begindate datetime,
primary key(id),
foreign key(master) references teachers(id)
);
+----+--------+--------+------+--------------+---------------------+--------------+
| id | name | gender | age | phone | birthday | home |
+----+--------+--------+------+--------------+---------------------+--------------+
| 1 | 张老师 | 男 | 40 | 135234234234 | 1976-05-02 10:01:01 | 北京通州32号 |
| 2 | 李老师 | 女 | 40 | 135234234234 | 1976-05-02 10:01:01 | 北京昌平32号 |
| 3 | 刘老师 | 男 | 30 | 135234234234 | 1986-05-02 10:01:01 | 北京朝阳32号 |
| 4 | 闫老师 | 女 | 25 | 124234234 | 1990-01-02 00:00:00 | 北京西城 |
+----+--------+--------+------+--------------+---------------------+--------------+
插入4值时,需要先到母表中进行检索,若可以找到数据,插入成功
insert into classes
(
name,
master,
classroom,
begindate
) values (
'H50826',
4,
232,
now()
);
插入400值时, 需要先到母表中进行检索, 如果没有找到数据,则插入失败
insert into classes
(
name,
master,
classroom,
begindate
) values (
'JavaEE0826',
400,
333,
now()
);
插入成功
insert into classes
(
name,
master,
classroom,
begindate
) values (
'Android0826',
4,
333,
now()
);
插入成功
insert into classes
(
name,
master,
classroom,
begindate
) values (
'JavaEE1117',
1,
101,
'2016-7-17'
);
在非定义的时候添加,丢弃外键约束语法
alter table 表名
add [constraint stu_fk] foreign key(classid) references class(id);
alter table 表名
drop foreign key 外键名;
测试外键约束
当母表中的记录被引用时, 不能删除这条记录
先把子表中的相应记录删除,才能再删除这条记录
insert into
teachers
(
id,
name,
gender,
age,
phone,
birthday,
home
) values (
4,
'奈老师',
'女',
40,
'135234234234',
'1976-5-2 10:1:1',
'北京京州32号'
);
把子表的外键约束丢弃
alter table classes
drop foreign key classes_ibfk_1;
设置级联删除
语法描述:
设置外键,在删除母表记录时,子表记录会被级联,也被删除
alter table 表名
add [constraint 键名] foreign key(子列引用的列) references 母表(母表主键) on delete cascade on update cascade;
设置外键,在删除母表记录时,子表记录中的引用列被置为null
alter table 表名
add [constraint 键名] foreign key(子列引用的列) references 母表(母表主键) on delete set null on update set null;
小例子:
级联删除
alter table classes
add constraint my_fk foreign key(master) references teachers(id) on delete cascade on update cascade;
级联置null
alter table classes
add constraint my_fk foreign key(master) references teachers(id) on delete set null on update set null;
limit关键字,分页
limit 数值1,数值2 数值1表示略过的记录,数值2表示显示的记录数。
分页 : 略过当前页以前的所有记录, 显示一页该显示的记录数
select * from 表名 where limit (pageNo - 1) * pageSize, pageSize;