Follow us: Connect on YouTube 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 :

1 comments:

  1. Hi There,


    Thank you! Thank you! Thank you! Your blog was a total game changer!

    I try to calculate MAT (Moving Annual Total) in my Finance Multi-dimensional cube with the below. It is not working or not calculating. But when I replace the Aggregate with Sum, it worked but wrong numbers. Need your help on finding the work around. I tried couple of options from internet

    Once again thanks for your tutorial.


    Thanks,
    Nithya

    ReplyDelete