当前位置:网站首页>Multi table operation - foreign key constraint
Multi table operation - foreign key constraint
2022-06-30 21:21:00 【Soup key TJ】
Catalog
Why foreign key constraints are required
When the data between tables is related
The role of foreign key constraints :
Add foreign key constraints when creating tables
Add foreign key constraints separately after creating the table
First create 2 surface , And use foreign key constraints
Why foreign key constraints are required
When the data between tables is related
- If there are no relevant data constraints , The accuracy of the data cannot be guaranteed
The role of foreign key constraints :
- Let tables relate to each other , So as to ensure the accuracy of the data
Foreign key constraints use
Add foreign key constraints when creating tables
- create table Table name (
- Name data type constraint
- .....
- constraint Foreign key name foreign key ( Foreign key column name of this table ) references Main table name ( Primary table primary key column name )
- )
Delete foreign key constraint
- alter table Table name drop foreign Foreign key name
Add foreign key constraints separately after creating the table
- alter table Table name add
- constraint Foreign key name foreign key ( Foreign key column name of this table ) references Main table name ( Primary key column name )
Examples demonstrate
First create 2 surface , And use foreign key constraints
CREATE TABLE user( id INT PRIMARY KEY auto_increment, name VARCHAR(40) NOT NULL ); INSERT INTO user VALUES (NULL,' Zhang San '),(NULL,' Li Si '); CREATE TABLE ouser( id INT PRIMARY KEY auto_increment, number VARCHAR(20) NOT NULL, uid INT, -- Foreign key column CONSTRAINT ou FOREIGN KEY (uid) REFERENCES user (id) ); INSERT INTO ouser VALUES (NULL,'hm1',1),(NULL,'hm2',1),(NULL,'hm3',2),(NULL,'hm4',2);
Conduct error test
-- Add one ouser But there is no corresponding user, Will fail to add INSERT INTO ouser VALUES (NULL,'hm5',3); -- Deleting Lisi user will fail DELETE FROM user WHERE name=' Li Si ';
Delete foreign key constraint
Add a foreign key constraint
-- Delete foreign key constraint ALTER TABLE ouser DROP FOREIGN KEY ou; -- Add a foreign key constraint ALTER TABLE ouser ADD CONSTRAINT ou FOREIGN KEY (uid) REFERENCES user(id);
边栏推荐
猜你喜欢
Icml2022 | utility theory of sequential decision making
MySQL advanced 3
报错FileSystemException: /datas/nodes/0/indices/gtTXk-hnTgKhAcm-8n60Jw/1/index/.es_temp_file:结构需要清理
go搭建服务器基础
多态在代码中的体现
Clickhouse native monitoring item, system table description
Understand what MySQL index push down (ICP) is in one article
大学生研究生毕业找工作,该选择哪个方向?
asp.net core JWT传递
凤凰架构——架构师的视角
随机推荐
《ClickHouse原理解析与应用实践》读书笔记(1)
多表操作-外键约束
防范未授权访问攻击的十项安全措施
侧睡哈哈哈哈
Clickhouse native monitoring item, system table description
升级kube出现unknown flag: --network-plugin
Apply for vector bus protocol color picture wallpaper hanging picture, very good!
变异系数法matlab代码[通俗易懂]
转:用实际行动赢得别人追随
Analysis and proposal on the "sour Fox" vulnerability attack weapon platform of the US National Security Agency
clickhouse原生監控項,系統錶描述
ncat详细介绍(转载)
开源实习经验分享:openEuler软件包加固测试
qsort函数和模拟实现qsort函数
Export El table as is to excel table
Upgrade Kube with unknown flag: --network plugin
Use the log server to output the topn of various Apache logs
[untitled]
Oprator-1 first acquaintance with oprator
qiao-npms:搜索npm包