SQL PRIMARY KEY

Posted on Updated on

SQL PRIMARY KEY Constraint

SQL PRIMARY KEY apply on column(s) for a uniquely identifies each record in the table. Unique identify each row in the table.

SQL Primary Key constraint has been specified for certain column. we ca not enter duplicate data in this column.

SQL Primary Key in a table have following three special attributes,

  • The NOT NULL attribute is automatic active.
  • The data across the column must be unique.
  • Defines column as a mandatory column.

The PRIMARY KEY constraint unambiguously identifies every record in a very info table. Primary keys should contain distinctive values.

A primary key column cannot contain NULL values.

Most tables ought to have a primary key ,  and every table will have just one primary key.

The following SQL creates a PRIMARY KEY on the “P _ Id” column once the “Persons” table is created:

CREATE TABLE Persons

(

P _ Id int NOT NULL ,

LastName varchar ( 255 ) NOT NULL ,

FirstName varchar ( 255 ) ,

Address varchar ( 255 ) ,

City varchar ( 255 ) ,

PRIMARY KEY ( P _ Id )

)

SQL Server / Oracle / MS Access:

CREATE TABLE Persons

(

P _ Id int NOT NULL PRIMARY KEY ,

LastName varchar ( 255 ) NOT NULL ,

FirstName varchar ( 255 ) ,

Address varchar ( 255 ) ,

City varchar ( 255 )

)

To allow naming of a PRIMARY KEY constraint , and for outlining a PRIMARY KEY constraint on multiple columns , use the subsequent SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

CREATE TABLE Persons

(

P _ Id int NOT NULL ,

LastName varchar ( 255 ) NOT NULL ,

FirstName varchar ( 255 ) ,

Address varchar ( 255 ) ,

City varchar ( 255 ) ,

CONSTRAINT pk _ PersonID PRIMARY KEY ( P _ Id , LastName )

)

To create a PRIMARY KEY constraint on the “P _ Id” column once the table is already created , use the subsequent SQL:

ALTER TABLE Persons

ADD PRIMARY KEY ( P _ Id )

To allow naming of a PRIMARY KEY constraint , and for outlining a PRIMARY KEY constraint on multiple columns , use the subsequent SQL syntax:

ALTER TABLE Persons

ADD CONSTRAINT pk _ PersonID PRIMARY KEY ( P _ Id , LastName )

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s