Loading...

Unique Key is used to uniquely identify the row in SQL Table. We can say that it is little like Primary Key but it can accept only one null value and it cannot have duplicate values. In a single table more then one Unique Key can be created. When we create a Primary Key constraint in a table Unique Key is automatically created. 


Student_ID

Student_Name

Father_Name

Email

DOB

Gender

1

Rakesh Kumar

Mr. Upendra Singh

rakeshkumar@gmail.com

12-Jul-91

M

2

Suman Kumari

Mr. Rohit Pathak

suman90@gmail.com

10-Mar-90

F

3

Anil Yadav

Mr. Sunil Yadav

anily12@gmail.com

18-Mar-92

M

4

Ravi Singh

Mr. Vivek Singh

ravikumar56@gmail.com

15-Dec-91

M

5

Prakash Kumar

Mr. Suraj Singh

null

23-Oct-93

M

6

Vijay Verma

Mr. Mohan Verma

vijay1990@gmail.com

1-Feb-90

M



Here, we took a table Student where all students have his email ID but for Prakash Kumar don't have email ID so, its value is NULL


Unique Key when new table is creating

Create table Students  
(  
Student_ID int NOT NULL UNIQUE,  
LastName varchar (255) NOT NULL,  
FirstName varchar (255),  
City varchar (255)  


Unique Key with existing table

Alter table Students  
ADD CONSTRAINT uc_StudentId UNIQUE  (Student_ID)    


Remove Unique Key Constraint

Alter table Students  
DROP CONSTRAINT uc_StudentId