[Super detailed] Constraints in Mysql database

Table of contents

1. The concept and classification of constraints

1. The concept of constraints

2. Classification of constraints

2. Case Analysis 

1. Not null constraint (not null)

2. Primary key constraint (primary key)

3. Default constraints (defaut)

4. Automatic growth (auto_increment)

5. Foreign key constraints


1. The concept and classification of constraints

1. The concept of constraints

  • Constraints are rules that act on columns in a table to limit the data that is added to the table
  • The existence of constraints ensures the correctness, validity and integrity of data in the database

2. Classification of constraints

2. Case Analysis 

DROP TABLE IF EXISTS emp;

-- 员工表
CREATE TABLE emp (
  id INT PRIMARY KEY auto_increment, -- 员工id,主键且自增长 -- 【auto_increment】
  ename VARCHAR(50) NOT NULL UNIQUE, -- 员工姓名,非空并且唯一
  joindate DATE NOT NULL , -- 入职日期,非空
  salary DOUBLE(7,2) NOT NULL , -- 工资,非空
  bonus DOUBLE(7,2) DEFAULT 0 -- 奖金,如果没有奖金默认为0
);

1. Not null constraint (not null)

-- 演示非空约束
insert into emp(id,ename,joindate,salary,bonus) values(3,null,'1999-11-11',8800,5000)-- 报错 null
insert into emp(id,ename,joindate,salary,bonus) values(3,"李四",'1999-11-11',8800,5000)-- 报错 唯一

(1) Add constraints:

-- Add a not-null constraint when creating the table

create table table name (

        column name data type not null ,

....

) ;

-- Add constraints after creating the table

alter table table name modify field name data type not null ;

(2) Delete constraints:

alter table table name modify field name data type;

2. Primary key constraint (primary key)

-- 演示主键约束:非空且唯一
insert into emp(id,ename,joindate,salary,bonus) values(1,"张三",'1999-11-11',8800,5000);

insert into emp(id,ename,joindate,salary,bonus) values(1,"张三",'1999-11-11',8800,5000);-- 报错,id唯一
insert into emp(id,ename,joindate,salary,bonus) values(null,"张三",'1999-11-11',8800,5000);-- 报错 null

insert into emp(id,ename,joindate,salary,bonus) values(2,"李四",'1999-11-11',8800,5000);

(1) Add constraints:

-- Add a primary key constraint when creating the table

create table table name (

        column name data type primary key [auto_increment] ,

....

) ;

create table table name (

        column name datatype ,

        [constraint] [constraint name] primary key (column name)

....

) ;

-- Add primary key constraints after the table is built

alter table table name add  primary key (field name) ;

(2) Delete constraints:

alter table table name drop primary key ; 

3. Default constraints (defaut)

-- 演示默认约束
insert into emp(id,ename,joindate,salary) values(3,"王五",'1999-11-11',8800);-- defaut 约束 不给任何值

insert into emp(id,ename,joindate,salary,bonus) values(4,"赵六",'1999-11-11',8800,null);-- null也算值

(1) Add constraints:

-- Add default constraints when creating a table

create table table name (

        column name data type default default value ,

....

) ;

-- Add default constraints after creating the table

alter table table name alter  column name set defaut default value ;

(2) Delete constraints:

alter table table name  arter  column name drop default ;

4. Automatic growth (auto_increment)

"auto_increment" available when column is numeric and unique

-- 演示自动增长:auto_increment:当列是数字类型且为唯一约束时
insert into emp(ename,joindate,salary,bonus) values("赵六",'1999-11-11',8800,null);-- null也算值
insert into emp(id,ename,joindate,salary,bonus) values(null,"赵六2",'1999-11-11',8800,null);
insert into emp(id,ename,joindate,salary,bonus) values(null,"赵六3",'1999-11-11',8800,null);

5. Foreign key constraints

  • Concept: Foreign keys are used to establish connections between data in two tables to ensure data consistency and integrity

/*
	外键约束:
		* 外键用来让两个表的数据之间建立链接,保证数据的一致性和完整性

	
	-- 创建表时添加外键约束
	CREATE TABLE 表名(
		 列名 数据类型,
		 …
		 [CONSTRAINT] [外键名称] FOREIGN KEY(外键列名) REFERENCES 主表(主表列名) 
	); 


	-- 建完表后添加外键约束
	ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段名称) REFERENCES 主表名称(主表列名称);


	-- 删除约束
	ALTER TABLE 表名 DROP FOREIGN KEY 外键名称;
	
	
*/
-- 删除表
DROP TABLE IF EXISTS emp;
DROP TABLE IF EXISTS dept;


-- 部门表
CREATE TABLE dept(
	id int primary key auto_increment,
	dep_name varchar(20),
	addr varchar(20)
);
-- 员工表 
CREATE TABLE emp(
	id int primary key auto_increment,
	name varchar(20),
	age int,
	dep_id int,

	-- 添加外键 dep_id,关联 dept 表的id主键
	CONSTRAINT fk_emp_dept FOREIGN KEY(dep_id) REFERENCES dept(id)
		
);
-- 添加 2 个部门
insert into dept(dep_name,addr) values
('研发部','广州'),('销售部', '深圳');

-- 添加员工,dep_id 表示员工所在的部门
INSERT INTO emp (NAME, age, dep_id) VALUES 
('张三', 20, 1),
('李四', 20, 1),
('王五', 20, 1),
('赵六', 20, 2),
('孙七', 22, 2),
('周八', 18, 2);

-- ------------------
select * from emp;
select * from dept;


-- 删除外键
alter table emp drop FOREIGN key fk_emp_dept;


-- 建完表后,添加外键
alter table emp add CONSTRAINT fk_emp_dept FOREIGN key(dep_id) REFERENCES dept(id);

5.1 Create employee table and department table

-- 删除表
DROP TABLE IF EXISTS emp;
DROP TABLE IF EXISTS dept;


-- 部门表
CREATE TABLE dept(
	id int primary key auto_increment,
	dep_name varchar(20),
	addr varchar(20)
);
-- 员工表 
CREATE TABLE emp(
	id int primary key auto_increment,
	name varchar(20),
	age int,
	dep_id int,

	-- 添加外键 dep_id,关联 dept 表的id主键
	CONSTRAINT fk_emp_dept FOREIGN KEY(dep_id) REFERENCES dept(id)
		
);

 5.2 Syntax

(1) Add constraints:

-- Add foreign key constraints when creating the table

create table table name (

        column name data type ,

        ....

[constraint] [foreign key name] foreign key (foreign key column name) references main table (main table column name)

-- 添加外键 dep_id,关联 dept 表的id主键,外键名称随便写,一般以fk_开头
	CONSTRAINT fk_emp_dept FOREIGN KEY(dep_id) REFERENCES dept(id)

) ;

-- Add foreign key constraints after the table is built

alter table table name add constraint foreign key name foreign key (foreign key field name) references main table name (main list name);

​-- 建完表后,添加外键
alter table emp add CONSTRAINT fk_emp_dept FOREIGN key(dep_id) REFERENCES dept(id);

(2) Delete constraints:

alter table table name drop  foreign key foreign key name ;  

-- 删除外键
alter table emp drop FOREIGN key fk_emp_dept;

Notice:

  • First create the master table and add the data in the master table , and then operate on the slave table ! ! !
  • When deleting, delete the slave table data first to delete the corresponding master table data ! ! !

Guess you like

Origin blog.csdn.net/weixin_48373085/article/details/128528737