Loading ...

Unique constraint in SQL | CodeAsp.Net

Unique constraint in SQL

 /5
0 (0votes)

In this blog I will explain Unique constraint in SQL.If i create a UNIQUE constraint, i can't insert duplicate data in that column.
The UNIQUE constraint ensures that all values in a column are distinct.
Example i have created a Table:-

CREATE TABLE UNIQUE_EXAMPLE
(
    ID INT UNIQUE,
    Name VARCHAR(50)
)


Now I am inserting data in UNIQUE_TABLE:-

INSERT INTO UNIQUE_EXAMPLE values ('1','Mohit');
INSERT INTO UNIQUE_EXAMPLE values ('2','Rahul');
INSERT INTO UNIQUE_EXAMPLE values ('3','Deepak');



If I use Select command, I will get following Output:-


But if i use following insert command, I will get Error bacause i can't insert duplicate data in ID field.

INSERT INTO Customer UNIQUE_EXAMPLE('3','Monu');



Happy Coding:)

Comments (no comments yet)

  • :*
  • :*
  • :
 *

Top Posts