1、什么是约束?
在创建表的时候,可以给表的字段添加相应的约束,添加约束的目的是为了保证表中数据的
合法性、有效性、完整性。
2、常见的约束
- 非空约束,not null : 非空约束,针对某个字段设置其值不为空
- 唯一约束,unique :约束的字段不能重复,但可以为null
- 主键约束,primary key :约束的字段既不能为NULL,也不能重复
- 外键约束,foreign key :
2.1、非空约束 not null
drop table if exists t_user;
create table t_user(
id int,
username varchar(255) not null,
password varchar(255)
);
insert into t_user(id,password) values(1,'123');
ERROR 1364 (HY000): Field 'username' doesn't have a default value
#username没有写入值,违背了非空约束
insert into t_user(id,username,password) values(1,'lisi','123');
Query OK, 1 row affected (0.01 sec)
2.2、唯一性约束(unique)
# 案例1:给某一列添加unique
drop table if exists t_user;
create table t_user(
id int,
username varchar(255) unique // 列级约束
);
insert into t_user values(1,'zhangsan');
insert into t_user values(2,'zhangsan');
ERROR 1062 (23000): Duplicate entry 'zhangsan' for key 'username'
insert into t_user(id) values(2);
insert into t_user(id) values(3);
insert into t_user(id) values(4);
# 案例2:给两个列或者多个列添加unique
drop table if exists t_user;
create table t_user(
id int,
usercode varchar(255),
username varchar(255),
unique(usercode,username) // 多个字段联合起来添加1个约束unique 【表级约束】
);
2.3、主键约束 primary key
### 怎么给一张表添加主键约束呢?
create table t_user(
id int primary key, #列级约束
username varchar(255),
email varchar(255)
);
insert into t_user(id,username,email) values(1,'zs','zs@123.com');
insert into t_user(id,username,email) values(2,'ls','ls@123.com');
insert into t_user(id,username,email) values(3,'ww','ww@123.com');
select * from t_user;
+----+----------+------------+
| id | username | email |
+----+----------+------------+
| 1 | zs | zs@123.com |
| 2 | ls | ls@123.com |
| 3 | ww | ww@123.com |
+----+----------+------------+
主键相关的术语
主键约束 : primary key
主键字段 : id字段添加primary key之后,id叫做主键字段
主键值 : id字段中的每一个值都是主键值。
主键的分类
- 根据主键字段的字段数量来划分:
单一主键(推荐,常用的。)
复合主键(多个字段联合起来添加一个主键约束)(复合主键不建议使用,因为复合主键违 背三范式。)
- 根据主键性质来划分:
自然主键:主键值最好就是一个和业务没有任何关系的自然数。(这种方式是推荐的)
业务主键:主键值和系统的业务挂钩,例如:拿着银行卡的卡号做主键,拿着身份证号码 作为主键。(不推荐用)
注意点:一张表只能使用1个主键约束。
mysql提供主键值自增:(auto_increment)
drop table if exists t_user;
create table t_user(
id int primary key auto_increment, # id字段自动维护一个自增的数字,从1开始,以1递增。
username varchar(255)
);
insert into t_user(username) values('a');
insert into t_user(username) values('b');
insert into t_user(username) values('c');
insert into t_user(username) values('d');
insert into t_user(username) values('e');
insert into t_user(username) values('f');
select * from t_user;
+----+----------+
| id | username |
+----+----------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |
| 6 | f |
+----+----------+
2.4、外键约束
关于外键约束的相关术语:
外键约束: foreign key
外键字段:添加有外键约束的字段
外键值:外键字段中的每一个值。
案例:创建两张表,班级表t_class和学生表t_student, t_student中的classno字段引用t_class表中的cno字段,此时t_student表叫做子表。t_class表叫做父表。
创建顺序要求:
- 删除数据的时候,先删除子表,再删除父表。
- 添加数据的时候,先添加父表,在添加子表。
- 创建表的时候,先创建父表,再创建子表。
- 删除表的时候,先删除子表,在删除父表。
create table t_class(
cno int,
cname varchar(255),
primary key(cno)
);
create table t_student(
sno int,
sname varchar(255),
classno int,
primary key(sno),
foreign key(classno) references t_class(cno) #外键约束
);
insert into t_class values(101,'xxxx');
insert into t_class values(102,'yyyy');
insert into t_student values(1,'zs1',101);
insert into t_student values(2,'zs2',101);
insert into t_student values(3,'zs3',102);
insert into t_student values(4,'zs4',102);
insert into t_student values(5,'zs5',102);
insert into t_student values(6,'zs6',102);
select * from t_class;
select * from t_student;
+-----+-------+
| cno | cname |
+-----+-------+
| 101 | xxxx |
| 102 | yyyy |
+-----+-------+
注意点:
- 外键值可以为NULL? —— 可以
- 外键字段引用其他表的某个字段的时候,被引用的字段必须是主键吗?
不必须,但至少要具有unique约束