programplug Logo



SQL Unique Key

Unique Key in SQL


A unique key is used to one or more than one fields/coloumns of a table that have uniquely identify records in a database table.


You can also assume that it is like a primary key but it takes one null value and it cannot have theor duplicate values.
Both the key whether it is Unique key or Primary key both will provide uniqueness gurrantee for a coloumn.


SQL SERVER:
 CREATE TABLE Student (
    ID int NOT NULL UNIQUE,
    Last_Name varchar(255) NOT NULL,
    Firs_tName varchar(255),
    class int
);
 
MYSQL SERVER:
 CREATE TABLE Student (
    ID int NOT NULL,
    Last_Name varchar(105) NOT NULL,
    First_Name varchar(250),
    class int,
    UNIQUE (ID)
);