In Oracle, Primary key is the column which has unique values, and it cannot be null. In a table, there can be only one primary key.
Primary Key – Using CREATE TABLE statement
Syntax:
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n)
);
Example
CREATE TABLE Test2(ID Number, NAME Varchar2 CONSTRAINT test2_pk PRIMARY KEY (ID));
Primary Key – Using ALTER TABLE statement
Syntax:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);
Example
ALTER TABLE student ADD CONSTRAINT student_pk PRIMARY KEY(id);
Drop Primary Key
Syntax:
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;
Example
ALTER TABLE student DROP CONSTRAINT student_pk ;
Disable Primary Key
Syntax:
ALTER TABLE table_name
DISABLE CONSTRAINT constraint_name;
Example
ALTER TABLE student DISABLE CONSTRAINT student_pk ;
Enable Primary Key
Syntax:
ALTER TABLE table_name
ENABLE CONSTRAINT constraint_name;
Example
ALTER TABLE student ENABLE CONSTRAINT student_pk ;
Leave a Reply