Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

now we discuss 'how to create a primary key using sql while we are creating a

table?'

THIS IS THE ONLY DEMO FOR SET THE PRIMARY KEY TO ANY ONE COLUMN..

CREATE TABLE STUDENT (


ROLL_NO NUMBER(3) PRIMARY KEY,
NAME VARCHAR2(30),
ADDRESS VARCHAR2(30),
PHONE_NO NUMBER(10),
APPLICATION_NO NUMBER(20)
);

HOW TO GIVE A NAME TO THE PRIMARY KEY WHILE CREATING A TABLE


THIS IS THE SAME AS ABOVE BUT THERE IS SOME DIFFERENCES..

CREATE TABLE STUDENT (


ROLL_NO NUMBER(3),
NAME VARCHAR2(30),
ADDRESS VARCHAR2(30),
PHONE_NO NUMBER(10),
APPLICATION_NO NUMBER(20),
CONSTRAINT ID_PK PRIMARY KEY(ROLL_NO)
);

this work is always done in the end of querying all the columns..

:: here "CONSTRAINT 'primary_key_name' PRIMARY KEY('Column_Name')"

above is the syntax and also the example of declaration of primary key with given
name.
here don't use the quotes. the quotes are here to highlight the main tasks.

'constraint' is a keyword and which field we want as the primary key field, we have
to write it
inside the the primary key's parantheses (i.e. '()')..

now how to delete the primary key functionality from the given field??

:: alter table student drop constraint id_pk;

the primary key is a constraint and we have given the name 'id_pk' to this
constraint primary key...
therefore we are deleting the constraint named id_pk ...

now how to assign the primary key to any field after creating the table..

:: alter table "EMPS" modify ("EMP_ID" NUMBER(3,0) PRIMARY KEY);


//only for assigning primary key
OR
:: alter table demossemp modify (emp_id number(3) , constraint id_pk primary
key(emp_id));
//for assigning primary key with a name
the dropping command of the above primary key is
:: alter table demossemp drop constraint id_pk;

You might also like