当前位置:网站首页>4. Data integrity
4. Data integrity
2022-07-26 07:17:00 【Piglet vs Hengge】
Catalog
1.1、 Primary key constraint (primary key)
1.2、 Unique constraint (unique)
1.3、 Auto grow Columns (auto_increment)
(3) Date and time numerical type
2.2、 Non empty constraint :NOT NULL
2.3、 Default constraint :default
03、 Citation integrity ( Referential integrity )
04、 The relationship between tables
effect : Ensure that the data entered by the user is saved correctly in the database .
Ensure data integrity = Add constraints to the table when creating
Classification of integrity :
(1) Entity integrity
(2) Domain integrity
(3) Citation integrity
01、 Entity integrity
Entity : That is, a row in the table ( A record ) Represents an entity (entity)
The role of entity integrity : Identify that each row of data does not repeat
Constraint type : Primary key constraint (primary key)、 Unique constraint (unique)、 Auto grow Columns (auto_increment)
1.1、 Primary key constraint (primary key)
notes : There should be a primary key in each table
characteristic : Data uniqueness , And cannot be null
(1) The first way to add :
# The first way to add
CREATE TABLE student1(
`id` INT PRIMARY KEY,
`name` VARCHAR(50)
``
)
--> Display table structure
mysql> desc student1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+(2) The second way to add : The advantage of this way is , You can create a union primary key
# The second way to add : The advantage of this way is , You can create a union primary key
#(1)
CREATE TABLE student2(
`id` INT,
`name` VARCHAR(50),
`classId` INT,
PRIMARY KEY(id,classId)
);
--> Display table structure
mysql> desc student2;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | YES | | NULL | |
| classId | int | NO | PRI | NULL | |
+---------+-------------+------+-----+---------+-------+(3) The third way to add :
# The third way to add
CREATE TABLE student3(
`id` INT,
`name` VARCHAR(50)
);
ALTER TABLE student3 ADD PRIMARY KEY(id);
--> Display table structure
mysql> desc student3;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+1.2、 Unique constraint (unique)
characteristic : Data cannot be repeated
CREATE TABLE student4(
`id` INT PRIMARY KEY,
`name` VARCHAR(50) UNIQUE
)
--> Display table structure
mysql> desc student4;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+1.3、 Auto grow Columns (auto_increment)
Be careful : Add an auto growing number to the primary key , Columns can only be of integer type
# Auto grow Columns :auto_increment
CREATE TABLE student5(
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(50)
)
--> final result :
mysql> SELECT * FROM student5;
+----+----------+
| id | NAME |
+----+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
+----+----------+02、 Domain integrity
The role of domain integrity : Limit the correct data of this unit , Do not compare other cells in this column
The field represents the current cell
Domain integrity constraints :
(1) data type : value type 、 The date type 、 String type
(2) Non empty constraint (not null)
(3) Default constraint (default)
2.1、 data type
(1) value type
(2) String type
(3) Date and time numerical type
2.2、 Non empty constraint :NOT NULL
# Non empty constraint :not null
CREATE TABLE student6(
`Id` INT PRIMARY KEY,
`Name` VARCHAR(50) NOT NULL,
`Sex` VARCHAR(10)
);
INSERT INTO student6 VALUES(1,'tom',NULL);
--> Display table structure
mysql> SELECT * FROM student6;
+----+------+------+
| Id | NAME | Sex |
+----+------+------+
| 1 | tom | NULL |
+----+------+------+2.3、 Default constraint :default
# Default constraint :default
CREATE TABLE student7(
`Id` INT PRIMARY KEY,
`Name` VARCHAR(50) NOT NULL,
`Sex` VARCHAR(10) DEFAULT ' male ' # The default value is set to ' male '
);
INSERT INTO student7 VALUES(1,'tom',' Woman ');
INSERT INTO student7 VALUES(2,'jerry',DEFAULT);
--> Display table structure
mysql> SELECT * FROM student7;
+----+-------+------+
| Id | NAME | Sex |
+----+-------+------+
| 1 | tom | Woman |
| 2 | jerry | male |
+----+-------+------+03、 Citation integrity ( Referential integrity )
Foreign key constraints :foreign key
# Foreign key constraints :foreign key
CREATE DATABASE mydb;
CREATE TABLE student8(
sid INT PRIMARY KEY,
NAME VARCHAR(50) NOT NULL,
sex VARCHAR(10) DEFAULT ' male '
);
INSERT INTO student8
VALUES
(1001,' Zhang San ',' male '),
(1002,' Li Si ',' male '),
(1003,' Wang Wu ',' Woman ');
# Add foreign key mode one :
CREATE TABLE score1(
id INT,
score INT,
sid INT , -- The data type of the foreign key column must be consistent with that of the primary key
CONSTRAINT fk_score_sid FOREIGN KEY (sid) REFERENCES student8(sid)
);
# Add foreign key mode 2 :
CREATE TABLE score2(
id INT,
score INT,
sid INT -- The data type of the foreign key column must be consistent with that of the primary key
);
ALTER TABLE score2 ADD CONSTRAINT fk_stu_score FOREIGN KEY(sid) REFERENCES student8(sid);
--> Display table structure : Add foreign key mode one
mysql> DESC score1;
+-------+------+------+-----+---------+-------+
| FIELD | TYPE | NULL | KEY | DEFAULT | Extra |
+-------+------+------+-----+---------+-------+
| id | INT | YES | | NULL | |
| score | INT | YES | | NULL | |
| sid | INT | YES | MUL | NULL | |
+-------+------+------+-----+---------+-------+
--> Display table structure : Add foreign key mode 2
mysql> DESC score2;
+-------+------+------+-----+---------+-------+
| FIELD | TYPE | NULL | KEY | DEFAULT | Extra |
+-------+------+------+-----+---------+-------+
| id | INT | YES | | NULL | |
| score | INT | YES | | NULL | |
| sid | INT | YES | MUL | NULL | |
+-------+------+------+-----+---------+-------+
04、 The relationship between tables
1、 one-on-one
for example t_person Table and t_card surface , People and ID cards . This situation requires a master-slave relationship , Who is the main table , Who is from the table . People can have no ID card , But the ID card must have talents , So people are the main watch , And the ID card is from the table . There are two schemes for designing slave tables :
(1) stay t_card Add foreign key columns to the table ( relative t_user surface ), And add a unique constraint to the foreign key ;
(2) to t_card Add a foreign key constraint to the primary key of a table ( relative t_user surface ), namely t_card The primary key of a table is also a foreign key .
2、 One to many
The most common is one to many ! One to many and many to one , From what angle can we see this .t_user and t_section The relationship between , from t_user It's one to many , And from t_section From the perspective of many to one ! In this case, foreign keys are created in multiple parties !
3、 Many to many
for example t_stu and t_teacher surface , That is, a student can have more than one teacher , And a teacher can have more than one student . In this case, you usually need to create an intermediate table to handle many to many relationships . For example, create another table t_stu_tea surface , Give two foreign keys , A relatively t_stu Table foreign key , Another relative t_teacher Table foreign key .

