完整性约束:
http://www.cnblogs.com/linhaifeng/articles/7238814.html
约束条件与数据类型的宽度一样,都是可选参数
作用:用于保证数据的完整性和一致性
主要分为:
PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录
FOREIGN KEY (FK) 标识该字段为该表的外键
NOT NULL 标识该字段不能为空
UNIQUE KEY (UK) 标识该字段的值是唯一的
AUTO_INCREMENT 标识该字段的值自动增长(整数类型,而且为主键)
DEFAULT 为该字段设置默认值
UNSIGNED 无符号
ZEROFILL 使用0填充
说明:
1. 是否允许为空,默认NULL,可设置NOT NULL,字段不允许为空,必须赋值
2. 字段是否有默认值,缺省的默认值是NULL,如果插入记录时不给字段赋值,此字段使用默认值
sex enum('male','female') not null default 'male'
age int unsigned NOT NULL default 20 必须为正值(无符号) 不允许为空 默认是20
3. 是否是key
主键 primary key
外键 foreign key
索引 (index,unique...)
重点:
1.not null与default
2.unique
3.primary key
4.auto_increment
5.foreign key
一、not null与default
not null - 不可空
null - 可空
默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
create table tb1(
nid int not null defalut 2,
num int not null
)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ==================not null====================
2 mysql> create table t1(id int); #id字段默认可以插入空
3 mysql> desc t1;
4 +-------+---------+------+-----+---------+-------+
5 | Field | Type | Null | Key | Default | Extra |
6 +-------+---------+------+-----+---------+-------+
7 | id | int(11) | YES | | NULL | |
8 +-------+---------+------+-----+---------+-------+
9 mysql> insert into t1 values(); #可以插入空
10
11 mysql> create table t2(id int not null); #设置字段id不为空
12 mysql> desc t2;
13 +-------+---------+------+-----+---------+-------+
14 | Field | Type | Null | Key | Default | Extra |
15 +-------+---------+------+-----+---------+-------+
16 | id | int(11) | NO | | NULL | |
17 +-------+---------+------+-----+---------+-------+
18 mysql> insert into t2 values(); #不能插入空
19 ERROR 1364 (HY000): Field 'id' doesn't have a default value
20
21 ==================default====================
22 #设置id字段有默认值后,则无论id字段是null还是not null,都可以插入空,插入空默认填入default指定的默认值
23 mysql> create table t3(id int default 1);
24 mysql> alter table t3 modify id int not null default 1;
25
26 ==================综合练习====================
27 mysql> create table student(
28 -> name varchar(20) not null,
29 -> age int(3) unsigned not null default 18,
30 -> sex enum('male','female') default 'male',
31 -> hobby set('play','study','read','music') default 'play,music'
32 -> );
33 mysql> desc student;
34 +-------+------------------------------------+------+-----+------------+-------+
35 | Field | Type | Null | Key | Default | Extra |
36 +-------+------------------------------------+------+-----+------------+-------+
37 | name | varchar(20) | NO | | NULL | |
38 | age | int(3) unsigned | NO | | 18 | |
39 | sex | enum('male','female') | YES | | male | |
40 | hobby | set('play','study','read','music') | YES | | play,music | |
41 +-------+------------------------------------+------+-----+------------+-------+
42 mysql> insert into student(name) values('egon');
43 mysql> select * from student;
44 +------+-----+------+------------+
45 | name | age | sex | hobby |
46 +------+-----+------+------------+
47 | egon | 18 | male | play,music |
48 +------+-----+------+------------+
49
50 not null与default
not null and default
二、unique
============设置唯一约束 UNIQUE===============
方法一:
create table department1(
id int,
name varchar(20) unique,
comment varchar(100)
);
方法二:
create table department2(
id int,
name varchar(20),
comment varchar(100),
constraint uk_name unique(name)
);
方法三:
create table department(
id int,
name char(10),
unique(id),
unique(name)
);
方法四:
create table service(
id int primary key auto_increment,
name varchar(20),
host varchar(15) not null,
port int not null,
unique(host,port) #联合唯一 几个字段和在一起不重复 ip,port和在一起唯一
);
not null + unique 的化学反应 = primary key
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 mysql> insert into department1 values(1,'IT','技术');
2 Query OK, 1 row affected (0.00 sec)
3 mysql> insert into department1 values(1,'IT','技术');
4 ERROR 1062 (23000): Duplicate entry 'IT' for key 'name'
5 mysql> create table t1(id int not null unique);
6 Query OK, 0 rows affected (0.02 sec)
7
8 mysql> desc t1;
9 +-------+---------+------+-----+---------+-------+
10 | Field | Type | Null | Key | Default | Extra |
11 +-------+---------+------+-----+---------+-------+
12 | id | int(11) | NO | PRI | NULL | |
13 +-------+---------+------+-----+---------+-------+
14 row in set (0.00 sec)
15
16 not null+unique的化学反应
17 create table service(
18 id int primary key auto_increment,
19 name varchar(20),
20 host varchar(15) not null,
21 port int not null,
22 unique(host,port) #联合唯一
23 );
24
25 mysql> insert into service values
26 -> (1,'nginx','192.168.0.10',80),
27 -> (2,'haproxy','192.168.0.20',80),
28 -> (3,'mysql','192.168.0.30',3306)
29 -> ;
30 Query OK, 3 rows affected (0.01 sec)
31 Records: 3 Duplicates: 0 Warnings: 0
32
33 mysql> insert into service(name,host,port) values('nginx','192.168.0.10',80);
34 ERROR 1062 (23000): Duplicate entry '192.168.0.10-80' for key 'host'
35 联合唯一
36
37 unique
unique
三、primary key
primary key字段的值不为空且唯一
一个表中可以:
单列做主键:不为空且唯一;
多列做主键(复合主键):多个字段连在一起,当作主键;
存储引擎: 默认 innodb,对于innodb存储引擎来说,一张表内容必须有一个主键;
主键: 功能,不为空且唯一,还有其他方面得功能,组织表得数据,提高查询方式;
主键:
如果不指定主键,innodb会找字段不为空且唯一得字段把它设为主键,如果没找到,就会有隐藏得字段为主键,
用主键来组织数据,拿到主键加速查找。建主键,可以加速查找,innodb 应该有个自己建得主键。
这是innodb 独特得特性,和innodb 组织数据得方式有关
注: 通常一张表,都应该有一个id字段用来标识记录编号,id通常设置为主键。
一个表内只能有一个主键primary key
1.单列做主键:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 方法一:not null + unique
2 create table department1(
3 id int not null unique, #主键
4 name varchar(20) not null unique,
5 comment varchar(100)
6 );
7 方法二:在某一个字段后用primary key
8 create table department2(
9 id int primary key, #主键
10 name varchar(20),
11 comment varchar(100)
12 );
13 方法三:在所有字段后单独定义primary key
14 create table department3(
15 id int,
16 name varchar(20),
17 comment varchar(100),
18 constraint pk_name primary key(id) #创建主键并为其命名pk_name
19 );
20
21
22 单列做主键
单列做主键
2.多列做主键:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 create table service(
2 ip varchar(15),
3 port char(5),
4 service_name varchar(10) not null,
5 primary key(ip,port)
6 );
7 -------
8 mysql> desc service;
9 +--------------+-------------+------+-----+---------+-------+
10 | Field | Type | Null | Key | Default | Extra |
11 +--------------+-------------+------+-----+---------+-------+
12 | ip | varchar(15) | NO | PRI | NULL | |
13 | port | char(5) | NO | PRI | NULL | |
14 | service_name | varchar(10) | NO | | NULL | |
15 +--------------+-------------+------+-----+---------+-------+
16 rows in set (0.00 sec)
17
18 mysql> insert into service values
19 -> ('172.16.45.10','3306','mysqld'),
20 -> ('172.16.45.11','3306','mariadb')
21 -> ;
22 Query OK, 2 rows affected (0.00 sec)
23 Records: 2 Duplicates: 0 Warnings: 0
24
25 mysql> insert into service values ('172.16.45.10','3306','nginx');
26 ERROR 1062 (23000): Duplicate entry '172.16.45.10-3306' for key 'PRIMARY'
27
28
29 多列做主键
多列做主键
四、auto_increment
约束字段为自动增长,被约束的字段必须同时被key约束
1.不指定id,则自动增长
create table student(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') default 'male'
);
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 mysql> desc student;
2 +-------+-----------------------+------+-----+---------+----------------+
3 | Field | Type | Null | Key | Default | Extra |
4 +-------+-----------------------+------+-----+---------+----------------+
5 | id | int(11) | NO | PRI | NULL | auto_increment |
6 | name | varchar(20) | YES | | NULL | |
7 | sex | enum('male','female') | YES | | male | |
8 +-------+-----------------------+------+-----+---------+----------------+
9 mysql> insert into student(name) values
10 -> ('egon'),
11 -> ('alex')
12 -> ;
13
14 mysql> select * from student;
15 +----+------+------+
16 | id | name | sex |
17 +----+------+------+
18 | 1 | egon | male |
19 | 2 | alex | male |
20 +----+------+------+
21
22 不指定id,则自动增长
不指定id,则自动增长
2.可以指定id
mysql> insert into student values(4,'asb','female');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values(7,'wsb','female');
Query OK, 1 row affected (0.00 sec)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 mysql> select * from student;
2 +----+------+--------+
3 | id | name | sex |
4 +----+------+--------+
5 | 1 | egon | male |
6 | 2 | alex | male |
7 | 4 | asb | female |
8 | 7 | wsb | female |
9 +----+------+--------+
10
11 可以指定id
可以指定id
3.对于自增的字段,在用delete删除后,再插入值,该字段仍按照删除前的位置继续增长。
mysql> delete from student;
Query OK, 4 rows affected (0.00 sec)
mysql> select * from student;
Empty set (0.00 sec)
mysql> insert into student(name) values('ysb');
mysql> select * from student;
+----+------+------+
| id | name | sex |
+----+------+------+
| 8 | ysb | male |
+----+------+------+
4.应该用truncate清空表,比起delete一条一条地删除记录,truncate是直接清空表,在删除大表时用它,删除速度快。
mysql> truncate student;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into student(name) values('egon');
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | egon | male |
+----+------+------+
row in set (0.00 sec)
5.在创建完表后,修改自增字段的起始值
1.alter table student auto_increment=3;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 mysql> create table student(
2 -> id int primary key auto_increment,
3 -> name varchar(20),
4 -> sex enum('male','female') default 'male'
5 );
6
7 mysql> alter table student auto_increment=3;
8
9 mysql> show create table student;
10 .......
11 ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
12
13 mysql> insert into student(name) values('egon');
14 Query OK, 1 row affected (0.01 sec)
15
16 mysql> select * from student;
17 +----+------+------+
18 | id | name | sex |
19 +----+------+------+
20 | 3 | egon | male |
21 +----+------+------+
22 row in set (0.00 sec)
23
24 mysql> show create table student;
25 .......
26 ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
27
28 修改自增字段的起始值
修改自增字段的起始值
2.也可以创建表时指定auto_increment的初始值,注意初始值的设置为表选项,应该放到括号外
create table student(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') default 'male'
)auto_increment=3;
3.设置步长:
步长increment与起始偏移量offset:auto_increment_increment,auto_increment_offset
mysql自增的步长:
show session variables like 'auto_inc%';
#基于会话级别
set session auto_increment_increment=2 #修改会话级别的步长
#基于全局级别的
set global auto_increment_increment=5 #修改全局级别的步长(所有会话都生效)
set global auto_increment_offset=2
#!!!注意了注意了注意了!!!
If the value of auto_increment_offset is greater than that of auto_increment_increment, the value of auto_increment_offset is ignored.
翻译:如果auto_increment_offset的值大于auto_increment_increment的值,则auto_increment_offset的值会被忽略
比如:设置auto_increment_offset=3,auto_increment_increment=2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 mysql> set global auto_increment_increment=5;
2 Query OK, 0 rows affected (0.00 sec)
3
4 mysql> set global auto_increment_offset=3;
5 Query OK, 0 rows affected (0.00 sec)
6
7 mysql> show variables like 'auto_incre%'; #需要退出重新登录
8 +--------------------------+-------+
9 | Variable_name | Value |
10 +--------------------------+-------+
11 | auto_increment_increment | 1 |
12 | auto_increment_offset | 1 |
13 +--------------------------+-------+
14
15 create table student(
16 id int primary key auto_increment,
17 name varchar(20),
18 sex enum('male','female') default 'male'
19 );
20
21 mysql> insert into student(name) values('egon1'),('egon2'),('egon3');
22 mysql> select * from student;
23 +----+-------+------+
24 | id | name | sex |
25 +----+-------+------+
26 | 3 | egon1 | male |
27 | 8 | egon2 | male |
28 | 13 | egon3 | male |
29 +----+-------+------+
30
31 设置步长
设置步长
五、foreign key
1.快速理解foreign key
# 先建被关联得表,并且保证被关联的字段唯一 # 在建关联得表
# 先往被关联表插入数据 # 在往关联表插入数据
员工信息表有三个字段:工号 姓名 部门
公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费
解决方法:
我们完全可以定义一个部门表
然后让员工信息表关联该表,如何关联,即foreign key
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 1.表类型必须是innodb存储引擎,且被关联的字段,即references指定的另外一个表的字段,必须保证唯一
2 create table department(
3 id int primary key,
4 name varchar(20) not null
5 )engine=innodb;
6
7 2.dpt_id外键,关联父表(department主键id),同步更新,同步删除
8 create table employee(
9 id int primary key,
10 name varchar(20) not null,
11 dpt_id int,
12 constraint fk_name foreign key(dpt_id)
13 references department(id)
14 on delete cascade # 关联表会受到影响,删除被关联表时,关联得表会自动得删。
15 on update cascade # 关联表会受到影响,修改被关联表时,关联得表会自动得修改。
16 )engine=innodb;
17
18 3.先往父表department中插入记录
19 insert into department values
20 (1,'欧德博爱技术有限事业部'),
21 (2,'艾利克斯人力资源部'),
22 (3,'销售部');
23
24 4.再往子表employee中插入记录
25 insert into employee values
26 (1,'egon',1),
27 (2,'alex1',2),
28 (3,'alex2',2),
29 (4,'alex3',2),
30 (5,'李坦克',3),
31 (6,'刘飞机',3),
32 (7,'张火箭',3),
33 (8,'林子弹',3),
34 (9,'加特林',3);
35
36 5.删父表department,子表employee中对应的记录跟着删
37 mysql> delete from department where id=3;
38 mysql> select * from employee;
39 +----+-------+--------+
40 | id | name | dpt_id |
41 +----+-------+--------+
42 | 1 | egon | 1 |
43 | 2 | alex1 | 2 |
44 | 3 | alex2 | 2 |
45 | 4 | alex3 | 2 |
46 +----+-------+--------+
47
48 6.更新父表department,子表employee中对应的记录跟着改
49 mysql> update department set id=22222 where id=2;
50 mysql> select * from employee;
51 +----+-------+--------+
52 | id | name | dpt_id |
53 +----+-------+--------+
54 | 1 | egon | 1 |
55 | 3 | alex2 | 22222 |
56 | 4 | alex3 | 22222 |
57 | 5 | alex1 | 22222 |
58 +----+-------+--------+
59
60 快速理解foreign key
快速理解foreign key
2.如何找出两张表之间得关系:
分析步骤:
1、先站在左表的角度去找
是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id)
2、再站在右表的角度去找
是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段foreign key 左表一个字段(通常是id)
3、总结:
1.多对一:
如果只有步骤1成立,则是左表多对一右表
如果只有步骤2成立,则是右表多对一左表
2.多对多
如果步骤1和2同时成立,则证明这两张表是一个双向的多对一,即多对多,需要定义一个这两张表的关系表来专门存放二者的关系
3.一对一:
如果1和2都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。
这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可
3.建立表之间得关系:
1.多对一或一对多
出版社 书
分析:多个出版社出一本书,不能。多本书可以出自一个出版社,能。
关联方式:foreign key
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 多对一:
2 create table press(
3 id int primary key auto_increment,
4 name varchar(20)
5 );
6 create table book(
7 id int primary key auto_increment,
8 name varchar(20),
9 press_id int not null,
10 foreign key(press_id)
11 references press(id)
12 on delete cascade
13 on update cascade
14 );
15 insert into press(name) values
16 ('北京工业地雷出版社'),
17 ('人名音乐不好听出版社'),
18 ('知识产权没有用出版社');
19
20 insert into book(name,press_id) values
21 ('九阳神功',1),
22 ('九阴真经',2),
23 ('九阴白骨爪',2),
24 ('独孤九剑',3),
25 ('降龙十巴掌',2),
26 ('葵花宝典',3);
27
28 多对一
多对一
2.多对多
作者 书
分析:多个作者写一本书,能。多本书一个作者写,能。
关联方式:foreign key + 一张新得表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 多对多
2 create table author(
3 id int primary key auto_increment,
4 name varchar(20)
5 );
6 。。。书之前已建
7 #这张表就存放作者表与书表的关系,即查询二者的关系查这表就可以了
8 #中间那一张存放关系的表,对外关联的字段可以联合唯一
9 create table author2book(
10 id int not null unique auto_increment,
11 author_id int not null,
12 book_id int not null,
13 constraint fk_author foreign key(author_id) references author(id)
14 on delete cascade
15 on update cascade,
16 constraint fk_book foreign key(book_id) references book(id)
17 on delete cascade
18 on update cascade,
19 primary key(author_id,book_id)
20 );
21 insert into author(name) values('egon'),('alex'),('yuanhao'),('wpq');
22 #每个作者与自己的代表作如下
23 egon:
24 九阳神功
25 九阴真经
26 九阴白骨爪
27 独孤九剑
28 降龙十巴掌
29 葵花宝典
30 alex:
31 九阳神功
32 葵花宝典
33 yuanhao:
34 独孤九剑
35 降龙十巴掌
36 葵花宝典
37 wpq:
38 九阳神功
39 insert into author2book(author_id,book_id) values
40 (1,1),
41 (1,2),
42 (1,3),
43 (1,4),
44 (1,5),
45 (1,6),
46 (2,1),
47 (2,6),
48 (3,4),
49 (3,5),
50 (3,6),
51 (4,1);
52
53
54 多对多
多对多
3.一对一
学生表 客户表
分析:多个学生是一个客户,不能。多个客户是一个学生,不能。
关联方式:foreign key + unique
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 一对一
2 #一定是student来foreign key表customer,这样就保证了:
3 #1 学生一定是一个客户,
4 #2 客户不一定是学生,但有可能成为一个学生
5 create table customer(
6 id int primary key auto_increment,
7 name varchar(20) not null,
8 qq varchar(10) not null,
9 phone varchar(12) not null
10 );
11 create table student(
12 id int primary key auto_increment,
13 class_name varchar(20) not null,
14 customer_id int unique, # 该字段一定要唯一
15 foreigner key(customer_id) references customer(id)
16 on delete cascade
17 on update cascade
18 );
19 insert into customer(name,qq,phone) values
20 ('李飞机','31811231',13811341220),
21 ('王大炮','123123123',15213146809),
22 ('守榴弹','283818181',1867141331),
23 ('吴坦克','283818181',1851143312),
24 ('赢火箭','888818181',1861243314),
25 ('战地雷','112312312',18811431230);
26 insert into student(class_name,customer_id) values
27 ('脱产3班',3),
28 ('周末19期',4),
29 ('周末19期',5);
30
31 一对一
一对一