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', ...