Monday, 20 August 2012

Primary Key

Primary Key
It is one or more columns in a table which is uniquely identified the rows of the table.

§       The primary key is the columns used to uniquely identify each row of a table. A table can have only one primary key.No primary key value can appear in more than one row in the table.
      
    Simply you can make any column as a primary key by using this code........
     Create Table testing(ID int not Null  primary key, name varchar(29),Fathername nvarchar(30)
    )
   
   
    But if you make this type of table and try to make a primary key in that table.........
   
    create table PanakjTesting
( Pid int , Name nvarchar(30),FatherName nvarchar(50),class nvarchar(10),Rollnumber numeric)

    And then we will try Alter the table to make a primary key so we will fire this query ...
    
    Alter table PanakjTesting
  add primary key(pid)
   
   Then it will show this error when we are making primary key in this table
  Error:  ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'ID' cannot be added to non-empty table ' PanakjTesting ' because it does not satisfy these conditions.

   Means if any table does not have any column as unique or not null constraint you can not alter that table to make primary key.
  
   Now we will talk about the Null value or Zero value.
   So Primary key can accept the 0 but can  not accept null value....
  I tested it practically ! if there is no identity the PRIMARY KEY WILL ACCEPT 0! BUT NOT NULL
  insert into PanakjTesting values('0','Panakj','Papa','MCA','02457265345')

    But if you set the identity to start primary key from a particular value then it will not accept.

   

  NOTE:-If YOu have any question or doubt then please send me that on my mail id:kushwaha.pankajkumar07@gmail.com

No comments:

Post a Comment