Mysql数据库-字段约束

数据库字段约束

  • 1. 字段约束原因
  • 2. 约束类型
    • 2.1 非空约束(not null)
    • 2.2 唯一约束(unique)
    • 2.3 主键约束(primary key)
      • 2.3.1 创建表时添加主键约束
      • 2.3.2 删除数据表主键约束
      • 2.3.3 创建表之后添加主键约束
    • 2.4 主键自动增长(auto_increment)
    • 2.5 联合主键
    • 2.6 外键约束(foreign key)
    • 2.7 外键约束-级联

1. 字段约束原因

  • 保证数据有效性
  • 保证数据完整性
  • 保证数据正确性

2. 约束类型

2.1 非空约束(not null)

限制数据表中此列值不能为空

创建表:设置学生表中的stu_name非空

create table student (
	stu_number char(8),
	stu_name varchar(10) not null,
	stu_age int 
)

添加数据:添加stu_name为空的一条数据提示添加错误
Mysql数据库-字段约束_第1张图片

2.2 唯一约束(unique)

在表中的多条数据,此列中的数据不能重复

创建表:设置student表中的stu_number为unique

create table student (
	stu_number char(8) unique,
	stu_name varchar(10) ,
	stu_age int 
)

添加数据:添加stu_number的数据已经存在,会有如下提示:

Mysql数据库-字段约束_第2张图片

2.3 主键约束(primary key)

数据表中记录的唯一标识,在一张表中只能有一个主键,主键可以是一个列,也可以是多个列的组合。主键不能为null,不能重复,唯一标识。

2.3.1 创建表时添加主键约束

创建表:设置stu_number为主键

create table student (
	stu_number char(8) primary key,
	stu_name varchar(10) ,
	stu_age int 
)
或者
create table student (
	stu_number char(8) ,
	stu_name varchar(10) ,
	stu_age int,
	primary key (stu_number)
)

添加数据:stu_number为空时,提示如下报错:
Mysql数据库-字段约束_第3张图片
stu_number重复时,提示如下报错:
Mysql数据库-字段约束_第4张图片

2.3.2 删除数据表主键约束

alter table student drop primary key;

2.3.3 创建表之后添加主键约束

创建表时未添加主键约束

create table student (
	stu_number char(8) ,
	stu_name varchar(10) ,
	stu_age int 
)

创建表后添加主键约束

alter table student modify stu_number char(8) primary key;

2.4 主键自动增长(auto_increment)

当数据表中没有合适的列做主键时,可以自定义一个与记录无关的列(ID)作为主键,此ID无具体含义主要是用作唯一标识。mysql中定义此列为int类型,同时设置自动增长,当数据表新增一条数据时,无需提供ID值,会自动+1生成

创建表:int类型主键自动增长 auto_increment

create table types (
	type_id int primary key auto_increment,
	type_name varchar(20) not null,
	type_remark varchar(100)
)

插入数据时,type_id列为自动生成。当把type_id = 4删除后再次添加数据,下一条数据type_id为5,即自动增长只保证唯一性,不保证连续性。
Mysql数据库-字段约束_第5张图片

2.5 联合主键

将数据表中的多列组合在一起设置为该表的主键
Mysql数据库-字段约束_第6张图片
定义联合主键表:

create table grades(
	stu_num char(8),
	course_id int,
	score int,
	primary key(stu_num,course_id)
);

注:实际项目使用频率不高,可以使用主键自动增长的方式解决。

2.6 外键约束(foreign key)

外键约束:将一个表的列添加外键约束,与另一张表的主键进行关联,外键列添加的数据必须在主键字段中存在

  • 创建班级表

    create table class(
    	class_id int primary key auto_increment,
    	class_name varchar(30) not null unique,
    	class_remark varchar(200)
    );
    
  • 创建学生表(在学生表中添加外键cid,与班级表中的主键class_id进行关联)
    【方式一】在创建表的时候,定义cid字段并添加外键约束。 因为 cid和class表的class_id进行关联,因此cid字段类型和长度应和class_id保持一致。

    create table students(
    	stu_num char(8) primary key,
    	stu_name varchar(20) not null,
    	stu_sex char(2) not null,
    	stu_age int not null,
    	cid int,
    	constraint FK_Students_Class foreign key(cid) references class(class_id)
    );
    

    【方式二】先创建表,在添加外键约束

    	create table students(
        	stu_num char(8) primary key,
        	stu_name varchar(20) not null,
        	stu_sex char(2) not null,
        	stu_age int not null,
        	cid int
        );
    

    添加外键约束

    alter table students add constraint FK_Students_class foreign key(cid) references class(class_id);
    
  • 插入数据
    班级表class插入数据

    insert into class (class_id,class_name) values ('1','java(1)班');
    insert into class (class_id,class_name) values ('2','java(2)班');
    insert into class (class_id,class_name) values ('3','java(3)班');
    insert into class (class_id,class_name) values ('4','python(1)班');
    insert into class (class_id,class_name) values ('5','python(2)班');
    

    查看学生表students中外键cid,仅能选择填写class表中class_id列所存在的值。
    Mysql数据库-字段约束_第7张图片

  • 删除外键

    alter table students drop foreign key FK_Students_Class;
    

