文章目录
0、约束定义(constraint)
约束是为了限制表中的数据,保证添加到数据表中的合法性、有效性、完整性!凡是不符合约束的数据,插入时就会失败!
1、常见约束
- 非空约束(not null):约束的字段不能为null
- 唯一约束(unique):约束的字段不能重复
- 主键约束(primary key):约束的字段既不能为Null也不能重复
- 外键约束(foreign key):
- 检查约束(check):
2、非空约束(not null)
示例:
创建一张表字段有id,username,password.默认让password字段不能为空
create table t_info1(
id int,
username varcha(10),
password varchar(20) not null
);
我们正常插入数据,即全部字段都赋值是没问题的。此时如果我们插入数据的password字段为空,或者省略不写(default默认值也是NULL)那么就会插入失败
password字段为非空约束着,因此报错
insert into t_info1(id,username,password)values(1,'zhangsan',NULL);
password字段为非空约束着,password字段不写使用默认值,差表结构值default 为null,相当该字段还是为空,插入错误,报错
insert into t_info1(id,username)values(1,'zhangsan');
3、唯一性约束(unique)
- 唯一约束修饰的字段具有唯一性,不能重复。但是可以为NULL,多个NULL之间不会重复,它不算值。
# 列级约束(单个字段约束)
create table 表名(
字段1 字段类型 unique,
字段2 字段类型 unique,
);
# 表级约束(多个字段联合约束)
create table 表名(
字段1 字段类型 ,
字段2 字段类型 ,
unique(字段1,字段3,……)
);
示例:创建一张表字段有id,username.默认让id字段不能重复
create table t_user(
id int unique,
username varchar(10)
);
然后向表中插入数据
insert into t_user values(0,'zhangsan'); 插入成功
insert into t_user values(0,'lisi'); 插入失败,因为id字段不能重复
insert into t_user(username) values('lisi');插入成功,id字段虽然不能重复但是可以为空
insert into t_user(username) values('lisi');插入成功
联合唯一性:
create table t_user(
单个字段每个不能重复
id int unique,
username varchar(10) unique
);
两个字段联合起来不能重复
create table t_user(
id int ,
username varchar(10),
unique(id,username)
);
注意两种的区别:
第一个:为单个字段不能重复
insert into t_user values(1,'ss');
insert into t_user values(1,'aa');
执行上述跟定不通过,因为id字段重复
第二个:为联合不能重复
insert into t_user values(1,'ss');
insert into t_user values(1,'aa');
执行上述跟定可以通过,因为id字段重复了, 但是username字段没有重复,它们联合起来没有重复。此时在insert into t_user values(1,'ss');
就报错重复了。
4、主键约束(primary key)
主键中的字段不能为空且唯一(不能重复)
//列级约束
create table 表名(
字段1 字段类型 primary key,
字段2 字段类型 ,
);
//表级约束
create table 表名(
字段1 字段类型,
字段2 字段类型,
primary key(字段) # 不建议使用复合主键,即primary key(字段1,字段2,……)违背三范式
);
示例:
# 如果表t_user存在,则删除
drop table if exists t_user;
# 创建t_user表,id字段为主键约束(非空且唯一)
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@11.com');
insert into t_user(id,username,email) values(2,'ls','ls@11.com');
insert into t_user(id,username,email) values(3,'ww','ww@11.com');
select * from t_user;
创建成功后表的结构如下
现在再向表中添加如下数据
# 插入失败,id为主键约束即不能为空且不可重复
insert into t_user(id,username,email) values(1,'ml','ml@11.com');# id重复
# 插入失败,id为主键约束即不能为空且不可重复
insert into t_user(username,email) values('zl','zl@11.com');#id为NULL
①、主键相关术语
- 主键约束:即primary key
- 主键字段:主键约束的字段称为主键字段
- 主键值:主键字段插入的值称为主键值
②、主键作用?
表的设计三范式中有要求,第一范式就要求任何一张表都应该有主键
主键值是这行记录在这张表当中唯一的标识,只要主键值不相等,就认为它是两条不同的记录值,即使其他字段完全相等。
③、主键分类
根据字段数量划分:
- 单一主键:只对一个字段添加主键约束(常用)
- 复合主键:多个字段联合添加一个主键约束(不建议用,违背表的设计中的三范式)
根据主键性质来划分:
- 自然主键:主键值最好是一个和业务没有任何关系的自然数
- 业务主键:主键值和系统业务的业务挂钩(如:银行业务中的银行卡号,身份证号等),不推荐使用
一张表的主键约束只能有一个
④、MySQL提供主键自增(auto_increment)
creat table 表名(
字段1 字段类型 primary key auto_increment, # 字段1自动维护一个自增的数字,从1开始,以1递增
字段2 字段类型,
……
);
示例:
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('aa'); # id不给值,默认自增
insert into t_user(username) values('bb');
insert into t_user(username) values('cc');
insert into t_user(username) values('dd');
insert into t_user(username) values('ee');
select *from t_user;
5、外键约束(foreign key)
外键约束是表的一个特殊字段,经常与主键约束一起使用。对于两个具有关联关系的表而言,相关联字段中主键所在的表就是主表(父表),外键所在的表就是从表(子表)
。
外键用来建立主表与从表的关联关系,为两个表的数据建立连接,约束两个表中数据的一致性和完整性。
外键是一列或一组列,用于强制两个表中的数据之间的链接。 在外键引用中,第一个表的主键列(或多个列)由第二个表的列(或列)引用。
第二个表的列(或列)成为外键。
原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:https://www.yiibai.com/sql/sql-foreign-key.html
create table 表名(
字段1 类型,
字段2 类型,
字段3 类型
foreign key(本表中的某字段如:字段2) references 父表名(父表中的某字段)
);
看下面例子:
①、外键约束术语
- 外键约束:即foreign key
- 外键字段:添加了外键约束的字段
- 外键值:外键字段中的每一个值
②、主键作用?
学生表中的clasnum字段引用班级表中的classnumber字段,此时学生表成为子表
。班级表成为父表
。
- 创建/插入数据时先创建/插入父表中的数据
- 删除表/数据时先删除字表/数据
- 外键字段的值必须是父表中该字段的值
上述表的创建语句为:
先删子表再删父表
drop table if exists t_student;
drop table if exists t_class;
先创建父表再删除字表
create table t_class(
classnumber varchar(3) primary key, 主键
classname varchar(255)
);
create table t_student(
id int primary key auto_increment , 主键 自增
name varchar(255) not null, 唯一性约束:不为空
clasnum varchar(3),
约束:外键约束!!!!!!!!!!!!!!!!!!!!!!!!!!
foreign key(clasnum) references t_class(classnumber)
);
先向父表中插入数据
insert into t_class values
('301','hupanzhongxuegaosan1ban'),
('302','hupanzhongxuegaosan2ban');
再向字表中插入数据
insert into t_student(name,clasnum) values
('zhangsan','301'),
('lisi','301'),
('wangwu','302'),
('liuliu','302'),
('niuniu','302');
select * from t_class;
select * from t_student;
在t_student表中插入下面的数据就会失败,因为外键字段引用的字段中没有添加303
insert into t_student(name,clasnum) values
('lala','303');
- 外键值可以为NULL
- 外键引用其他表的某个字段时,被引用的字段不一定是主键,但是必须是唯一性约束(unique),即被引用的字段不能重复