MySQL-约束

约束是作用与表中字段上的规则,用于限制存储在表中的数据。保证数据库中数据的正确、有效性和完整性。

分类:

注意:约束是作用于表中的字段上的,可以在创建表/修改表的时候添加约束。

外键约束:

语法:

添加外键:

crate table 表名(

字段名 数据类型,

......

constraint\] \[外键名称\] foreign key (外键字段名) references 主表 (主表列名) ); 或: alter table add constraint 外键名称 foreign key (外键字段名)references, 主表(主表列名); 删除外键: alter table 表名 drop foreign key 外键名称; 删除/更新行为: ![](https://file.jishuzhan.net/article/1704356860305346561/b26a337c94e44bf9be815e8c7969b911.png) alter table 表名 add constraint 外键名称 foreign key (外键字段) references 主表名(主表字段名) on update (cascade) on delete (cascade);

相关推荐
fzb5QsS1p2 小时前
MySQL 事务的二阶段提交是什么?
数据库·mysql
清风徐来QCQ5 小时前
Lombok/SSM/devTools
数据库
LaughingZhu5 小时前
Product Hunt 每日热榜 | 2026-04-05
前端·数据库·人工智能·经验分享·神经网络
2601_949814696 小时前
使用mysql报Communications link failure异常解决
数据库·mysql
搜佛说6 小时前
02-第2章-核心概念与架构
数据库·物联网·微服务·架构·边缘计算·iot
#六脉神剑7 小时前
MySQL参数调优:十个关键参数助力数据库性能数倍提升
运维·mysql
C'ᴇsᴛ.小琳 ℡7 小时前
高性能NoSQL
数据库·nosql
i220818 Faiz Ul8 小时前
动漫商城|基于springboot + vue动漫商城系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·动漫商城系统
iNgs IMAC8 小时前
redis 使用
数据库·redis·缓存
bearpping8 小时前
MySQL压缩版安装详细图解
android·mysql·adb