当前位置:网站首页>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 ;
边栏推荐
- Launch of Rural Revitalization public welfare fund and release of public welfare bank for intangible cultural heritage protection of ancient tea tree
- C語言實現掃雷遊戲,附詳解及完整代碼
- KOREANO ESSENTIAL打造气质职场范
- L'activité "Kunming City coffee map" a rouvert
- Highlight display of Jinbei LB box, adhering to mini special effects
- keras ‘InputLayer‘ object is not iterable
- Curl --- the request fails when the post request parameter is too long (more than 1024b)
- unable to convert expression into double array
- JS get the substring of the specified character position and the specified character position interval of the specified string [simple and detailed]
- 光明行动:共同呵护好孩子的眼睛——广西实施光明行动实地考察调研综述
猜你喜欢

郭琳加冕 2022第三季完美大师 全球人气季军

The preliminary round of the sixth season of 2022 perfect children's model Hefei competition area was successfully concluded

2022第六季完美童模 合肥赛区 初赛圆满落幕

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

NLopt--非线性优化--原理介绍及使用方法

Dyson design award, changing the world with sustainable design

Getting started with X86 - take over bare metal control

Magnetic levitation 3D lamp

Test memory read rate

MySQL log management, backup and recovery of databases (2)
随机推荐
著名画家史国良《丰收时节》数字藏品上线长城数艺
Chen Haotian won the national championship of the national finals of the 7th children's model star ceremony
The famous painter shiguoliang's "harvest season" digital collection was launched on the Great Wall Digital Art
"Hackers and painters" -- why not be stupid
Oracle creates a stored procedure successfully, but the compilation fails
After recording 7000 IELTS words in 100 sentences, there are only 1043 words (including simple words such as I and you)
Eth is not connected to the ore pool
Node environment configuration
AttributeError: ‘Version‘ object has no attribute ‘major‘
采坑:Didn‘t receive robot state (joint angles) with recent timestamp within 1 seconds.
NLopt--非线性优化--原理介绍及使用方法
1033 To Fill or Not to Fill
Yixian e - commerce publie un rapport trimestriel: adhérer à la R & D et à l’investissement de la marque, réaliser un développement durable et de haute qualité
技能梳理[email protected]体感机械臂
Leetcode question brushing (II) -- sorting (go Implementation)
Js獲取指定字符串指定字符比特置&指定字符比特置區間的子串【簡單詳細】
‘Failed to fetch current robot state‘ when using the ‘plan_kinematic_path‘ service #868
Implementation of iterative method for linear equations
Foster design method
【C语言快速上手】带你了解C语言,零基础入门③