当前位置:网站首页>MYSQL unique constraint
MYSQL unique constraint
2022-07-30 04:20:00 【m0_67403013】
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 non-repeatable.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 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
边栏推荐
- Pytorch framework learning record 3 - the use of Transform
- Unity3D Application模拟进入前后台及暂停
- 机器学习:知道通过低方差过滤实现降维过程
- 【Redis高手修炼之路】Jedis——Jedis的基本使用
- Redis "super explanation!!!!!!"
- 验证addShutdownHook钩子生效
- Operational configuration: How to run multiple EasyCVR programs as a service in one server?
- Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
- golang八股文整理(持续搬运)
- Detailed transport layer
猜你喜欢

为什么突然间麒麟 9000 5G 版本,又有库存了?

使用EFR32作为Zigbee/Thread的sniffer的用法

My first experience of Go+ language——Blessing message system, so that she can also feel your blessings

精品MySQL面试题,备战八月99%必问!过不了面试算我的

The difference between forward and redirect

商品管理系统数据库设计--SQL Server

state space representation

Pytorch框架学习记录2——TensorBoard的使用

高并发框架 Disruptor

How does the AI intelligent security video platform EasyCVR configure the simultaneous transmission of audio and video?
随机推荐
Many overseas authoritative media hotly discuss TRON: laying the foundation for the decentralization of the Internet
golang中如何比较struct,slice,map是否相等以及几种对比方法的区别
How to extract year, month and day data in date type in SQL Server
forward与redirect的区别
Roperties类配置文件&DOS查看主机网络情况
sublime text 3 settings
2.4希尔排序
What are Redis server startup after the operation?
Go书籍大全-从初级到高级以及Web开发
SQLSERVER merges subquery data into one field
C. Travelling Salesman and Special Numbers (二进制 + 组合数)
(6) "Digital Electricity" - Diodes and CMOS Gate Circuits (Introduction)
Charles 替换 接口响应信息
phpoffice edit excel document
QT(39)-vs开发qt程序提示无法打开源文件
Redis "super explanation!!!!!!"
Taobao/Tmall get Taobao store details API
Redis【超详解!!!】
Why is the Kirin 9000 5G version suddenly back in stock?
How does the AI intelligent security video platform EasyCVR configure the simultaneous transmission of audio and video?