Tuesday, 13 March 2012

How To Create Primary key In Sql Server 2008

Primary Key:

      Primary Key Means Unique And Not Null.

      A primary key is a special case of unique key. Primary keys may consist of a single attribute
 or multiple attributes in combination. And it’s unique, what the difference from the unique key is
 the primary key cannot be NULL, and primary keys must be defined by using another syntax.

Syntax:

  CREATE TABLE student
  (
      Student_Id int NOT NULL PRIMARY KEY,
      LastName varchar(255) NOT NULL,
      FirstName varchar(255),
      Address varchar(255),
      City varchar(255)
    )

How to Add Primary Key For Existing table:


             ALTER TABLE Persons
             ADD PRIMARY KEY (Person_Id)
 
     Here Persons is the Table Name And Person_Id is the Column name.

How to DROP a PRIMARY KEY Constraint:

          ALTER TABLE Persons
          DROP CONSTRAINT pk_Person_Id

No comments:

Post a Comment