User Id :    Password :      New Member   Forgot Password
 
How to create primary key on a table
Description In this codesnippet I will explain how you can create a primary key on a new as wel as on an existing table.   No. of Views     1448
  Rating     0
Author Anshu Rastogi   Posted On     11 Oct 2010
Tags SQL SERVER,SQL SERVER : How To    

Sample Code   Download Code

Primary Key : A primary key is a constaints on a column which enforce uniqly identifies records in the table in the database.

  • A primary key must be unique and can not be NULL.
  • A table can have only primary key.
To create primary key on new table 
CREATE TABLE myTable
(
EmpId int NOT NULL PRIMARY KEY,
FirstName varchar(50) NOT NULL,
EmpLastName varchar(50),
ProjectId INT
)
To create primary on an existing table need to do following 
ALTER TABLE myTable ADD PRIMARY KEY (EmpId)

 

About Author

About Author N/A Anshu
No Photo
 
Country N/A
Company NetEdge Computing Solutions
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

     
Write your comment here.  
Comment
Verification Code   
  
    
 
Section sponsored by