当前位置:网站首页>MySQL约束的分类、作用及用法
MySQL约束的分类、作用及用法
2022-07-06 14:38:00 【黑马程序员官方】
MySQL性能强劲,是目前使用最广泛的数据库之一,以 MySQL为学习原型也方便之后掌握其他数据库,下面就给大家全面讲解下MySQL8.0的新特性,从零基础到高阶一站式学习,结合实际案例让大家有所收获!
▼ MySQL8.0入门-高阶学习笔记:(汇总)
- 第1讲:SQL概述及数据库系统介绍
- 第2讲:MySQL简介、详细安装步骤及使用
- 第3讲:MySQL常用图形管理工具
- 第4讲:MySQL数据库基本操作-DDL
- 第5讲:MySQL数据库基本操作-DML
目录
三、MySQL约束-自增长约束(auto_increment)
一、MySQL约束
概念
约束英文:constraint
约束实际上就是表中数据的限制条件
作用
表在设计的时候加入约束的目的就是为了保证表中的记录完整性和有效性,比如用户表有些列的值(手机号)不能为空,有些列的值(身份证号)不能重复。
分类
- 主键约束(primary key) PK
- 自增长约束(auto_increment)
- 非空约束(not null)
- 唯一性约束(unique)
- 默认约束(default)
- 零填充约束(zerofill)
- 外键约束(foreign key) FK
二、MySQL约束-主键约束
概念
- MySQL主键约束是一个列或者多个列的组合,其值能唯一地标识表中的每一行,方便在RDBMS中尽快的找到某一行。
- 主键约束相当于 唯一约束 + 非空约束 的组合,主键约束列不允许重复,也不允许出现空值。
- 每个表最多只允许一个主键
- 主键约束的关键字是:primary key
- 当创建主键的约束时,系统默认会在所在的列和列组合上建立对应的唯一索引。
操作
- 添加单列主键
- 添加多列联合主键
- 删除主键
操作-添加单列主键
创建单列主键有两种方式,一种是在定义字段的同时指定主键,一种是定义完字段之后指定主键。
方法1-语法:
-- 在 create table 语句中,通过 PRIMARY KEY 关键字来指定主键。
--在定义字段的同时指定主键,语法格式如下:
create table 表名(
...
<字段名> <数据类型> primary key
...
)方法1-实现:
create table emp1(
eid int primay key,
name VARCHAR(20),
deptId int,
salary double
);方式2-语法:
--在定义字段之后再指定主键,语法格式如下:
create table 表名(
...
[constraint <约束名>] primary key [字段名]
);方式2-实现:
create table emp2(
eid INT,
name VARCHAR(20),
deptId INT,
salary double,
constraint pk1 primary key(id)
);操作-添加多列主键(联合主键)
所谓的联合主键,就是这个主键是由一张表中多个字段组成的。
注意:
- 当主键是由多个字段组成时,不能直接在字段名后面声明主键约束。
- 一张表只能有一个主键,联合主键也是一个主键
语法:
create table 表名(
...
primary key (字段1,字段2,…,字段n)
);实现:
create table emp3(
name varchar(20),
deptId int,
salary double,
primary key(name,deptId)
);通过修改表结构添加主键
主键约束不仅可以在创建表的同时创建,也可以在修改表时添加。
语法:
create table 表名(
...
);
alter table <表名> add primary key(字段列表);实现:
-- 添加单列主键
create table emp4(
eid int,
name varchar(20),
deptId int,
salary double,
);
alter table emp4 add primary key(eid);删除主键约束
一个表中不需要主键约束时,就需要从表中将其删除。删除主键约束的方法要比创建主键约束容易的多。
格式:
alter table <数据表名> drop primary key;
实现:
-- 删除单列主键
alter table emp1 drop primary key;
-- 删除联合主键
alter table emp5 drop primary key;三、MySQL约束-自增长约束(auto_increment)
概念
在 MySQL 中,当主键定义为自增长后,这个主键的值就不再需要用户输入数据了,而由数据库系统根据定义自动赋值。每增加一条记录,主键会自动以相同的步长进行增长。
通过给字段添加 auto_increment 属性来实现主键自增长
语法
字段名 数据类型 auto_increment
操作
create table t_user1(
id int primary key auto_increment,
name varchar(20)
);特点
- 默认情况下,auto_increment的初始值是 1,每新增一条记录,字段值自动加 1。
- 一个表中只能有一个字段使用 auto_increment约束,且该字段必须有唯一索引,以避免序号重复(即为主键或主键的一部分)。
- auto_increment约束的字段必须具备 NOT NULL 属性。
- auto_increment约束的字段只能是整数类型(TINYINT、SMALLINT、INT、BIGINT 等。
- auto_increment约束字段的最大值受该字段的数据类型约束,如果达到上限,auto_increment就会失效。
指定自增字段初始值
如果第一条记录设置了该字段的初始值,那么新增加的记录就从这个初始值开始自增。例如,如果表中插入的第一条记录的 id 值设置为 5,那么再插入记录时,id 值就会从 5 开始往上增加
-- 方式1,创建表时指定
create table t_user2 (
id int primary key auto_increment,
name varchar(20)
)auto_increment=100;-- 方式2,创建表之后指定
create table t_user3 (
id int primary key auto_increment,
name varchar(20)
);
alter table t_user2 auto_increment=100;delete和truncate在删除后自增列的变化
- delete数据之后自动增长从断点开始
- truncate数据之后自动增长从默认起始值开始
四、MySQL约束-非空约束(not null
概念
MySQL 非空约束(not null)指字段的值不能为空。对于使用了非空约束的字段,如果用户在添加数据时没有指定值,数据库系统就会报错。
语法
方式1:<字段名><数据类型> not null;
方式2:alter table 表名 modify 字段 类型 not null;添加非空约束-方式1
-- 方式1,创建表时指定
create table t_user6 (
id int ,
name varchar(20) not null,
address varchar(20) not null
);添加非空约束-方式
create table t_user7 (
id int ,
name varchar(20) , -- 指定非空约束
address varchar(20) -- 指定非空约束
);
alter table t_user7 modify name varchar(20) not null;
alter table t_user7 modify address varchar(20) not null;删除非空约束
-- alter table 表名 modify 字段 类型
alter table t_user7 modify name varchar(20) ;
alter table t_user7 modify address varchar(20) ;四、MySQL约束-唯一约束(unique)
概念
唯一约束(Unique Key)是指所有记录中字段的值不能重复出现。例如,为 id 字段加上唯一性约束后,每条记录的 id 值都是唯一的,不能出现重复的情况。
语法
方式1:<字段名> <数据类型> unique
方式2: alter table 表名 add constraint 约束名 unique(列);添加唯一约束-方式1
-- 创建表时指定
create table t_user8 (
id int ,
name varchar(20) ,
phone_number varchar(20) unique -- 指定唯一约束
);添加唯一约束-方式2
create table t_user9 (
id int ,
name varchar(20) ,
phone_number varchar(20) -- 指定唯一约束
);
alter table t_user9 add constraint unique_ph unique(phone_number);删除唯一约束
-- alter table <表名> drop index <唯一约束名>;
alter table t_user9 drop index unique_ph;五、MySQL约束-默认约束(default)
概念
MySQL 默认值约束用来指定某列的默认值。
语法
方式1: <字段名> <数据类型> default <默认值>;
方式2: alter table 表名 modify 列名 类型 default 默认值; 添加默认约束-方式1
create table t_user10 (
id int ,
name varchar(20) ,
address varchar(20) default ‘北京’ -- 指定默认约束
);添加默认约束-方式2
-- alter table 表名 modify 列名 类型 default 默认值;
create table t_user11 (
id int ,
name varchar(20) ,
address varchar(20)
);
alter table t_user11 modify address varchar(20) default ‘北京’;删除默认约束
-- alter table <表名> modify column <字段名> <类型> default null;
alter table t_user11 modify column address varchar(20) default null;六、MySQL约束- 零填充约束(zerofill)
概念
1、插入数据时,当该字段的值的长度小于定义的长度时,会在该值的前面补上相应的0
2、zerofill默认为int(10)
3、当使用zerofill 时,默认会自动加unsigned(无符号)属性,使用unsigned属性后,数值范围是原值的2倍,例如,有符号为-128~+127,无符号为0~256。
操作
create table t_user12 (
id int zerofill , -- 零填充约束
name varchar(20)
);删除
alter table t_user12 modify id int;
边栏推荐
- C # realizes crystal report binding data and printing 4-bar code
- Oracle control file and log file management
- 0 basic learning C language - digital tube
- What are the interface tests? What are the general test points?
- GPS从入门到放弃(十三)、接收机自主完好性监测(RAIM)
- Wechat red envelope cover applet source code - background independent version - source code with evaluation points function
- What is the difference between animators and animators- What is the difference between an Animator and an Animation?
- UNI-Admin基础框架怎么关闭创建超级管理员入口?
- The nearest common ancestor of binary (search) tree ●●
- Search element topic (DFS)
猜你喜欢

GPS from getting started to giving up (19), precise ephemeris (SP3 format)

Barcodex (ActiveX print control) v5.3.0.80 free version

Attack and defense world ditf Misc

硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件

zabbix 代理服务器 与 zabbix-snmp 监控

图像的spatial domain 和 frequency domain 图像压缩

2021 geometry deep learning master Michael Bronstein long article analysis

C#實現水晶報錶綁定數據並實現打印4-條形碼

Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution

Spatial domain and frequency domain image compression of images
随机推荐
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
ZABBIX proxy server and ZABBIX SNMP monitoring
AI 企业多云存储架构实践 | 深势科技分享
GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)
数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
【sciter】: 基于 sciter 封装通知栏组件
GPS从入门到放弃(十六)、卫星时钟误差和卫星星历误差
MariaDB database management system learning (I) installation diagram
A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely
GPS from getting started to giving up (19), precise ephemeris (SP3 format)
Solve project cross domain problems
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
Chapter 3: detailed explanation of class loading process (class life cycle)
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
Force buckle 575 Divide candy
Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
2022-07-05 stonedb的子查询处理解析耗时分析
GPS从入门到放弃(十一)、差分GPS