1. Referential Integrity Referential integrity refers to the design between multiple tables, mainly using foreign key constraints. 1. One-to-many Related key sentences: -- Association (1 to N) create table customer( id int PRIMARY KEY auto_increment, name varchar (20) not null, address varchar (20) not null ); create table orders( order_num varchar(20) PRIMARY KEY, price FLOAT not NULL, customer_id int, -- foreign key constraint cus_ord_fk foreign key (customer_id) REFERENCES customer(id) ); insert into customer(name,adress) values("zs","北京"); insert into customer(name,adress) values("ls","上海"); SELECT * from customer; INSERT INTO orders values("010",30.5,1); INSERT INTO orders values("011",60.5,2); INSERT INTO orders values("012",120.5,1); SELECT * from orders; notice: constraint: means constraint. foreign key: foreign key. references: Create a foreign key constraint named customer_id_fk, where the foreign key is customer_id and the reference is the id column in the customers table. Extensions: Deletion and update strategy of word table:
2. Many-to-Many Create a student-teacher relationship table where a student can have multiple teachers and a teacher can have multiple students -- Many-to-many relationship -- Create a teacher table CREATE TABLE teacher ( id INT, NAME VARCHAR (20) NOT NULL, gender CHAR (2) NOT NULL, PRIMARY KEY (id) ); -- Create a student table CREATE TABLE student ( id INT, NAME VARCHAR (20) NOT NULL, age INT NOT NULL ); ALTER TABLE student ADD PRIMARY KEY (id); -- The third relation table CREATE TABLE tch_std ( teacher_id INT, student_id INT, CONSTRAINT fk_teacher FOREIGN KEY (teacher_id) REFERENCES teacher (id), CONSTRAINT fk_student FOREIGN KEY (student_id) REFERENCES teacher (id) ); DELETE FROM teacher WHERE id = 3; SELECT * FROM teacher; SELECT DISTINCT * FROM tch_std; Attention: Many-to-many is three tables, the third table creates a foreign key constraint corresponding to the id in the first two tables 3. One-to-one Two forms: 1) Add a unique constraint to the foreign key in the IdCard table according to the foreign key association -- One-to-one -- Create the users table CREATE TABLE users ( id INT, NAME VARCHAR (20) NOT NULL, gender CHAR (2) NOT NULL, PRIMARY KEY (id) ); -- Create the card table CREATE TABLE card ( id INT, address VARCHAR (100) NOT NULL, user_id INT UNIQUE, constraint users_id_fk foreign key(user_id) references users(id) ); ALTER TABLE card ADD PRIMARY KEY (id); This is method 1, adding a unique constraint unique to the foreign key The above is the detailed explanation and integration of referential integrity in SQL introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: SpringBoot integrates Activiti7 implementation code
>>: jQuery achieves seamless scrolling of tables
The cause is that the process opens a number of f...
The Docker daemon uses HTTP_PROXY , HTTPS_PROXY ,...
Table of contents 1. Background 2. Operation step...
Preface Some of the earlier codes on Github may r...
Table of contents What is Docker Compose Requirem...
First configure the project artifacts Configuring...
Install ssh tool 1. Open the terminal and type th...
With the rise of mobile terminals such as iPad, p...
The specific upgrade script is as follows: Dynami...
Checkboxes are very common on web pages. Whether ...
When applying docker containers, we often mount t...
Scenario Description In a certain system, the fun...
Sometimes you will see English commas ",&quo...
The GtkTreeView component is an advanced componen...
Preface Index Condition Pushdown (ICP) is a new f...