Friday, 3 August 2018

SQL Server Primary Key

Primary Key maybe combination of single or multiple column , that define uniqueness of value in a table.

           Primary key guaranteed that no duplicate value should we enter in the table. While inserting data into the table, primary key check that same value already is there or not, if same value exits , primary key prevent entering the data and raise an error message. 


The as primary key create an unique record for a table. Primary key does not allow null , a null is not a value or absent of  value. If you create primary key with the help of combination multiple column it is called composite primary key


Below is the syntax of creating primary key. 

CREATE TABLE table_name
(
  column1 datatype primary key,
  column2 datatype NULL/NOT NULL,
  column3 datatype NULL/NOT NULL,
 
);

 


Below a table is created with primary key.


create table myTable
(
ID int identity primary key ,
Name Varchar(50),
Roll Varchar(50),
)

Below a table is created with multiple primary key. 



CREATE TABLE STUDENT
(
ID int ,
Roll int,
RegNo int,
Name Varchar(50)
PRIMARY KEY (ID, Roll, RegNo)
)

Below is the example of alter table with primary key. 



CREATE TABLE STUDENT
(
ID int NOT NULL ,
Roll int,
RegNo int,
Name Varchar(50)
)
GO

alter table STUDENT add primary key (ID)


Below is the example of script to drop primary key from a Table.



ALTER TABLE STUDENT
DROP PRIMARY KEY

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...