Constraints (finishing)

1. Concept and classification

Concept: Constraints are rules that act on fields in a table to limit the data stored in the table

Purpose: To ensure the correctness, validity and integrity of the data in the database.

Classification:

constraint describe keywords
not-null constraint Restrict the data of this field to not be null not null
unique constraint Ensure that all data in this field is unique and not repeated unique
primary key constraint The primary key is the unique identifier of a row of data, which requires non-null and unique primary key
default constraints When saving data, if the value of this field is not specified, the default value will be used default
Check constraints (after 8.0.16) Ensure that the field value satisfies a certain condition check
foreign key constraints It is used to establish a connection between the data of the two tables to ensure the consistency and integrity of the data foreign key

[Note]: Constraints are applied to the fields in the table, and constraints can be added when creating/modifying tables.

case study

field name field meaning Field Type Restrictions constraint keyword
id id unique identification int Primary key, and auto-increment primary key,auto_increment
name Name varchar(10) is not empty and unique not null,unique
age age int Greater than 0 and less than or equal to 120 check
status state char(1) If this value is not specified, it defaults to 1 default
gender gender char(1) none
create table luse(
    id int primary key auto_increment comment '主键',
    name varchar(10) not null unique comment '姓名',
    age int check ( age > 0 && age <= 120 ) comment '年龄',
    status char(1) default(1) comment '状态',
    gender char(1) comment '性别'
)comment '用于练习约束的用户表';

 

 2. Foreign key constraints

Behavior illustrate
no action When deleting/updating the corresponding record in the parent table, first check whether the record has a corresponding foreign key, and if so, delete/update is not allowed. (consistent with restrict)
restrict When deleting/updating the corresponding record in the parent table, first check whether the record has a corresponding foreign key, and if so, delete/update is not allowed. (consistent with no action)
cascade When deleting/updating the corresponding record in the parent table, first check whether the record has a corresponding foreign key, and if so, delete/update the record with the foreign key in the child table.
set null When deleting the corresponding record in the parent table, first check whether the record has a corresponding foreign key, and if so, set the value of the foreign key in the child table to null (this requires that the foreign key allows null).
set default When the parent table is changed, the child table sets the foreign key column to a default value (innodb does not support)
alter table 表名 add constraint 外键名称 
foreign key (外键字段) references 主表名(主表字段名)
on update 行为 on delete 行为;

#这里的外键名称是自己取的

Auxiliary table content:

create table fkdept(
    id int auto_increment comment '编号' primary key ,
    name varchar(50) not null comment '部门名称'
)comment '用于练习外键约束的部门表';

insert into fkdept(id, name) values (1,'研发部'),(2,'市场部'),(3,'财务部'),(4,'销售部'),(5,'总经办');

create table fkemp(
    id int auto_increment primary key comment '编号',
    name varchar(50) not null comment '姓名',
    age int comment '年龄',
    job varchar(20) comment '职位',
    salary int comment '薪资',
    entrydate date comment '入职时间',
    managerid int comment '直属领导id',
    dept_id int comment '部门id'
)comment '用于练习外键约束的员工表';

insert into fkemp (id,name,age,job,salary,entrydate,managerid,dept_id)
            values  (1,'金庸',66,'总裁',20000,'2000-01-01',null,5),
                    (2,'张无忌',20,'项目经理',12500,'2005-12-05',1,1),
                    (3,'杨逍',33,'开发',8400,'2000-11-03',2,1),
                    (4,'韦一笑',48,'开发',11000,'2002-02-05',2,1),
                    (5,'常遇春',43,'开发',10500,'2004-09-07',3,1),
                    (6,'小昭',19,'程序员鼓励师',6600,'2004-10-12',2,1);

Only the usage here has no effect

-- 添加外键
alter table fkemp 
add constraint fk_fkemp_dept_id 
foreign key (dept_id) references fkdept(id);
-- 删除外键
alter table fkemp 
drop foreign key fk_fkemp_dept_id;
-- 外键的删除和更新行为
alter table fkemp 
add constraint fk_fkemp_dept_id 
foreign key (dept_id) references fkdept(id) 
on update cascade on delete cascade ;
alter table fkemp 
add constraint fk_fkemp_dept_id 
foreign key (dept_id) references fkdept(id) 
on update set null on delete set null ;

Guess you like

Origin blog.csdn.net/weixin_43294936/article/details/123292975