All Type Coding

Search Here

Primary Key in sql server.

PRIMARY KEY

The PRIMARY KEY constraint is used to uniquely identifies each record in a database table. A primary key column cannot contain NULL values. A table can have only one primary key, which may consist of single or multiple fields. When multiple fields are used as a primary key, they are called a composite key.Primary keys must contain unique values.
Now Let's create a table test and we'll see how to set primary key on table column
CREATE TABLE Test(
       ID   INT              NOT NULL,
       NAME VARCHAR (30)     NOT NULL,
       AGE  INT              NOT NULL,
       ADDRESS  CHAR (250) ,
       SALARY   DECIMAL (18, 2),       
       PRIMARY KEY (ID)
);

How to create a PRIMARY KEY constraint on any  column of a table when  table already exists, 
use the following SQL syntax:

ALTER TABLE Table_name  ADD PRIMARY KEY (Column_name);
For Example:
Alter table Test Add PRIMARY KEY(ID)





No comments :

Post a Comment