当前位置:网站首页>mysql数据库基础:约束、标识列
mysql数据库基础:约束、标识列
2022-06-30 09:46:00 【持久的棒棒君】
约束
1、定义
一种限制,用于限制表中的数据,为了保证表中的数据的准确和可靠性。
2、分类
- NOT NULL:非空,用于保证该字段的值不能为空
- DEFAULT:默认,用于保证该字段有默认值
- PRIMARY KEY:主键,用于保证该字段的值具有唯一性,并且非空
- UNIQUE:唯一,用于保证该字段的值具有唯一性,可以为空。
- CHECK:检查约束(mysql中不支持)
- FOREIGN KEY:外键约束,限制两个表的关系,用于保证该字段的值必须来自于主表的关联列的值。在从表中添加外键约束,用于引用主表中某列的值。
3、创建表时添加约束
3.1 列级约束
列级约束通常直接写在字段类型后面,如下:
create table 表名(
字段名 字段类型 列级约束,
字段名 字段类型 列级约束,
);
- 例如
# 学生表
create table student(
id int primary key, # 主键
stuName varchar(20) not null, # 非空
gender char(1) check(gender='男' or gender='女'), # 检查
seat int unique, # 唯一约束
age int default 18, # 默认约束
majorId int foreign key references major(id) # 外键约束
)
# 课程表
create table major(
id int primary key,
majorName varchar(20)
);
注意:六大约束在语法上都支持,不会报错,但外键约束和检查约束没有效果
3.2 表级约束
表级约束通常在所有字段的最下面,语法如下:
create table 表名(
字段名 字段类型,
字段名 字段类型,
# 表级约束
【constraint 别名】 约束类型(字段名),
...
);
- 例如
# 学生表
create table student(
id int,
stuName varchar(20),
gender char(1),
seat int,
age int,
majorId int,
# 添加表级约束
constraint pk primary key(id), # 给id添加主键约束
constraint uq unique(seat),
constraint ck check(gender='男' or gender='女'), #检查约束
constraint fk_student_major foreign key(majorId) references major(id) # 外键约束
)
# 课程表
create table major(
id int primary key,
majorName varchar(20)
);
表级约束中,不支持非空和默认约束
对比主键与唯一键
(1)主键和唯一键都要保证唯一性
(2)主键不可以为空,唯一键可以为空
(3)一个表中至多一个主键,但可以有多个唯一键
4、修改表时添加约束
语法:
# 列级约束
alter table 表名 modify column 列名 类型 约束类型
# 表记约束
alter table 表名 add 【constraint 约束名】 约束类型(字段名) 【外键的引用】
例如:
# 添加非空约束
alter table student modify column stuName varchar(20) not null;
# 添加默认约束
alter table student modify column age int default 18;
# 添加主键
# (1) 列级约束
alter table student modify column id int primary key;
# (2) 表级约束
alter table student add primary key(id);
# 添加唯一键
# (1) 列级约束
alter table student modify column seat int unique;
# (2) 表级约束
alter table student add unique(seat);
# 添加外键
alter table student add foreign key(majorid) references major(id);
列级约束不支持起别名,表级约束可以起别名
4、修改表时删除约束
# 删除非空约束
alter table student modify column stuName varchar(20) null;
# 删除主键
alter table student drop primary key;
# 删除默认约束
alter table student modify column age int;
# 删除唯一
alter table student drop index seat;
# 删除外键
alter table student drop foreign key majorid;
标识列
定义
又称为自增长列,可以不用手动的插入值,系统提供默认的序列值
使用
- 创建表时设置标识列
create table tab_identity(
id int primary key auto_increment,
name varchar(20)
);
# 插入数据
insert into tab_identity values(null,'john');
insert into tab_identity values(null,'john');
insert into tab_identity values(null,'john');
insert into tab_identity values(null,'john');
select * from tab_identity;

通过下面的语句可以查看标识列的步长和起始值
show variables like '%auto_increment%';

通过下面的语句可以修改标识列的步长,mysql中不支持修改起始值。
set auto_increment_increment=3;
特点
(1)标识列需要和一个键搭配(主键,唯一键等)
(2)一个表至多一个标识列
(3)标识列的类型只能是数值型
- 修改表时设置标识列
alter table tab_identity modify column id int primary key auto_increment;
- 修改表时删除标识列
alter table tab_identity modify column id int ;
边栏推荐
- 技能梳理[email protected]+阿里云+nbiot+dht11+bh1750+土壤湿度传感器+oled
- ‘Failed to fetch current robot state‘ when using the ‘plan_kinematic_path‘ service #868
- Guolin was crowned the third place of global popularity of perfect master in the third quarter of 2022
- Enter the world of helium (hNT) hotspot servers to bring you different benefits
- 六月集训(第30天) —— 拓扑排序
- Implementation of iterative method for linear equations
- ArcGIS Pro脚本工具(6)——修复CAD图层数据源
- What is the real performance of CK5, the king machine of CKB?
- Applying applet container technology to IOT ecological construction
- MIT-6874-Deep Learning in the Life Sciences Week6
猜你喜欢

戴森设计大奖,以可持续化设计改变世界

Chen Haotian won the national championship of the national finals of the 7th children's model star ceremony

Yixian e-commerce released its first quarterly report: adhere to R & D and brand investment to achieve sustainable and high-quality development

新冠无情人有情,芸众惠爱心善举暖人间——捐赠商丘市儿童福利院公益行动

光明行动:共同呵护好孩子的眼睛——广西实施光明行动实地考察调研综述

6. Redis new data type

Jump table introduction

调试方法和技巧详解

Go -- maximum heap and minimum heap

Action bright: take good care of children's eyes together -- a summary of the field investigation on the implementation of action bright in Guangxi
随机推荐
打通供应链 深圳礼品展助跨境电商寻破局之道
那个程序员,被打了。
ArcGIS Pro + PS 矢量化用地规划图
GD32 RT-Thread OTA/Bootloader驱动函数
AttributeError: ‘Version‘ object has no attribute ‘major‘
Didn't receive robot state (joint angles) with recent timestamp within 1 seconds
记一次实习的经历,趟坑必备(一)
跳跃表介绍
JS get the substring of the specified character position and the specified character position interval of the specified string [simple and detailed]
South China Industrial Group launched digital economy and successfully held the city chain technology conference
开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
“昆明城市咖啡地圖”活動再度開啟
A brief introduction to database mysql
NLopt--非线性优化--原理介绍及使用方法
IPhone address book import into Excel
OSError: [Errno 28] No space left on device
机器人系统动力学——惯性参数
Tooltips in the era of touch
Configure Yii: display MySQL extension module verification failed
Ant s19xp appeared in 140t, why is it called the computing power ceiling by the world