SQL UNIQUE constraint The UNIQUE constraint uniquely identifies each record in a database table. Both the UNIQUE and PRIMARY KEY constraints provide a guarantee of uniqueness for a column or collection of columns. The PRIMARY KEY has an automatically defined UNIQUE constraint. Note that each table can have multiple UNIQUE constraints, but each table can have only one PRIMARY KEY constraint. Select * from Persons where Id_P = UNIQUE;
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 UNIQUE constraints and define UNIQUE constraints 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) )
SQL > create UNIQUE constraint on ‘Id_P’;
ALTER TABLE Persons ADD UNIQUE (Id_P)
To name UNIQUE constraints and define UNIQUE constraints for multiple columns, use the following SQL syntax:
ALTER TABLE Persons ADD CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)
To undo the UNIQUE constraint, use the following SQL
ALTER TABLE Persons DROP INDEX uc_PersonID
Duplicate entry value1-value2 for key uni_que (Duplicate entry value1-value2 for key uni_que, Duplicate entry value1-value2 for key uni_que) The update operation exists
INSERT INTO tablename (field1, field2, field3, ...) VALUES ('value1', 'value2','value3', ...) ON DUPLICATE KEY UPDATE field1='value1', field2='value2', field3='value3', ...
Insert value if no record is executed
INSERT INTO tablename (field1, field2, field3, ...) VALUES ('value1', 'value2','value3', ...)
This section is executed if the record exists
UPDATE field1='value1', field2='value2', field3='value3', ...