表的约束
为了防止不符合规范的数据存入数据库,在用户对数据进行插入、修改、删除等操作时,
MySQL 数据库管理系统提供了一种机制来检查数据库中的数据是否满足规定的条件,以保
证数据库中数据的准确性和一致性,这种机制就是约束。
一、完整性约束
[外链图片转存失败(img-rbQUMbpF-1562326334076)(C:\Users\Administrator\Desktop\表的完整性约束.jpg)]
从约束条件上可以分为两类:
1)表级约束:可以约束表中任意一个 或者多个字段。
2)列级约束:只能约束其所在的某一个字段。
二、主键约束
主键约束(primarykey ,缩写 PK),作用是约束表中的某个字段可以唯一标识一条记录。因此使用主键约束可以快速查找表中的记录。就像人的身份证、学生的学号。因此设置的主键的字段不能重复(唯一),且不能为空(非空)。
1、创建列表时添加主键约束
主键可以对单个字段,也可以对多个字段的添加约束。对单个字段可以表级约束也可以列级约束;对多个字段主键添加只能使用表级约束。
a. 单个字段添加主键约束
create table table_name(
column_name1 date_type primary key,
column_name2 date_type,
...... );
'''
其中“table_name”为新创建的表的名称,“column_name1”为添加主键的字段名,
“date_type”为字段的数据类型,“primary key”为设置主键所用的 SQL 语句
'''
#举个列级约束的栗子
create table student1(
stu_id int(10) primary key,
stu_name varchar(20),
);
desc student1 #查看表的基本结构
单个字段可以添加表级约束
create table student2(
stu_id int(10),
stu_name varchar(5),
stu_age int(2),
constraint pk_stu_id primary key(stu_id)
);
其中,“constraint pk_name”为可选项,“constraint”为设置主键约束标识符所用到的
关键字,“pk_name”为主键标识符;
b.为多个字段添加主键约束
给多个字段添加主键约束只能用表级约束
create table student3(
stu_id int(2),
stu_name varchar(20),
stu_age int(2),
primary key(stu_id,stu_name)
);
2、在已经存在的表中添加主键约束
alter table t_student add primary key(id,name);
#注意添加表中的主键时表中没有主键,否则会报错
3、删除主键
alter table t_student drop primary key;
desc t_student;
三、唯一约束
唯一约束(unique , 缩写 UK),它指定了某个字段的值不能重复,即这一字段的每个值都是唯一的。
1、使用列级约束添加唯一约束
create table student4(
stu_id int(10) unique,
stu_name varchar(20) unique,
stu_sex varchar(3)
);
2、使用表级约束添加唯一约束
create table student5(
stu_id int(2),
stu_name varchar(3),
stu_age int(1),
unique(stu_id,stu_name)
);
#注意 表级
#约束为多个字段添加唯一约束后,实际上是将被约束的多个字段看成了一个组合,只有当组
#合字段中的值全部重复时,才会提示插入数据失败。这跟上述列级的唯一约束的不同。
3、在已存在的表中添加唯一约束
alter table student6 add unique(stu_id);
alter table student6 add unique(stu_name); #分别给两个字段添加唯一约束, 如果添加的数据必须都得满足这两个字段不能重复才能添加成功,显示类型为‘UNI’
alter table student6 add unique(stu_id,stu_name) #给多个字段添加唯一约束,添加的数据只有都重复时才失败,显示类型为‘MUL’
4、删除唯一约束
alter table student7 drop index stu_id;
#stu_id 是唯一约束名,如果没有指定,则为默认字段名
四、非空约束
非空约束(NOT NULL,缩写 NK)规定了某个字段值不能为空(NULL)。无论是单个字段还是多个字段非空约束的添加只能使用列级约束。
1、在创建表时添加非空约束。
create table student8(
stu_id int(10) not null,
stu_name varchar(3) not null,
stu_age int(2)
);
2、在已存在的表中添加非空约束
alter table studet8 modify stu_name varchar(3) not null;
3、删除非空约束
alter table student8 modify stu_name varchar(3) null;
五、默认值约束
默认值约束(DEFAULT)用来规定字段的默认值。如果某个被设置为 DEFAULT 约束的字段没有插入具体的值,那么该字段的值将会被默认值填充。
1、在创建表时添加默认值约束
create table student9(
stu_id int(2),
stu_name varchar(10),
stu_sex varchar(2) default '男'
);
2、在已有的表中添加默认值约束
alter table student9 modify stu_name varchar(3) default '男';
3、删除默认值
alter table student9 modify stu_name varchar(3);
六、字段值自动增加约束
自增约束(AUTO_INCREMENT)可以使表中某个字段的值自动增加。一张表中只能有一个自增长字段,并且该字段必须定义了约束(该约束可以是主键约束、唯一约束以及外键约束)自增约束通常会配合主键使用,并且只适用于整数类型。一般情况下,设置为自增约束字段的值会从 1 开始,每增加一条记录,该字段的值加 1。
字段值自动增加约束的添加,删除
#在创建表时添加自增约束
create table student10(
stu_id int(10) primary key auto_increment,
stu_name varchar,
);
#在已有表中添加自增约束
alter table student10 modify stu_id int(10) auto_increment;
#删除自增约束
alter table student10 modify stu_id int(10);
七、外键约束
外键约束(FOREIGN KEY,缩写 FK)是用来实现数据库表的参照完整性的。外键约
束可以使两张表紧密的结合起来,特别是针对修改或者删除的级联操作时,会保证数据的完
整性外键是指表中某个字段的值依赖于另一张表中某个字段的值,而被依赖的字段必须具有
主键约束或者唯一约束。
#在创建表时添加外键约束
create table clazz(
cla_no int(3) primary key,
cla_name varchar(20),
cla_loc varchar(30)
);
create table student12(
stu_id int(10) primary key,
stu_name varchar(3),
stu_clazz int(3),
constraint fk_stu_clazz
foreign key(stu_clazz) references clazz(cla_no)
);
'''
其中“child_table_name”为新建表的名称(该表为从表);“constraint fk_name”为
可选项,“fk_name”为外键约束名,用来标识外键约束;“foreign key”用来指定表的外
键字段;“child_column_name”为外键字段;“references”来指定外键字段参照的表
'cla_no' 为主表中被参照的字段
'''
#在已有表中添加外键约束
create table student13(
stu_id int(10) primary key,
stu_name varchar(3),
stu_clazz int(3)
);
alter table student13 add constraint fk_stu_clazz_1 foreign key(stu_clazz) references
clazz(cla_no);
#删除外键约束
alter table student13 drop foreign key fk_stu_clazz_1;
**主表的删除
要删除表主表clazz 会报错,应该她跟从表student12 关联着,应先删除 外键约束条件 ,再删除主表。
alter table student12 drop foreign key fk_stu_clazz_1;
drop table clazz;