1.完整性约束
1.1 主键(primary key)
主键约束相当于not null 和 唯一性约束。
创建表时添加主键约束的多种方式
1. create table tmp( num int primary key, name varchar(20), sex char(2));
2. create table tmp( num int, name varchar(20), sex char(2), primary key (name));
3. create table tmp ( num int, name varchar(20), sex char(2), constraint PK_NAME primary key(name));
修改表添加主键约束
alter table tmp add constraint PK_SEX primary key(sex);
SHOW CREATE TABLE tmp;
//查看创建tmp表的语句,可以看到已经有了PRIMARY KEY (`num`)
CREATE TABLE `tmp` (
`num` int(11) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`sex` char(2) DEFAULT NULL,
PRIMARY KEY (`num`)
测试主键约束效果:
表的结构:
mysql> desc tmp;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| num | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | char(2) | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+
插入数据:
mysql> insert into tmp values(9527, 'mike', 'm');
Query OK, 1 row affected (0.04 sec)
mysql> insert into tmp values(9528, 'lilei', 'm');
ERROR 1062 (23000): Duplicate entry 'm' for key 'PRIMARY'
mysql> insert into tmp values(9528, 'lilei', NULL);
ERROR 1048 (23000): Column 'sex' cannot be null
//因为sex字段有主键约束,所以不能输入相同的值并且不能为NULL
主键约束和自动增长类型
test表的id字段为自动增长,age字段为默认字段20
create table test(id int auto_increment primary key, name varchar(2), age int default 20);
默认字段:当插入数据没有指定默认字段,则设置为20,否则设置为指定的数据
自动增长类型:
测试:
insert into test values(NULL, 'li', 10);//age将被设置为10,id增长为1
insert into test(name) values('zh');//age为默认值20,id增长为2
insert into test values(5, 'mi', NULL);//age为NULL,id增长为5
insert into test values(NULL, 'xu', NULL);//age为NULL,id接着上衣条记录增长为6
1.2 外键(foreign key)
1. 创建student表添加主键约束
create table student (num int, name varchar(20), sex char(2));
alter table student add PK_NUM primary key(num);
2. 创建num_t表添加主键约束
careate table num_t(num int, high int);
alter table num_t add constraint PK_NUM primary key(num);
3. 给student表创建外键约束,使student的num字段引用num_t的num字段
alter table student add constraint FK_NUM foreign key(num) references num_t(num);
4. 查看SHOW CREATE TABLE student,可以查看到外键约束
CREATE TABLE `student` (
`num` int(11) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`sex` char(2) DEFAULT NULL,
PRIMARY KEY (`num`),
CONSTRAINT `FK_NUM` FOREIGN KEY (`num`) REFERENCES `num_t` (`num`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
student表可以称为主表,num_t可以称为从表,student的num字段类型必须与引用从表字段类型一致,名称可以不相同。
测试外键约束:
1. 先为num_t插入数据:
insert into num_t values(1000, 2000);
insert into num_t values(1001, 2001);
2. 为student插入数据:
insert into student values(1000, 'mike', 'm');//插入num字段的数据来自num_t的num字段
insert into student values(1001, 'lilei', 'm');//因为主键约束插入失败
ERROR 1062 (23000): Duplicate entry '1001' for key 'PRIMARY'
insert into student values(1002, 'zhangfei', 'm');//插入的num字段1002在num_t中没有,因为外键约束插入失败
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`menwen`.`student`, CONSTRAINT `FK_NUM` FOREIGN KEY (`num`) REFERENCES `num_t` (`num`))
1.3 用户自定义完整性约束(check)
设置test表的age字段必须大于等于1,小于等于18
alter table test add constraint CC_AGE check (age >= 1 and age <= 18);
不过MySQL数据库并不支持,插入一个age=30的数据
insert into test (age) values(30);
select显示仍然插入成功:
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | NULL | 30 |
+----+------+------+