MySQL - table integrity constraints (foreign key constraints)

1. What are integrity constraints?
In order to prevent non-conforming data from being stored in the database, MySQL provides a mechanism to check whether the data in the database meets the specified conditions when users insert, modify and delete the data, so as to ensure the accuracy and consistency of the data in the database. This mechanism is integrity constraint.
2. What are foreign key constraints?
FOREIGN KEY constraint (FK) is used to realize the referential integrity of database tables. The FOREIGN KEY constraint can closely combine the two tables, especially for the cascade operation of modification or deletion, which will ensure the data integrity.
3. What is a foreign key?
Foreign key means that the value of a field in a table depends on the value of a field in another table, and the dependent field must have primary key constraints or unique constraints
4. What is a parent table?
The dependent table is usually called the parent table or the primary table
5. What is a sub table?
Tables that set foreign key constraints are called child tables or slave tables

For example:
For example, if you want to express the relationship between students and classes, you must first have two tables: student table and class table, and then a field in the student table is stu_clazz (this field represents the class of the student), and the value range of this field is determined by the primary key CLA in the class table_ The value of the no field (which represents the class number) is determined. Then the class table is the master table, the student table is the slave table, and stu_ The clazz field is the foreign key of the student table. Through stu_ The clazz field establishes the relationship between the student table and the class table.

Primary table (parent table): class table - class number - primary key
Slave table (sub table): student table - class number - foreign key

Error case

6. Create parent table first: class table

create table t_class(
	cno int(4) primary key auto_increment,
	cname varchar(5) not null,
	room char(4)

-- insert data	
insert into t_class values(null,'c1','1001'),(null,'c2','1002'),(null,'c3','1003');

7. Create sub table: student table

create table t_student(
	sno int(6) primary key auto_increment,
	sname varchar(5) not null,
	classno int(4)
)
-- insert data
insert into t_student values (null,'Zhang San',1),(null,'Li Si',1),(null,'Wang Wu',2);

Requirement 1:
Add a student whose corresponding class number is 4

insert into t_student values(null,'curry',3);

Operation result: successfully added

There is a problem with requirement 1: because your current foreign key constraints are not added with grammar, now you just logically think that the class number is a foreign key and are not defined from grammar
Solution: add a foreign key constraint
Note: foreign key constraints have only table level constraints and no column level constraints:

Correct case

Create the parent table first; Class table

create table t_class(
	cno int(4) primary key auto_increment,
	cname varchar(5) not null,
	room char(4)

)

Create sub table, student table (add foreign key constraint to create table)

create table t_student(
	sno int(6) primary key auto_increment,
	sname varchar(5) not null,
	classno int(4),
	constraint fk_stu_classno foreign key (classno) references t_class (cno)
)

Or create a sub table, student table (add foreign key constraints after creating the table)

create table t_student(
        sno int(6) primary key auto_increment, 
        sname varchar(5) not null, 
        classno int(4)
);
-- Add a foreign key constraint after creating the table:
alter table t_student add constraint fk_stu_classno foreign key (classno) references t_class (cno)

When deleting a table, first delete the primary table, and then delete the secondary table
Requirement 1: add a student whose corresponding class number is 4

insert into t_student values(null,'curry',4);

Error message: 1452 - cannot add or update a child row: a foreign key constraint failures (mytestdb.t_student, constraint fk_stu_classno foreign key (classno) references t_class (CNO))

Requirement 2: students with No. 3 in the student table can be deleted successfully because the data does not exist in the sub table

delete from t_student where sno=3;

Requirement 3: delete the information with class number 3 in the class table

delete from t_class where cno=1;

Error message: 1451 - cannot delete or update a parent row: a foreign key constraint failures (mytestdb.t_student, constraint fk_stu_classno foreign key (classno) references t_class (CNO))

Keywords: Database MySQL

Added by lost305 on Fri, 22 Oct 2021 00:50:39 +0300