2.7 外键约束-级联

如下:学生表students中的cid已经关联了class表中的class_id,不能对class表中的class_id进行修改或者删除操作

mysql> select * from class;
+----------+-------------+--------------+
| class_id | class_name  | class_remark |
+----------+-------------+--------------+
|        1 | java2021    | NULL         |
|        2 | java(2)| NULL         |
|        3 | java(3)| NULL         |
|        4 | python(1)| NULL         |
|        5 | python(2)| NULL         |
+----------+-------------+--------------+


mysql> select * from students;
+----------+----------+---------+---------+------+
| stu_num  | stu_name | stu_sex | stu_age | cid  |
+----------+----------+---------+---------+------+
| 20210001 | 张三     ||      18 |    1 |
| 20210002 | 李四     ||      20 |    2 |
| 20210003 | 王五     ||      20 |    1 |
| 20210004 | 李六     ||      24 |    1 |
+----------+----------+---------+---------+------+

可以采用级联操作来实现

  1. 在添加外键时,设置级联修改级联删除
##删除外键约束
alter table students drop foreign key FK_Students_Class;
## 新建外键约束,设置级联修改和级联删除
alter table students add constraint FK_Students_Class foreign key(cid) references class(class_id) on update cascade on delete cascade;
  1. 级联修改,修改class表中主键class_id =1 ,修改后为class_id = 8;查看students表外键cid同步成功修改。
mysql> select * from class;
+----------+-------------+--------------+
| class_id | class_name  | class_remark |
+----------+-------------+--------------+
|        1 | java2021    | NULL         |
|        2 | java(2)| NULL         |
|        3 | java(3)| NULL         |
|        4 | python(1)| NULL         |
|        5 | python(2)| NULL         |
+----------+-------------+--------------+

mysql> select * from students;
+----------+----------+---------+---------+------+
| stu_num  | stu_name | stu_sex | stu_age | cid  |
+----------+----------+---------+---------+------+
| 20210001 | 张三     ||      18 |    1 |
| 20210002 | 李四     ||      20 |    2 |
| 20210003 | 王五     ||      20 |    1 |
| 20210004 | 李六     ||      24 |    1 |
+----------+----------+---------+---------+------+

mysql> update class set class_id = 8 where class_name = 'java2021';

mysql> select * from students;
+----------+----------+---------+---------+------+
| stu_num  | stu_name | stu_sex | stu_age | cid  |
+----------+----------+---------+---------+------+
| 20210001 | 张三     ||      18 |    8 |
| 20210002 | 李四     ||      20 |    2 |
| 20210003 | 王五     ||      20 |    8 |
| 20210004 | 李六     ||      24 |    8 |
+----------+----------+---------+---------+------+
  1. 级联删除,删除class表中class_id为8的数据,students同步删除cid为8的数据
mysql> select * from students;
+----------+----------+---------+---------+------+
| stu_num  | stu_name | stu_sex | stu_age | cid  |
+----------+----------+---------+---------+------+
| 20210001 | 张三     ||      18 |    8 |
| 20210002 | 李四     ||      20 |    2 |
| 20210003 | 王五     ||      20 |    8 |
| 20210004 | 李六     ||      24 |    8 |
+----------+----------+---------+---------+------+

mysql> select * from class;
+----------+-------------+--------------+
| class_id | class_name  | class_remark |
+----------+-------------+--------------+
|        2 | java(2)| NULL         |
|        3 | java(3)| NULL         |
|        4 | python(1)| NULL         |
|        5 | python(2)| NULL         |
|        8 | java2021    | NULL         |
+----------+-------------+--------------+
mysql> delete from class where class_id ='8';

mysql> select * from students;
+----------+----------+---------+---------+------+
| stu_num  | stu_name | stu_sex | stu_age | cid  |
+----------+----------+---------+---------+------+
| 20210002 | 李四     ||      20 |    2 |
+----------+----------+---------+---------+------+

你可能感兴趣的:(数据库,数据库,mysql,database)