当前位置:网站首页>MYSQL 唯一约束
MYSQL 唯一约束
2022-07-30 04:15:00 【m0_67403013】
一、唯一约束(UNIQUE)
唯一约束用来保证一列(或一组列)中的数据是唯一的。类似与主键,但是有以下区别:
- 表可包含多个唯一约束,但每个表只允许一个主键。
- 唯一约束列可包含 NULL 值。
- 唯一约束列可修改或更新。
- 唯一约束列的值可重复使用。
- 唯一约束不能用来定义外键。
二、创建唯一约束
1、 定义表中单列的唯一约束
CREATE TABLE Employees
(
? ? social_num INTEGER UNIQUE
);
-- 给表的 social_num 列定义添加关键字 UNIQUE,使其成为唯一约束。如果在 social_num 列中插入或更新导致重复值的值,MySQL将发出错误消息并拒绝更改
2、定义表的唯一约束(单列)
CREATE TABLE Employees
(
employee_name CHAR(50) NOT NULL,
social_num INTEGER,
phone INTEGER,
UNIQUE (social_num)
);
-- 如果在 social_num 列中插入或更新导致重复值的值,MySQL将发出错误消息并拒绝更改
3、定义表的唯一约束(多列联合)
CREATE TABLE Employees
(
employee_name CHAR(50) NOT NULL,
social_num INTEGER,
phone INTEGER,
UNIQUE (employee_name, social_num)
);
-- 要求两个字段联合起来时唯一的,即employee_name + social_num 是唯一的
4、使用CONSTRAINT语法定义唯一约束(可指定约束名)
(1)创建表时定义
-- 创建表时定义
CREATE TABLE Employees
(
employee_name CHAR(50) NOT NULL,
social_num INTEGER,
phone INTEGER UNIQUE,
employee_address CHAR(255),
CONSTRAINT name_address UNIQUE (employee_name, employee_address)
);
-- 插入数据
INSERT INTO Employees(employee_name, employee_address) VALUES('a', 'NJ');
- 第一个UNIQUE约束应用于phone列,表示每个雇员必须具有不同的电话号码
- 第二个UNIQUE约束名称为name_address,表示雇员employee_name可以重复,employee_address可以重复,但employee_name+employee_address必须唯一不可重复。例:a + NJ 和 a + SH
a + NJ 和 b + NJ
a + NJ 和 b + SH
a + NJ 和 a + NJ 报错“1062 - Duplicate entry”
(2)在已创建好的表上加唯一约束
-- 在已创建好的表上加唯一约束,指定约束名为uni_name
ALTER TABLE Employees
ADD CONSTRAINT uni_name UNIQUE (employee_name);
-- 在已创建好的表上加唯一约束,不指定约束名
ALTER TABLE Employees
ADD UNIQUE (employee_name);
三、删除唯一约束
添加唯一约束时,MySQL将为数据库创建一个相应的 BTREE 索引,所以删除索引即可删除约束。通过如下命令查询索引:
SHOW INDEX FROM 表名;
-- 格式一:
DROP INDEX 约束名 ON 表名;
-- 例如:
DROP INDEX name_address ON Employees;
-- 格式二:
ALTER TABLE 表名
DROP INDEX 约束名;
-- 例如:
ALTER TABLE Employees
DROP INDEX name_address;
参考学习链接:
mysql联合唯一约束_MySQL中添加唯一约束和联合唯一约束 - 百度文库
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- How to Effectively Conduct Retrospective Meetings (Part 1)?
- 数组和结构体
- 高并发框架 Disruptor
- Azure 开发者新闻快讯丨开发者7月大事记一览
- ospf map
- state space representation
- Pytorch framework learning record 5 - the use of DataLoader
- What are Redis server startup after the operation?
- JQ source code analysis (environment)
- Usage of exists in sql
猜你喜欢
验证addShutdownHook钩子生效
Pytorch framework learning record 4 - the use of datasets (torchvision.dataset)
What are Redis server startup after the operation?
SQL introduction of the first lecture -- MySQL 8.0.29 installation tutorial (Windows 64 - bit)
CMake installation and testing
The leap second that may cause the next "Millennium Bug" is boycotted by tech giants
When the EasyNVR platform is cascaded to the EasyCVR, why can't the video be played after a while?
(6) "Digital Electricity" - Diodes and CMOS Gate Circuits (Introduction)
ospf map
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
随机推荐
为什么突然间麒麟 9000 5G 版本,又有库存了?
The curl command to get the network IP
Pytorch framework learning record 2 - the use of TensorBoard
The leap second that may cause the next "Millennium Bug" is boycotted by tech giants
Summary of Rpc and gRpc Introduction
Boutique: Taobao/Tmall Get Order Details API for Purchased Products
国内首家沉浸式高逼真元宇宙,希元宇宙正式上线
spicy(二)unit hooks
Drools (7): WorkBench
The difference between forward and redirect
Pytorch framework learning record 7 - convolutional layer
使用EFR32作为Zigbee/Thread的sniffer的用法
Chapter 51 - Knowing the request header parameter analysis【2022-07-28】
Wechat second-hand transaction small program graduation design finished product (1) Development overview
Pytorch框架学习记录4——数据集的使用(torchvision.dataset)
[Switch] Protocol-Oriented Programming in Swift: Introduction
Why is the Kirin 9000 5G version suddenly back in stock?
Pytorch framework learning record 5 - the use of DataLoader
How to compare struct, slice, map for equality and the difference between several comparison methods in golang
权值线段树+线段树分裂/合并+CF1659D