当前位置:网站首页>[MySQL learning notes 18] constraints
[MySQL learning notes 18] constraints
2022-06-21 17:25:00 【yqs_ two hundred and eighty-one million eight hundred and seven】
Common constraints

practice
create table stu(
id int primary key auto_increment,
name varchar(10) not null unique,
age int check (age>=0 and age<=120),
status char(1) default 1,
gender char(1) default ' male '
)
# Create department table
create table dept(
id int primary key auto_increment,
name varchar(20) not null
);
# insert data
insert into dept(id,name) value (1,' R & D department '),(2,' The Marketing Department '),(3,' Finance Department '),(4,' The sales department '),(5,' General Manager's Office ');
# Create an employee table
create table emp(
id int primary key auto_increment,
age int,
job varchar(20),
salary int,
entry_date date,
manager_id int,
dept_id int,
constraint fk_emp_dept_id foreign key (dept_id) references dept(id)
);
# Delete foreign key
alter table emp drop foreign key fk_emp_dept_id;
# Add foreign keys
alter table emp add constraint foreign key (dept_id) references dept(id);
Foreign key delete / Update behavior
| name | explain |
|---|---|
| NO ACTION/RESTRICT( Default behavior ) | Reject the operation when the updated record is deleted and referenced by a foreign key |
| CASCADE | When the deleted updated record is referenced by a foreign key , At the same time, delete and update the record corresponding to the foreign key |
| SET NULL | When the deleted updated record is referenced by a foreign key , Set the referenced record foreign key value to null |
| SET DEFAULT | In the same way as above , Set to default (innodb Engine not supported ) |
grammar
alter table Table name add constraint Foreign key name foreign key ( Foreign key field ) references Table name ( Field name ) on update cascade on delete set null;
边栏推荐
猜你喜欢

Beaucoup de sociétés de logiciels sont en fait des "blagues"

海外new things | 美国人工智能初创「Zoovu」新一轮融资1.69亿美元,为消费者优化线上的“产品发现”体验

变量与指针

Concept drift in machine learning (Apria)

Three color mark removal method

Still using xshell? Try this cool SSH terminal tool, which is very powerful!

Elegant request retry using guzzle Middleware

Postman basic operations

阿里云服务器+宝塔面板+无域名部署web项目

Notice on the third national operation research / data, model and decision-making course teaching seminar in 2022
随机推荐
NLog自定义Target之MQTT
很多软件公司,其实都是“笑话”
Notice on the third national operation research / data, model and decision-making course teaching seminar in 2022
Oracle JDBC 驱动
exness:美国通货膨胀影响太大,美联储大佬纷纷表态
Fidder tool usage notes
阿里云服务器+宝塔面板+无域名部署web项目
Pytest framework
How to open an account for futures agricultural products? How much is the handling charge?
[learn FPGA programming from scratch -38]: Advanced - syntax - functions and tasks
2022年第三届全国运筹学/数据、模型与决策课程教学研讨会通知
一体化伺服电机与施耐德PLC TM241CEC24T在Canopen协议下的应用
今年的 618 不行了?
疫情数据对应的大陆和全球的矢量数据下载,基于geojson转shp
3M互助智能合约系统开发搭建技术
Unittest framework
Many software companies are actually "jokes"
Overseas new things | software developer "dynaboard" seed round raised US $6.6 million to develop low code platform to connect design, products and developers
MATLAB实现的基于对称TSP问题研究
智能制造的下一站:云原生+边缘计算双轮驱动