2024 MySQL Study Guide (4), explore the MySQL database and grasp future data management trends

Preface

Continuing from the previous article:

2024 MySQL Study Guide (1)

2024 MySQL Study Guide (2)

2024 MySQL Study Guide (3)

9. The concept of constraints

约束是作用于表中列上的规则,用于限制加入表的数据。For example, the column used as the primary key must be non-null and unique, otherwise the data cannot be distinguished. The existence of constraints ensures the correctness, validity and integrity of the data in the database. So constraints are very important in database design.

10. Classification of constraints

As mentioned earlier, the SQL standard divides constraints into six categories, namely non-null constraints, unique constraints, primary key constraints, check constraints, default constraints and foreign key constraints. When adding constraints, we only need to add keywords in SQL to limit them. data in the table.

constraint type Function
NOT NULL constraintNOT NULL Ensure that all data in the column cannot have null values
Unique constraint UNIQUE Ensure that all data in the column is distinct
PRIMARY KEY constraint The primary key is the unique identifier of a row of data and must be non-empty and unique.
Check constraints CHECK Ensure that the values ​​in the column meet a certain condition
Default constraintDEFAULT When saving data, if no value is specified, the default value is used.
Foreign key constraintsFOREIGN KEY Foreign keys are used to establish links between data in two tables to ensure data consistency and integrity.

11. Non-empty constraints

Purpose: Ensure that all data in the column cannot have null values

Add constraints:

CREATE TABLE 表名(
	列名 数据类型 NOT NULL,);

After creating the table, add a non-null constraint:

ALTER TABLE 表名 MODIFY 字段名 数据类型 NOT NULL;

Remove constraints:

ALTER TABLE 表名 MODIFY 字段名 数据类型;

12. Unique Constraints

Purpose: Ensure that all data in the column are distinct

Add constraints:

CREATE TABLE 表名(
	列名 数据类型 UNIQUE [AUTO_INCREMENT],
	-- AUTO_INCREMENT: 当不指定值时自动增长);

CREATE TABLE 表名(
	列名 数据类型,[CONSTRAINT] [约束名称] UNIQUE(列名)
);

After creating the table, add only constraints:

ALTER TABLE 表名 MODIFY 字段名 数据类型 UNIQUE;

Remove constraints:

ALTER TABLE 表名 DROP INDEX 字段名;

13. Primary key constraints

Purpose: The primary key is the unique identifier of a row of data and must be non-empty and unique.

Add constraints:

CREATE TABLE 表名(
	列名 数据类型 PRIMARY KEY [AUTO_INCREMENT],);

CREATE TABLE 表名(
	列名 数据类型,
	[CONSTRAINT] [约束名称] PRIMARY KEY(列名)
);

After creating the table, add primary key constraints:

ALTER TABLE 表名 ADD PRIMARY KEY(字段名);

Remove constraints:

ALTER TABLE 表名 DROP PRIMARY KEY;

14. Default constraints

Purpose: When saving data, if no value is specified, the default value will be used.

Add constraints:

CREATE TABLE 表名(
	列名 数据类型 DEFAULT 默认值,);

After creating the table, add default constraints:

ALTER TABLE 表名 ALTER 列名 SET DEFAULT 默认值;

Remove constraints:

ALTER TABLE 表名 ALTER 列名 DROP DEFAULT;

15. Foreign key constraints

当我们添加了外键以后,就在数据库层面建立了两张表的关系。

Purpose: Foreign keys are used to establish links between data in two tables to ensure data consistency and integrity.

Add constraints:

CREATE TABLE 表名(
	列名 数据类型,[CONSTRAINT] [外键名称] FOREIGN KEY(外键列名) REFERENCES 主表(主表列名)
);

After creating the table, add foreign key constraints:

ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段名称) REFERENCES 主表名称(主表列名称);

Remove constraints:

ALTER TABLE 表名 DROP FOREIGN KEY 外键名称;

16. Constraint case exercises

First, we use the following case to practice constraints:

-- 删除stu表
drop table if exists stu;
-- 创建stu表
CREATE TABLE stu (
id int primary key, -- 编号 主键
name varchar(10)  not null unique, -- 姓名 非空,唯一
age int not null, -- 年龄 非空
gender varchar(5) not null, -- 性别 非空
math double(5,2) not null, -- 数学成绩  非空
english double(5,2) default 0 -- 英语成绩 默认为0
);

Insert image description here

To verify the primary key constraint , which is non-empty and unique, first add a piece of data:

insert into stu(id,name,age,gender,math,english) values(1,'小张',23,'男',66,78);

When adding the second piece of data, try to add the id as a null value:

insert into stu(id,name,age,gender,math,english) values (null,'小李',20,'女',98,87);

Insert image description here

Try adding id as a duplicate value:

insert into stu(id,name,age,gender,math,english) values (1,'小陈',55,'男',56,77);

Insert image description hereWe have verified the primary key constraints and when we add invalid data, the addition fails.

Verify non-null constraints:

When we add the following data:

insert into stu(id,name,age,gender,math,english) values (2,NULL,20,'女',76,65);

Insert image description here
Verify unique constraints:

When we add the following data:

insert into stu(id,name,age,gender,math,english) values 
(5,'小张',20,'男',86,NULL);

Insert image description hereVerify default constraints:

When we add the following data:

insert into stu(id,name,age,gender,math) values (6,'小赵',23,'男',99);

Insert image description here

Verify foreign key constraints:

We use the following case to verify foreign key constraints:

-- 删除表
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)
);

At this time, we have added a foreign key to the employee table, which is equivalent to establishing a relationship between the two tables at the database level. At this time, if there is employee a in the employee table and he belongs to department No. 1, then we want to delete the department table The deletion of department No. 1 will fail because employee a belongs to department No. 1. At this time, the two tables have established a relationship.

Insert image description here

adding data:

-- 添加 2 个部门
insert into dept(dep_name,addr) values
('研发部','西安'),('销售部', '成都');
-- 添加员工,dep_id 表示员工所在的部门
INSERT INTO emp (NAME, age, dep_id) VALUES
('张三', 20, 1),
('李四', 20, 2);

At this time, when I wanted to delete the sales department, I found that the deletion failed.

Insert image description here

Guess you like

Origin blog.csdn.net/zhangxia_/article/details/135364854