Follow us: Subscribe via RSS Feed Connect on YouTube Connect on YouTube

Pages

Tuesday, 27 June 2017

SQL Create Table Primary Key



This is used to avoid duplicates and nulls. This will work as combination of unique and not null. Primary key always attached to the parent table.

We can add this constraint in all three levels.

Example:

TABLE LEVEL PRIMARY KEY


SQL>CREATE TABLE student
(
   no      NUMBER (2),
   name    VARCHAR (10),
   marks   NUMBER (3),
   PRIMARY KEY (no)
);
SQL>CREATE TABLE student
(
   no      NUMBER (2),
   name    VARCHAR (10),
   marks   NUMBER (3),
   CONSTRAINT pk PRIMARY KEY (no)
);

COLUMN LEVEL PRIMARY KEY     



SQL>CREATE TABLE student
(
   no      NUMBER (2) PRIMARY KEY,
   name    VARCHAR (10),
   marks   NUMBER (3)
);
SQL>CREATE TABLE student
(
   no      NUMBER (2) CONSTRAINT pk PRIMARY KEY,
   name    VARCHAR (10),
   marks   NUMBER (3)
); un UNIQUE(no);
          

ALTER TABLE ADD PRIMARY KEY


SQL> ALTER TABLE student ADD PRIMARY KEY(no);
         SQL> ALTER TABLE student ADD CONSTRAINT pk PRIMARY KEY(no); 
Share this article :

0 comments:

Post a Comment