当前位置:网站首页>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 ;
边栏推荐
- Jinbei LT6 is powerful in the year of the tiger, making waves
- Implementation of monitor program with assembly language
- 转卡通学习笔记
- 戴森设计大奖,以可持续化设计改变世界
- Why can't you rob scientists of NFT
- About the split and join operations of strings
- Getting started with X86 - take over bare metal control
- Node environment configuration
- Harvester ch1 of CKB and HNS, connection tutorial analysis
- [AGC] build service 3- authentication service example
猜你喜欢
[email protected]體感機械臂"/>技能梳理[email protected]體感機械臂

逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展

2022第六季完美童模 托克逊赛区 决赛圆满落幕

ArcGIS Pro + PS 矢量化用地规划图

Harvester ch1 of CKB and HNS, connection tutorial analysis

Jump table introduction

What is the real performance of CK5, the king machine of CKB?

Test memory read rate

打通供应链 深圳礼品展助跨境电商寻破局之道

Guolin was crowned the third place of global popularity of perfect master in the third quarter of 2022
随机推荐
Detailed explanation of commissioning methods and techniques
[ark UI] implementation of the startup page of harmoniyos ETS
GNN hands on practice (II): reproduction graph attention network gat
Koreano essential creates a professional style
Eth is not connected to the ore pool
Deploy lvs-dr cluster
Implementation of iterative method for linear equations
A brief introduction to database mysql
ArcGIS Pro脚本工具(5)——排序后删除重复项
调试方法和技巧详解
Curl --- the request fails when the post request parameter is too long (more than 1024b)
2022第六季完美童模 合肥赛区 初赛圆满落幕
逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展
What is the real performance of CK5, the king machine of CKB?
乡村振兴公益基金启动暨古茶树非遗保护公益行发布
Theme Studio
Magnetic levitation 3D lamp
Guolin was crowned the third place of global popularity of perfect master in the third quarter of 2022
GD32 RT-Thread RTC驱动函数
背课文记单词,读课文记单词,读文章记单词;40篇文章搞定3500词;71篇文章突破中考单词;15篇文章贯通四级词汇;15篇文章贯通六级词汇