Friday, 12 May 2017

What is Primary Key?

In this post, I will explain What is Primary Key?

Primary Key provides uniqueness for a column or set of columns in a table and used to identify a row (a record) in a table. A database table can have only one Primary Key and a column with a Primary Key doesn't allow NULL value (Not nullable). Primary Key generates a Unique Clustered Index.

Example of Primary Key-

Create Primary Key while creating a table
CREATE TABLE tbl_Employee
(
    EmpId INT NOT NULL,
    EmpName VARCHAR(100) NULL,
    CONSTRAINT PK_EmpId PRIMARY KEY (EmpId)
)

Create Primary Key using alter table statement
ALTER TABLE tbl_Employee
ADD CONSTRAINT PK_EmpId PRIMARY KEY (EmpId)

Drop Primary Key using alter table statement
ALTER TABLE tbl_Employee
DROP CONSTRAINT  PK_EmpId

 

0 comments:

Post a Comment

Please do not enter any spam link in the message box.