SQL UNIQUE constraint The UNIQUE constraint uniquely identifies each record in a database table. Note that you can have multiple UNIQUE constraints per table, but you can have only one PRIMARY KEY constraint per table. The following SQL creates a UNIQUE constraint on the "Id_P" column when the "Persons" table is created: CREATE TABLE Persons ( Id_P int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), UNIQUE (Id_P) ) If you need to name a UNIQUE constraint and define a UNIQUE constraint for multiple columns, use the following SQL syntax: CREATE TABLE Persons ( Id_P int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName) ) When the table has been created, to create a UNIQUE constraint on the "Id_P" column, use the following SQL: To name a UNIQUE constraint and define a UNIQUE constraint on multiple columns, use the following SQL syntax: To drop a UNIQUE constraint, use the following SQL: In this way, every time you insert a duplicate record, MySQL will prompt Duplicate entry value1-value2 for key uni_que. Of course, you can add ignore to ignore when inserting. Now that there are no duplicate records, we want to start inserting if the record does not exist and updating if it exists. This statement means to insert a value. If there is no record, execute In this section, if the record exists, execute You may also be interested in:
|
<<: WeChat applet realizes multi-line text scrolling effect
>>: How to show or hide common icons on the desktop in Windows Server 2012
Table of contents 1. Introduction to MHA 1. What ...
Everyone knows that images on web pages are genera...
Table of contents background 1. Document Descript...
As shown below: 1. ssh -v -p [port number] [user ...
Table of contents background 1) Enable the keepch...
When we encounter a fault, we often think about h...
1. scale() method Zoom refers to "reducing&q...
The Docker daemon uses HTTP_PROXY , HTTPS_PROXY ,...
Use "onInput(event)" to detect whether ...
introduction Let's start with our content. I ...
MySQL UNION Operator This tutorial introduces the...
In MySQL, there is a function called "group_...
Error occurs: When exporting the database from My...
The explain command is the primary way to see how...
Table of contents 1. Delete the old version 2. Ch...