数据库约束是在数据库中定义的规则,用于保证数据的完整性和一致性。它们用于限制对数据库表中数据的插入、更新和删除操作,以确保数据的有效性和准确性。以下是几种常见的数据库约束:
主键约束(Primary Key Constraint):主键是一个唯一标识数据库表中每一行记录的字段或字段组合。主键约束要求主键字段的值在表中是唯一的,且不能为空(即非空)。主键约束保证了每个记录的唯一性,并且提供了一种快速查找和访问记录的方式。
唯一约束(Unique Constraint):唯一约束要求某个或某些字段的取值在表中是唯一的,但可以为空。与主键约束不同的是,唯一约束可以允许空值。唯一约束可以用于确保某个字段或字段组合的取值的唯一性,例如确保电子邮件地址或用户名的唯一性。
外键约束(Foreign Key Constraint):外键约束用于建立表与表之间的关系。它定义了一个表中的字段(称为外键),其取值必须在另一个表的主键字段中存在。外键约束可以用于实现表之间的引用完整性,保证相关表之间的数据一致性。
非空约束(Not Null Constraint):非空约束要求某个字段的值不能为空。这可以防止在插入或更新记录时将空值存储到该字段中,确保字段中的数据是有效的。
检查约束(Check Constraint):检查约束定义了一个逻辑表达式,用于对某个字段的取值进行验证。只有满足表达式条件的记录才能被插入或更新到表中。检查约束可以用于限制字段的取值范围或满足特定的条件。
这些约束可以在创建表时定义,也可以在已存在的表上添加或删除。它们对数据库的数据完整性和一致性起到了重要的作用,帮助避免无效或不一致的数据存储在数据库中。
讲人话:
在创建表的时候,可以给表的字段添加相应的约束,添加约束的目的是为了保证表中数据的合法性,有效性,完整性.
常见约束:
1.非空约束(not null):约束的字段不能为null;
2.唯一约束(unique):约束的字段不能重复,但是,可以是null 原因可以这么理解(null在数据库中并不是用==比较,is null 或者 is not null 可以理解null和null不一样 哈哈哈)
3.主键约束(primary key):约束的字段不能重复,同时也不能为null (简称PK)
4.外键约束(foreign key):
5.检查约束(check):注意Oracle数据库有check约束,但是Mysql没有,目前Mysql不支持该约束
1.非空约束没什么好演示的
2.唯一性约束:唯一约束修饰的字段具有唯一性,不能重复,但可以为null
例子:
drop table if exists t_user (在Oracle中好像不能这么干) 只能drop table t_user
create table t_user(
id int,
username varchar(255) unique
);
当然也可以这么写
create table t_user(
id int,
username varchar(255),
unique(username)
);
insert into t_user values(1,'zhangsan');
insert into t_user values(1,'zhangsan');
ERROR 1062 (23000): Duplicate entry 'zhangsan' for key 't_user.username'
上面说明该约束字段不能重复
insert into t_user(id) values(2);
insert into t_user(id) values(3);
insert into t_user(id) values(4);
说明唯一约束的字段可以为null
给两个列或者多个列添加唯一约束
create table t_user(
id int,
usercode varchar(255) unique,-------列级唯一约束
username varchar(255) unique,-------列级唯一约束
);
区别是一个是单列不能重复,一个是多列不能重复
上面是单个字段唯一,下面是多个字段组合一起唯一
create table t_user(
id int,
usercode varchar(255),
username varchar(255),
unique(usercode,username)-----表级唯一约束
);
但是发现添加联合唯一约束 如果 '111',null 有一个为null的话,会一直可以插入
insert into t_user values(1,'111',null);
insert into t_user values(1,'111',null);
insert into t_user values(1,'111','222');
insert into t_user values(1,'111','222');
那就这样呗,非空约束加唯一约束
create table t_user(
id int,
usercode varchar(255) not null,
username varchar(255) not null,
unique(usercode,username)
);
3.主键约束:非空且唯一
drop table if exists t_user;
create table t_user(
id int primary key,
username varchar(255),
email varchar(255)
);
insert into t_user(id,username,email) values(1,'zs','[email protected]');
insert into t_user(id,username,email) values(2,'ls','[email protected]');
insert into t_user(id,username,email) values(3,'ww','[email protected]');
主键相关术语废话:primary key为主键约束,id为主键字段, 插入的为主键值
主键的作用,作为该条数据的唯一标识 一张表只能有一个主键
表设计三范式中要求:第一范式就要求任何一张表都应该有主键.
主键的分类:
根据主键字段的字段数量来划分:
单一主键(推荐的,常用的)
复合主键(多个字段联合起来添加一个主键约束),不建议使用,因为符合主键违背三范式
根据主键性质来划分:
自然主键
业务主键:主键值和系统业务挂钩,比如用银行卡卡号作为主键,用身份证号做主键(不推荐使用)
最好不要拿和业务挂钩的字段作为主键.因为有可能业务改变时,主键值也随着发生变化.而变化可能导致主键重复,反正不推荐使用
使用表级主键约束定义主键
drop table if exists t_user;
create table t_user(
id int,
username varchar(255),
primary key(id)
)
使用字段级主键约束定义主键
drop table if exists t_user;
create table t_user(
id int primary key(id)
username varchar(255),
)
演示符合主键约束定义主键
drop table if exists t_user;
create table t_user(
id int,
username varchar(255),
password varchar(255),
primary key(id,username)
)
Mysql提供主键值自增
drop table if exists t_user;
create table t_user(
id int primary key auto_increment,
username varchar(255),
password varchar(255)
)
insert into t_user(username) values('a');
注意:Oracle中也提供了一个自增机制,叫做序列(sequence)对象
4.外键约束:一张表的某字段的值是另外一张表的主键(非必须)(且一个为主表,一个为子表,删除时候需要先删子再删父)原因:外键所在表还在用着主表数据,添加数据时候,先添加父表再添加子表,创建表的时候,先创建父表,再创建子表,删除表的时候,先删除子表再删除父表(有操作顺序)
外键必须引用主表的主键或具有唯一约束的列,是不是主键不是必须
关于外键约束的相关术语:
外键约束:foreign key
外键字段:添加有外键约束的字段
外键值:外键字段中的每个值
外键演示
drop table if exists t_student;
drop table if exists t_class;
create table t_class(
cid int primary key,
cname varchar(255)
);
create table t_student(
sid int primary key,
sname varchar(255),
classid int,
foreign key(classid) references t_class(cid)------指定外键 引用t_class的(cid)
);
当子表需要插入字段是外键的时候,如果主表没有这个值,那么会失败
比如说主表是
1 张三
子表想插入
1, 学生1 2(该值在父表中不存在) 报失败 注意,外键可以为null(但是如果外键是另外一张表的主键的时候,则不可以为null)
外键字段引用其他表的某个字段的时候,被引用的字段必须引用主表的主键或具有唯一约束的列