当前位置:网站首页>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
边栏推荐
- MySQL installation error solution
- Thymeleaf简介
- cv2.polylines
- Arrays and Structures
- Pytorch framework to study record 6 - the torch. Nn. The Module and the torch nn. Functional. The use of conv2d
- MYSQL 唯一约束
- handler+message【消息机制】
- 【驱动】udev设置GPIO加载后所有者、所属组和权限
- 新型LaaS协议Elephant Swap给ePLATO提供可持续溢价空间
- PyG builds R-GCN to realize node classification
猜你喜欢

函数的底层机制

网页元素解析a标签

海外多家权威媒体热议波场TRON:为互联网去中心化奠定基础

Android Studio 实现登录注册-源代码 (连接MySql数据库)

The difference between BGP room and ordinary room in Beijing

Roperties class configuration file & DOS to view the host network situation

MYSQL 唯一约束

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

OA Project Pending Meeting & History Meeting & All Meetings

Go书籍大全-从初级到高级以及Web开发
随机推荐
mysql 结构、索引详解
[SQL] at a certain correlation with a table of data update another table
New interface - API interface for "Taote" keyword search
Drools (7): WorkBench
Detailed transport layer
What is CDH/CDP?
PyG builds R-GCN to realize node classification
MySQL 字符串拼接 - 多种字符串拼接实战案例
Go 学习笔记(84)— Go 项目目录结构
MySQL installation error solution
phpoffice edit excel document
Why is the Kirin 9000 5G version suddenly back in stock?
@WebServlet注解(Servlet注解)
MySQL data query (subtotal and sorting)
Roperties类配置文件&DOS查看主机网络情况
CMake installation and testing
Android Studio 实现登录注册-源代码 (连接MySql数据库)
C. Travelling Salesman and Special Numbers (二进制 + 组合数)
Advanced [C] array to participate in the function pointer
【驱动】udev设置GPIO加载后所有者、所属组和权限