边栏推荐
- 中国联通改造 Apache DolphinScheduler 资源中心,实现计费环境跨集群调用与数据脚本一站式访问
- Leetcode:749. isolate virus [Unicom component + priority queue + status representation]
- 基于C51实现led流水灯
- Opencv learn resize and crop
- 20220724 三角函数系的正交性
- 【QT】怎样获得QTableView和QTableWidget的行数和列数
- Anaconda installation tutorial - hands on installation
- Apache DolphinScheduler 2.X保姆级源码解析,中国移动工程师揭秘服务调度启动全流程
- NFT数字藏品系统开发:华为发布首款珍藏版数字藏品
- Uncover the mystery of cloud native data management: operation level
猜你喜欢

Drools (2): drools quick start

Heap parsing and heap sorting
![Rgb-t tracking - [dataset benchmark] gtot / rgbt210 / rgbt234 / vot-2019-2020 / laser / VTUAV](/img/10/40d02da10a6f6779635dc820c074c6.png)
Rgb-t tracking - [dataset benchmark] gtot / rgbt210 / rgbt234 / vot-2019-2020 / laser / VTUAV

7月消息,Glassnode数据显示,Deribit上ETH永续期货合约未平仓头寸刚刚达到一个月高点237,959,827美元。

20220725 自动控制原理中的补偿器

Drools(3):Drools基础语法(1)

Relevant configurations of pychart: change font style and size, change picture background, and change the font color of console output

DaemonSet

Leetcode 1184: distance between bus stops

Advanced Mathematics (Seventh Edition) Tongji University General exercises two person solution
随机推荐
Manifest merger failed with multiple errors, see logs
Leetcode:1898. maximum number of removable characters [if you want to delete some IDX from a pile of things, don't use pop]
MySql 中应该如何将多行数据转为多列数据
IDEA——使用@Slf4j打印日志
Opencv learn resize and crop
Moonbeam orbiters program: provides a new way for collectors to participate in moonbeam and Moonriver
How to expand and repartition the C disk?
20220725 compensator in automatic control principle
Do you want to restart the database to replace the license?
WPS or office compression of ppt
College degree sales career, from the third tier 4K to the first tier 20k+, I am very satisfied with myself
What are the basics of getting started with spot silver
[QT] how to obtain the number of rows and columns of qtableview and qtablewidget
Advanced Mathematics (Seventh Edition) Tongji University General exercises two person solution
Opencv learning basic functions
C51与MDK共存 Keil5安装教程
【QT】详解 *.pro、*.pri、*.prf、*.prl文件
How regular expressions write variables
如何对C盘进行扩容重新分区?
Leetcode:749. isolate virus [Unicom component + priority queue + status representation]