当前位置:网站首页>sql 外键约束【表关系绑定】
sql 外键约束【表关系绑定】
2022-07-31 05:15:00 【为今天而努力】
外键
外键必须是表中的一个字段但不一定是该表的主键,但要对应的必须是另一张表的主键,外键的主要作用就是啊要保持数据的完整性,定义外键后不允许删除在另外一张表中具有关联关系的行【一条数据】。
- 主表(父表):主键所在的表即为主表。
- 从表(子表):外键所在的表即为从表。
什么时候使用外键约束
当两张表之间有有关联时可以采用外键约束进行绑定。
示例:有两张表分别为 tb_book【书 】 、 tb_reader【阅读】 ,显然没有书就无法阅读,所以 tb_book 为主表,tb_reader 为从表
// 创建tb_book
CREATE TABLE tb_book(
id int primary key auto_increment,
book_name varchar(50));
// 创建tb_reader
CREATE TABLE tb_reader(
id int primary key auto_increment,
bookId int,
constraint fk_book_id foreign key(bookId) references tb_book(id));
insert into book (book_name) values('喀尔巴阡古堡');
// 正确操作
insert into reader (readPage) values(45);
insert into reader (bookId, readPage) values (1, 30);
// 错误操作
delete from book where id = 1;
insert into reader (bookId, readPage) values (2, 30);
使用外键时注意
- 一个表可以有一个或者多个外键。
- 外键对应的是参照完整性,一个表的外键可以为空值,如果不为空值,则每一个外键的值必须是另一个表中主键的某一个值,否则会提示如下错误:
insert into reader (bookId, readPage) values (30, 1);
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`test_db`.`reader`,
CONSTRAINT `fk_book_id` FOREIGN KEY (`bookId`) REFERENCES `book` (`id`))
- 如果删除在另一张表中具有关联关系的行【一条数据】则会出一下异常:
delete from book where id = 1;
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`test_db`.`reader`,
CONSTRAINT `fk_book_id` FOREIGN KEY (`bookId`) REFERENCES `book` (`id`))
如果创建时没有添加外键约束可以使用 alter table add 添加
alter table reader add constraint fk_book_id foreign key(bookNameId) references tb_book(id);
边栏推荐
- 【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了
- NFT与数字藏品到底有何区别?
- 【ubuntu20.04安装MySQL以及MySQL-workbench可视化工具】
- Build DVWA with phpstudy
- How MySQL - depots table?A look at will understand
- leetcode-每日一题1252. 奇数值单元格的数目(模拟优化)
- Detailed explanation of pointers in C language
- 通信原理——纠错编码 | 汉明码(海明码)手算详解
- Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?
- 【云原生】SQL(及存储过程)跑得太慢怎么办?
猜你喜欢

【windows】--- SQL Server 2008 超详细安装教程

leetcode-1833. 雪糕的最大数量(排序+贪心)

Using IIS10 to build an asp website in win11

工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息

js中的全局作用域与函数作用域

flutter 混合开发 module 依赖
![[uiautomation] Get WeChat friend list (stored in txt)](/img/26/1f3424c5998c52c6e10ced8529012a.png)
[uiautomation] Get WeChat friend list (stored in txt)

gin框架学习-JWT认证

为什么bash中的read要配合while才能读取/dev/stdin的内容

npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案
随机推荐
Install mysqldb in mac10.14
flutter 混合开发 module 依赖
sqlmap注入教程 常用指令
Eternal blue bug reappears
Volatility取证工具使用日记
mysql password modification method in Linux (pro-test available)
"limit" query in Oracle database
Error: Cannot find module ‘D:\Application\nodejs\node_modules\npm\bin\npm-cli.js‘
Error: Cannot find module 'D:\Application\nodejs\node_modules\npm\bin\npm-cli.js'
Three-party login using wallet Metamask based on web3.0
【云原生】SQL(及存储过程)跑得太慢怎么办?
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
数据库 | SQL增删改查基础语法
How MySQL - depots table?A look at will understand
腾讯云GPU桌面服务器驱动安装
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
使用ps | egrep时过滤排除掉egrep自身
腾讯云轻量服务器删除所有防火墙规则
Getting to know regular expressions
5 methods of MySQL paging query