当前位置:网站首页>MySQL约束
MySQL约束
2022-07-22 07:11:00 【小唐同学(๑><๑)】
目录
1.主键约束:
其值能唯一的标识表中的每一行(唯一约束+非空约束)
自定义主键
(1)create table 表名 (
列名1 类型 ,
列名2 类型 primary key
);
(2)create table 表名(
...
primary key(字段名)
);
联合主键:
create table 表名(
...
...
primary key (字段名1,字段名2)
);
2.删除主键:
alter table 表名 drop primary key;
3.自增约束:
添加自增长的列为整数列 必须有唯一索引具备非空属性
主键自增长:
create table 表名(
列名1 数据类型 primary key auto_increment,
...
);
指定自增长字段初始值:
create table 表名(
列名1 数据类型 primary key auto_increment,
...
)auto_increment=数值;
创建表后指定自增长字段初始值:
create table 表名(
列名1 数据类型 primary key auto_increment,
...
);
alter table 表名 auto_increment=数值;
delete 数据后,自动增长从断点继续增长
turncate 数据后自动增长从默认起始值开始
4.非空约束
(1)创建表时指定的约束
create table 表名 (
字段名 类型 not null,
字段名 类型
);
(2)创建表之后修改建立约束
alter table 表名 madify 字段名 类型 not null;
(3)删除非空约束
alter table 表名 modify 字段名 类型;
5.唯一约束
(1)创建表时
create table 表名 (
....
字段名 类型 unique
);
(2)创建表之后
alter table 表名 add constraint unique_pn(唯一约束名) unique(列名);
-----unique_pn 是唯一约束名
在MySQL中NULL和任何值都不相等(包括他自己)
(3)删除唯一约束
alter table 表名 drop index unique_pn(唯一约束名)
6.默认约束
(1)在创建时设置默认值
create table 表名(
字段名 类型 default 默认值
);
(2)创建后指定默认值
alter table 表名 modify 列名 类型 default 默认值;
(3)删除默认约束
alter table 表名 modify 列名 类型 default null;
边栏推荐
- Chery Xingtu's product plan was exposed, and the 2.0T turbocharged engine was launched at the end of the year
- How does win11 close the touch pad? Three solutions for closing the touch panel in win11
- Misc advanced
- Map insert element
- 秒杀实现图
- MySQL constraint_ Non empty constraint
- How to make an appointment while watching the panorama? Here comes the VR catering system tutorial
- PlayBook introduction
- Overview of nftfi track layout
- 基于 Flink CDC 实现海量数据的实时同步和转换
猜你喜欢
ACM warm-up exercise 3 in 2022 summer vacation (summary of some topics)
Hcip OSPF interface network type experiment report
【云原生】Docker部署数据库的持久化
Android 面试题:你说一下 PendingIntent 和 Intent 的区别?
[network counting] (III) hypernetwork, routing, NAT protocol
[HMS core] [FAQ] [account kit] typical problem set 1
【HMS core】【Health Kit】【FAQ】数据订阅功能问题合集
使用 Abp.Zero 搭建第三方登录模块(三):网页端开发
What if the win11 display microphone is not plugged in? Solution of win11 display microphone not plugged in
Niuke net brush question 1 [Fibonacci sequence of minimum steps]
随机推荐
[must see for developers] [push kit] collection of typical problems of push service 1
MySQL constraint_ Self growth constraint_ auto_ increment
Can the task of flinksql monitor whether it is interrupted?
postgreSQL数据库部署在linux服务器上,本机查询ms级,用windows上安装的pgadmin查询超级慢20s左右,是网络的问题还是数据库配置问题?
Virtual machine performance test scheme
Sqlmap is opened in the form of code rather than image
DP subsequence series problem
有人知道oracle cdc这个问题吗?source没有空值,但是查询定义的cdc表时说有空值,让修
Overview of nftfi track layout
关键路径实现
Discussion on the path of Chinese enterprise management software towards globalization and internationalization
栈/堆/队列刷题(上)
What is the difference between win11 beta 22621.436 and 22622.436?
subprocess
VR全景在各行各业是如何展示?如何落地应用的?
家庭琐事问题
【数字IC】深入浅出理解AXI协议
Buu misc advanced
【单片机仿真项目】外部中断0和1分别通过不同触发方式控制二极管
使用 Abp.Zero 搭建第三方登录模块(三):网页端开发