MySQL basics six [constraints]

1. What is a constraint

In order to ensure the consistency and integrity of the data, the SQL specification imposes additional conditions on table data in a constrained manner.

• Constraints are mandatory at the table level

• You can specify constraints when you create the table (via the CREATE TABLE statement), or after the table is created (via the ALTER TABLE statement)

Two, common constraints

There are the following six constraints:

-NOT NULL non-empty constraint, which stipulates that a certain field cannot be empty

-UNIQUE unique constraint, which stipulates that a certain field is unique in the entire table

-PRIMARY KEY primary key (non-empty and unique)

– FOREIGN KEY foreign key

– CHECK check constraints

– DEFAULT default value

Note: MySQL does not support check constraints, but check constraints can be used without any effect.

According to the constraints of the constraint data column, the constraints can be divided into:

– Single column constraint: each constraint only constrains one column

– Multi-column constraints: Each constraint can constrain multiple columns of data

•According to the scope of constraints, constraints can be divided into:

-Column-level constraints can only be applied to one column, following the column definition

– Table-level constraints can act on multiple columns, not together with the columns, but separately defined

常见约束
一种限制,用于限制表中的数据,为了保证表中的数据的准确和可靠性
CREATE TABLE 表名(
字段名 字段类型 约束
)

分类
六大约束
NOT NULL 非空约束 保证该字段的值不能为空
比如姓名 学号

DEFAULT 默认约束 用于保证该字段有默认值
比如性别

PRIMARY KEY 主键约束 用于保证该字段的值具有非空 唯一性
比如学号

UNIQUE :唯一,用于保证该字段的值具有唯一性,可以为空
比如座位号 也可以为空

CHECK 检查约束 MySQL中不支持,虽然语法没错
性别只能是男或者女 那么添加的数据只能是二者之一 不然无法添加

FOREIGN KEY 外键约束 用于限制两个表的关系,用于保证该字段的值必须来自于
主表关联列的值

比如
表一:学生信息表里有一个专业列
表二:专业信息表 假如只有两个专业 1 Java 2 h5
那么表一中的专业列号只能是1或者2,来自于专业信息表
这个约束就叫做外键约束,一般添加在从表(学生信息表)中,
引用主表(专业信息表)中某列的值。

比如:专业编号,员工表的部门编号,工种编号,都可添加外键约束。

添加约束时机:
1.创建表时
2.修改表时

约束的添加分类:
1.列级约束
创建表时,写在:
字段名 字段类型 列级约束;
六大约束语法上都没问题,但外键约束无效果

2.表级约束
在写完列后,最后加上:
表级约束
不支持非空和默认

 

Three, add column-level constraints when creating a table

1.添加列级约束
CREATE TABLE stuinfo(
id INT PRIMARY KEY,
stuName VARCHAR(20) NOT NULL,
gender CHAR(1) CHECK(gender = '男' OR gender = '女'),
seat INT UNIQUE,
age INT DEFAULT 18,
majorId INT REFERENCES major(id)
);

CREATE TABLE major(
id INT PRIMARY KEY,
majorName VARCHAR(20)
);

SELECT * FROM stuinfo;

语法:直接在字段名和类型后面追加约束类型即可。
只支持默认 非空 主键和唯一

 

Four, add table-level constraints when creating a table

2.添加表级约束
DROP TABLE IF EXISTS stuinfo;

VARCHAR:可变长 CHAR:定长
CREATE TABLE stuinfo(
id INT,
stuname VARCHAR(20),
gender CHAR(1),
seat INT,
age INT,
majorid INT,

CONSTRAINT pk PRIMARY KEY(id),#主键
CONSTRAINT uq UNIQUE(seat),
CONSTRAINT ck CHECK(gender = '男' OR gender = '女'),
CONSTRAINT fk_stuinfo_major FOREIGN KEY(majorid) REFERENCES major(id)
)

SHOW INDEX FROM stuinfo;

语法:
【constraint 约束名】

通用写法:
CREATE TABLE IF EXISTS stuinfo(
id INT PRIMARY KEY,
stuName VARCHAR(20) NOT NULL,
gender CHAR(1),
seat INT UNIQUE,
age INT DEFAULT 18,
majorId INT,
CONSTRAINT fk_stuinfo_major FOREIGN KEY(majorId) REFERENCES major(id)
)

 

Five, primary key and unique comparison

1. Uniqueness can be guaranteed

2. The primary key is not allowed to be empty

3. There can be at most one primary key in a table, and there can be multiple unique ones

4. All combinations are allowed, but not recommended

For example, a student table contains student ID, which is the primary key.

Each outstanding student can be assigned a one-to-one tutor.

There is also a column for the mentor ID

Students with tutors have tutor IDs, which are different from each other;

Of course, some students can also have no tutor ID

 

Six, foreign keys

1. It is required to set the foreign key relationship from the table

2. The type of the foreign key column of the slave table is consistent with the type of the associated column of the main table.

3. The associated column of the main table must be a key, generally the primary key or unique

4. When inserting data, insert the master table first, and then insert the slave table

5. When deleting data, delete the secondary table first, and then delete the primary table

Guess you like

Origin blog.csdn.net/Kukeoo/article/details/114275183