Data integrity is divided into: entity integrity, domain integrity, and referential integrity. Referential Integrity: Referential integrity refers to the design between multiple tables, mainly using foreign key constraints. 1. Entity (row) integrity Entity integrity is achieved through the primary key of the table. Logical primary key: such as ID, does not represent actual business significance, but is only used to uniquely identify a record (recommended) For example: create table person2( id int primary key auto_increment, name varchar(100) ); 2. Domain (column) integrity Not null constraint: not null creat table person3( id int not null unique, name varchar(20) not null unique, gender varchar(20) not null, phone char(11) unique #Not allowed, ); Three: Referential Integrity 1. One-to-many Syntax input to create one-to-many: create table customers( id int, name varchar(20), address varchar(100), primary key(id) ); Create the Orders table: create table orders( id int, order_num int primary key, price float(4,2), customer_id int, constraint customer_ord_fk foreign key(customer_id) references customers(id) ); Note: constraint: constraint, foreign key: foreign key, references: references Create a foreign key constraint named customer_ord_fk, the foreign key refers to customer_id, which refers to the id column in the customers table Table operation creates one-to-many customers table orders table orders foreign key settings Create a foreign key constraint named customer_ord_fk with the foreign key field customer_id, which refers to the id column in the customers table. Extension: Four sub-table deletion and update strategies:
2. Many-to-many Create the teacher table: Create table teachers( id int primary key auto_increment, name varchar(20) , gender char(5), subject varchar(20) ); Create the student table: Create table students( id int primary key , name varchar(20), age int ); The third one is the association table: Create table teacher_student( id int, t_id int, s_id int, primary key(t_id,s_id) constraint teacher_id_fk foreign key(t_id) references students(id), constrains student_id_fk foreign key(s_id) references teachers(id) ); Table Operation Many-to-Many Create the teacher table: Create the student table: The third table: 3. One-to-one (1) Generally, you only need to design one table. (2) You can design two tables. Method 1: Create a field in the second table as a foreign key (set to unique) corresponding to the primary key in the first table; Two tables: unique is very important create table user( id int primary key auto_increment, name varchar(20), age int ); create table card( id int primary key auto_increment, num varchar(20) not null, address varchar(100) not null, user_id int unique, constraint user_card_fk foreign key(user_id) references user(id) ); The above is the detailed explanation and integration of MySQL data integrity 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:
|
<<: Teach you how to use webpack to package and compile TypeScript code
>>: Complete steps to set up automatic updates in CentOS 8
1. Install nginx in docker: It is very simple to ...
The installation tutorial of mysql 8.0.11 winx64 ...
This article takes the deployment of Spring boot ...
Click on the anchor link to scroll smoothly and a...
Let me first introduce to you that the node proce...
1. Development environment vue+vant 2. Computer s...
# Installation daemon configuration for Redis on ...
This article example shares the specific code of ...
Earlier we talked about how to make a square with...
Use examples to familiarize yourself with the mean...
Usage of time difference functions TIMESTAMPDIFF ...
Table of contents infer Case: Deepen your underst...
Separation of static and dynamic Dynamic requests...
Configuration Preface Project construction: built...
Table of contents 1. Operators Summarize 1. Opera...