CREATE TABLE table_name(
column1 datatype(size) UNIQUE,
column2 datatype(size) ,
column3 datatype(size) ,
);
Example
CREATE TABLE Persons (
ID int UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
);
In the above example, as we have used unique constraint on ID column we are not supposed to enter the data that is already present, simply no two ID values are same.
Combination of not null and unique
- You can use multiple constraints on a single column
- Whenever you apply both not null and unique constraints to a column it restricts to have only unique data values and also show it prevents null values at the same time.
Example
Primary key vs Unique key
- Both primary key and unique key will not allow duplicate values but the difference is that, the primary key will not accept null values where as unique key allows null values on the column
- Simply only one
unique + not null= primary key