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 :

3 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
  2. Hello There,


    In total awe…. So much respect and gratitude to you folks for pulling off such amazing blogs without missing any points on the SQL Create Table Primary Key. Kudos!
    I am facing an issue with DBMS_CHAIN execution.

    Issue :Even one of the step got Failed, remaining steps were processed with out waiting for depended step SUCCESS.

    In the below run_details , you can see STEP2 is in failed state but further steps(i.e. STEP3 of CHAIN1 and STEP1 , STEP2 of CHAIN2 got processed)

    Please suggest me on this(what I need to do to hold at FAILED step and how can i RERUN that FAILED step only (or) from Failed Steps).
    I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.


    Many Thanks,
    Jennifa

    ReplyDelete
  3. Hi There,

    This is indeed great! But I think perhaps you are generally referring SQL Create Table Primary Key which is getting unsustainable.

    Can anyone suggest on TEMP table concept. I have below requirement and I tried the below but I am getting error.

    select partyno into #tmptbl
    from partym --------- when I executed this I got ORA-00911: invalid character error.

    my requirement is :
    I need to execute 3 queries one by one and hold the result of the first query and second query to feed as input to 3rd query. at the end of the job I need to delete first query and second query result.

    It was cool to see your article pop up in my google search for the process yesterday. Great Guide.
    Keep up the good work!

    Gracias,
    Preethi.

    ReplyDelete