当前位置: 首页 > news >正文

SQL学习笔记4

约束

1、约束

约束,是指作用在表中字段上的规则,用于限制字段输入的数据,使得表格式统一,数据内容正确。同一个字段的约束可以有多个

约束包括:

非空约束,限制表中的值不为null:not null

唯一约束:同一个字段的值不重复:unique

主键约束:一行数据的唯一标识,非空且唯一:primary key

默认约束:若字段的值为空,则使用默认值:default

检查约束:保证字段值符合某一个条件:check(条件)

约束的语法为:字段名 字段类型 约束1 约束2……[comment]

create table cusinfo(id int primary key auto_increment comment'序号',name varchar(20) not null comment '姓名',sex varchar(10) check(sex='男'||sex='女'||sex='未知') default '未知' comment '性别',age int check (age>=0&&age<=120)comment '年龄',birthday date  check ( birthday <= '2025-6-27')  comment '生日',phone varchar(20) check(length(phone)=11) unique comment '电话',address varchar(50) comment '地址'
)comment='用户信息表';insert into cusinfo values(null,'张三','男',18,'1999-01-01','13888888888','北京'),
(null,'李四','女',19,'1998-01-01','13888888889','上海'),
(null,'王五','未知',20,'1997-01-01','13888888890','广州'),
(null,'赵六','男',21,'1996-01-01','13888888891','深圳'),
(null,'孙七','女',22,'1995-01-01','13888888892','杭州'),
(null,'周八','男',23,'1994-01-01','13888888893','西安'),
(null,'吴九','女',24,'1993-01-01','13888888894','武汉'),
(null,'郑十','男',25,'1992-01-01','13888888895','南京');

2、外键约束(不常用)

两张表在数据库层面建立连接的约束,使得两张表的完整与统一,外键连接最常用的方法是在子表中创建一个子id,然后去连接父表的主键

添加外键:在创建表格时添加:create table 表名(

字段名 数据类型 ,

……

[constraint] [外键名称] foreign key (外键字段名) reference 父表 父表字段名

创建表后在表中增加一个外键: alter table 表名 [add constraint fk_子表名_父表名] foreign key (父表名_外键字段名 )references 父表名 (父表字段名)

create table serinfo(id int primary key  comment'序号',level varchar(10) comment'等级'
)comment '用户等级表';insert into serinfo values(1,'普通会员'),(2,'高级会员'),(3,'VIP会员');alter table cusinfo add column serinfo_id int comment '等级id';update cusinfo set serinfo_id=1 where id=1;
update cusinfo set serinfo_id=2 where id=2;
update cusinfo set serinfo_id=1 where id=3;
update cusinfo set serinfo_id=3 where id=4;
update cusinfo set serinfo_id=2 where id=5;
update cusinfo set serinfo_id=3 where id=6;
update cusinfo set serinfo_id=1 where id=7;
update cusinfo set serinfo_id=3 where id=8;
update cusinfo set serinfo_id=2 where id=9;alter table cusinfo add constraint fk_cusinfo_serinfo foreign key (serinfo_id) references serinfo(id);

删除外键

alter table 表名 drop foreign key 外键名称;

alter table cusinfo drop foreign key fk_cusinfo_serinfo;

外键的删除更新行为约束

在创建外键时可以语句后增加一个约束更新、删除的语法:alter table 表名 [add constraint fk_子表名_父表名] foreign key (父表名_外键字段名 )references 父表名 (父表字段名)on update 行为 on delete 行为

外键的更新/删除行为有:

当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除/更新:no action #默认存在

当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除/更新:

restrict #默认存在,与no action一致

将父表与子表级联:当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有,则也删除/更新外键在子表中的记录:cascadeas

当在父表中删除对应记录时,首先检查该记录是否有对应外键,如果有则设置子表中该外键值为null: set null

alter table cusinfo add constraint fk_cusinfo_serinfo foreign key (serinfo_id) references serinfo(id) on delete cascade on update cascade;alter table cusinfo add constraint fk_cusinfo_serinfo foreign key (serinfo_id) references serinfo(id) on delete set null;

http://www.lqws.cn/news/549883.html

相关文章:

  • MFC获取本机所有IP、局域网所有IP、本机和局域网可连接IP
  • 一起endpoint迷路的问题排查总结
  • 浅谈Apache HttpClient的相关配置和使用
  • git add 报错UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xaf in position 42
  • SOCKS 协议版本 5 (RFC 1928)
  • 【stm32】HAL库开发——CubeMX配置串口通讯(中断方式)
  • VUE使用过程中的碰到问题记录
  • 自动对焦技术助力TGV检测 半导体检测精度大突破
  • 工作台-02.代码开发
  • Linux信号机制:从入门到精通
  • [Python]-基础篇1- 从零开始的Python入门指南
  • 微调大语言模型(生成任务),怎么评估它到底“变好”了?
  • Python网安-zip文件暴力破解
  • Java:链接mysql数据库报错:CommunicationsException: Communications link failure
  • Coze API如何上传文件能得到文件的file_url
  • 缓解停车难的城市密码:4G地磁检测器如何重构车位资源分配
  • Discrete Audio Tokens: More Than a Survey
  • TensorRT-LLM的深度剖析:关键问题与核心局限性
  • Java-异常类
  • Spearman检验组间相关性及SCI风格绘图
  • MySQL之事务原理深度解析
  • [Python] -基础篇2-Python中的变量和数据类型详解
  • 运营医疗信息化建设的思路
  • 创建一个简单入门SpringBoot3项目
  • 华为云Flexus+DeepSeek征文|高可用部署 Dify 平台构建 Flux 绘画中台 Chatflow 的设计与实现
  • Re:从零开始的文件结构(融合线性表来理解 考研向)
  • 设计模式精讲 Day 15:解释器模式(Interpreter Pattern)
  • LangChain4j(20)——调用百度地图MCP服务
  • Ubuntu系统Postman资源占用高怎么办?
  • Stable Diffusion入门-ControlNet 深入理解 第二课:ControlNet模型揭秘与使用技巧