当前位置:网站首页>MYSQL unique constraint
MYSQL unique constraint
2022-08-02 06:06:00 【good moon births autumn】
First, the unique constraint (UNIQUE)
Unique constraints are used to ensure that the data in a column (or group of columns) is unique.Similar to primary key, but with the following differences:
- Tables can contain multiple unique constraints, but only one primary key is allowed per table.
- Unique constraint columns can contain NULL values.
- Unique constraint columns can be modified or updated.
- The value of the unique constraint column can be reused.
- Unique constraints cannot be used to define foreign keys.
Second, create a unique constraint
1. Define a unique constraint for a single column in a table
CREATE TABLE Employees(? ? social_num INTEGER UNIQUE);-- Add the keyword UNIQUE to the social_num column definition of the table to make it a unique constraint.If an insert or update in the social_num column results in a duplicate value, MySQL will issue an error message and reject the change2, define the unique constraint of the table (single column)
CREATE TABLE Employees(employee_name CHAR(50) NOT NULL,social_num INTEGER,phone INTEGER,UNIQUE (social_num));-- If an insert or update in the social_num column results in a duplicate value, MySQL will issue an error message and reject the change3. Define the unique constraint of the table (multi-column union)
CREATE TABLE Employees(employee_name CHAR(50) NOT NULL,social_num INTEGER,phone INTEGER,UNIQUE (employee_name, social_num));-- The two fields are required to be unique when they are combined, that is, employee_name + social_num is unique4. Use CONSTRAINT syntax to define a unique constraint (the constraint name can be specified)
(1) Defined when creating a table
-- Defined when creating a tableCREATE 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 dataINSERT INTO Employees(employee_name, employee_address) VALUES('a', 'NJ');- The first UNIQUE constraint is applied to the phone column, indicating that each employee must have a different phone number
- The second UNIQUE constraint name is name_address, which means that the employee employee_name can be repeated, and the employee_address can be repeated, but employee_name+employee_address must be unique and not repeated.Example: a + NJ and a + SH
a + NJ and b + NJ
a + NJ and b + SH
a + NJ and a + NJ error "1062 - Duplicate entry"

(2) Add a unique constraint to the created table
-- Add a unique constraint to the created table and specify the constraint name as uni_nameALTER TABLE EmployeesADD CONSTRAINT uni_name UNIQUE (employee_name);-- Add a unique constraint to the created table without specifying a constraint nameALTER TABLE EmployeesADD UNIQUE (employee_name);Third, delete the unique constraint
When adding a unique constraint, MySQL will create a corresponding BTREE index for the database, so delete the index to delete the constraint.Query the index with the following command:
SHOW INDEX FROM table name;
-- Format 1:DROP INDEX constraint name ON table name;-- E.g:DROP INDEX name_address ON Employees;-- Format 2:ALTER TABLE table nameDROP INDEX constraint name;-- E.g:ALTER TABLE EmployeesDROP INDEX name_address;Reference learning link:
MySQL Unique Constraint - MySQL Tutorial?
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
- Detailed explanation of mysql stored procedure
- [Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation
- 质数路径(DAY 99)
- MySQL(7)
- MySQL multi-table association one-to-many query to get the latest data
- UE4 蓝图实现AI随机移动
- 【无标题】
- CubeSat Light-1
- 分享|5G+智慧工业园区解决方案(附PDF)
猜你喜欢

简道云-灵活易用的应用搭建平台

【云原生】什么是CI/CD? | CI/CD 带来的好处

"Digital reconstruction of the system, getting the CEO is the first step"

navicat无法连接mysql超详细处理方法

Detailed explanation of mysql stored procedure

系统(层次)聚类

Towhee 每周模型

公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范

How to quickly delete the compressed package password?

Mysql存储json格式数据
随机推荐
12个MySQL慢查询的原因分析
mysql 查询表 所有字段
navicat新建数据库
在 .NET MAUI 中如何更好地自定义控件
Go语言中定时任务库Cron使用详解
数学建模学习笔记:层次分析法(AHP)
Towhee 每周模型
【Gazebo入门教程】第一讲 Gazebo的安装、UI界面、SDF文件介绍
【HCIE】NO.30 OSPFv3的基本配置
SQL数据表增加列
力扣练习——37 复原IP地址
navicat无法连接mysql超详细处理方法
ZCMU--1891: kotomi and game(C语言)
[QNX Hypervisor 2.2用户手册]9.19 user
力扣练习——44 路径总和 III
RADIUS 如何提高 WiFi 无线网络安全性?
分享|5G+智慧工业园区解决方案(附PDF)
ES6——class类实现继承
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
来自雪域高原的馈赠——大凉山高原生态糖心苹果