当前位置:网站首页>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 ;
边栏推荐
- 逸仙电商发布一季报:坚持研发及品牌投入,实现可持续高质量发展
- MIT-6874-Deep Learning in the Life Sciences Week4
- Ant s19xp appeared in 140t, why is it called the computing power ceiling by the world
- About the split and join operations of strings
- 半钢同轴射频线的史密斯圆图查看和网络分析仪E5071C的射频线匹配校准
- 6.Redis新数据类型
- Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
- GNN hands on practice (II): reproduction graph attention network gat
- MIT-6874-Deep Learning in the Life Sciences Week5
- Setting up the d2lbook environment for Li Mu's "hands on learning and deep learning"
猜你喜欢

Eth is not connected to the ore pool

MIT-6874-Deep Learning in the Life Sciences Week4

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

C語言實現掃雷遊戲,附詳解及完整代碼

Dyson design award, changing the world with sustainable design

Js获取指定字符串指定字符位置&指定字符位置区间的子串【简单详细】

MySQL advanced SQL statement of database (2)

MySQL index, transaction and storage engine of database (3)

The digital collection of sunanmin's lotus heart clearing was launched on the Great Wall Digital Art

新冠无情人有情,芸众惠爱心善举暖人间——捐赠商丘市儿童福利院公益行动
随机推荐
MySQL log management, backup and recovery of databases (2)
AttributeError: ‘Version‘ object has no attribute ‘major‘
Koreano essential creates a professional style
About the split and join operations of strings
“昆明城市咖啡地图”活动再度开启
六月集训(第30天) —— 拓扑排序
The URL copied by the browser and pasted into the document is a hyperlink
Leetcode question brushing (III) -- binary search (go Implementation)
Xinguan has no lover, and all the people benefit from loving deeds to warm the world -- donation to the public welfare action of Shangqiu children's welfare home
技能梳理[email protected]体感机械臂
Theme Studio
Rider does not prompt after opening unity script
Implementation of monitor program with assembly language
开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
GD32 RT-Thread OTA/Bootloader驱动函数
MySQL index, transaction and storage engine of database (2)
Action bright: take good care of children's eyes together -- a summary of the field investigation on the implementation of action bright in Guangxi
Basic MySQL operation commands of database
KOREANO ESSENTIAL打造气质职场范
技能梳理[email protected]+阿里云+nbiot+dht11+bh1750+土壤湿度传感器+oled