MySQL uses UNIQUE for non-repeated data insertion. mysqlunique

Source: Internet
Author: User

MySQL uses UNIQUE for non-repeated data insertion. mysqlunique

SQL UNIQUE constraints

The UNIQUE constraint uniquely identifies each record in the database table.
UNIQUE and primary key constraints provide uniqueness guarantee for columns or column sets.
Primary key has automatically defined UNIQUE constraints.

Note that each table can have multiple UNIQUE constraints, but each table can have only one primary key constraint.

The following SQL statement creates a UNIQUE constraint in the "Id_P" column when creating the "Persons" table:

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))

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 a table has been created, use the following SQL statement to create a UNIQUE constraint in the "Id_P" column:

ALTER TABLE Persons ADD UNIQUE (Id_P)

To name the UNIQUE constraint and define the UNIQUE constraint for multiple columns, use the following SQL Syntax:

ALTER TABLE Persons ADD CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)

To revoke the UNIQUE constraint, use the following SQL statement:

ALTER TABLE Persons DROP INDEX uc_PersonID

In this way, MySQL will prompt Duplicate entry value1-value2 for key uni_que every time you insert Duplicate records. Of course you can add ignore to insert to ignore this
After the record is retained, insert the record if the record does not exist and update the record if the record does not exist.

INSERT INTO tablename (field1, field2, field3, ...) VALUES ('value1', 'value2','value3', ...) ON DUPLICATE KEY UPDATE field1='value1', field2='value2', field3='value3', ...

This statement means to insert a value. If this record is not executed

INSERT INTO tablename (field1, field2, field3, ...) VALUES ('value1', 'value2','value3', ...)

If this record exists, execute

UPDATE field1='value1', field2='value2', field3='value3', ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.