当前位置:网站首页>MySQL constraint
MySQL constraint
2022-07-16 06:01:00 【mintsolace】
constraint
Rules that act on fields in a table , Users restrict data stored in tables
Purpose : Ensure that the data in the database is correct , Effectiveness and integrity .
Constraints act on fields in a table , You can create a table / Add constraints when modifying tables .
classification :
| constraint | describe | keyword |
|---|---|---|
| Non empty constraint | Limit the data of this field to null | NOT NULL |
| Unique constraint | Ensure that all data in this field is unique , Not repeated | UNIQUE |
| Primary key constraint | A primary key is the unique identifier of a row of data , It is required to be non empty and unique | PRIMARY KEY |
| Default constraint | When saving data , If the value of this field is not specified , The default value | DEFAULT |
| Check constraint (8.016 After the version ) | Ensure that the field value meets a certain condition | CHECK |
| Foreign key constraints | Used to establish a connection between the data of two tables , Ensure data consistency and integrity | FOREIGN KEY |
Case study : According to the demand , Complete the creation of table structure
| Field name | Field meaning | Field type | constraint condition | Constraint keywords |
|---|---|---|---|---|
| id | ID Unique identification | int | Primary key , And grow automatically | PRIMARY KEY, AUTO_INCREMENT |
| name | full name | varchar(10) | Not empty , And only | NOT NULL,UNIQUE |
| age | Age | int | Greater than 0, And less than or equal to 120 | CHECK |
| status | state | char(1) | If this value is not specified , The default is 1 | DEFAULT |
| gender | Gender | char(1) | nothing |
create table user(
id int primary key auto_increment comment 'ID Unique identification ',
name varchar(10) not null unique comment ' full name ',
age int check (age>0 && age<=120) comment ' Age ',
status char(1) default '1' comment ' state ',
gender char(1) comment ' Gender '
)comment ' User table ';
Foreign key constraints
Make a connection between the data of two tables , Ensure data consistency and integrity
Add foreign keys の grammar :
Create table Table name (
Field name data type ,
...
[Constraint] [ Name of the foreign key ] Foreign Key( Foreign key field name ) References Main table ( Name of main table )
);
or
Alter Table Table name Add Constraint Name of the foreign key Foreign Key( Foreign key field name ) References Main table ( Name of main table );
Delete foreign key の grammar
Alter Table Table name Drop Foreign Key Name of the foreign key ;
Foreign key deletion / Update behavior
| Behavior | explain |
|---|---|
| NO ACTION | When deleting in the parent table / When updating the corresponding record , First, check whether the record has a corresponding foreign key , If there is, it is not allowed to delete / to update .( And RESTRICT Agreement ) |
| RESTRICT | When deleting in the parent table / When updating the corresponding record , First, check whether the record has a corresponding foreign key , If there is, it is not allowed to delete / to update .( And NO ACTION Agreement ) |
| CASCADE | When deleting in the parent table / When updating the corresponding record , First, check whether the record has a corresponding foreign key , If there is , Then delete / Update the record of the foreign key in the sub table |
| SET NULL | When deleting the corresponding record in the parent table , First, check whether the record has a corresponding foreign key , If yes, set the foreign key value in the sub table to null( This requires that the foreign key be allowed to take null) |
| SET DEFAULT | When the parent table changes , The sub table sets the foreign key column to a default value (Innodb I won't support it ) |
边栏推荐
- ES6 -- object
- Cross domain exceptions where the admin system is nested in a third-party system
- 关于 Visual Studio 2022的安装与使用
- 内网渗透笔记——:)一个笑脸
- BUUCTF 荷兰宽带数据泄露
- 2. Memory leak and memory overflow
- REM layout
- Network security emergency response - common tools
- 开源实习 | Compiler SIG 实习任务正式发布,欢迎报名申请!
- 基础设施 NFTScan 正式发布 Solana 网络 NFT 浏览器
猜你喜欢

BUUCTF 来首歌吧

Notes on network communication security -- static routing and experiment

内网渗透笔记——注册表自启动与msi提权

41.js -- closure

网络通信安全部分笔记——静态路由及实验

網絡安全應急響應-電子數據取證技術

Summer precipitation Web Learning -- PHP Foundation

欧拉Talk | 开发者社区体验坦白局7月14日19:30约起

Mysql 主从服务器配置实验 centos7

Intranet penetration notes - Registry self startup and MSI rights lifting
随机推荐
内存取证-Volatility安装使用以及一些CTF比赛题目
ES6 -- let and Const
MSF利用永恒之蓝渗透win2003
2. Memory leak and memory overflow
Intranet penetration notes - layer 2 Discovery
sniffer Pro对ARP协议的分析、捕获与模拟攻击
How is the advertising model with 100 billion parameters refined?
编译原理-语法分析器设计
网络通信安全部分笔记一
Network security emergency response terminal detection and response technology
手摸手教学-利用原生POI对excel的导入导出以及阿里的easyexcel的基本操作
ES6 -- class
unity实验-控制游戏物体运动
内网渗透笔记——:)一个笑脸
基础设施 NFTScan 正式发布 Solana 网络 NFT 浏览器
Find a value of tree data
Flag under buuctf mask
Steps for JS parsing engine to execute JS code
Intranet penetration notes - Three Layer Discovery and four layer discovery
ERR_ PNPM_ PEER_ DEP_ ISSUES Unmet peer dependencies