Primary Key

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:

snippet
CREATE TABLE table_name
(
  column1 datatype null/not null,
  column2 datatype null/not null,
  ...
  CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n)
);

Example

snippet
CREATE TABLE Test2(ID Number, NAME Varchar2 CONSTRAINT test2_pk PRIMARY KEY (ID));
Primary Key in Oracle

Primary Key - Using ALTER TABLE statement

Syntax:

snippet
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);

Example

snippet
ALTER TABLE student ADD CONSTRAINT student_pk PRIMARY KEY(id);
Primary Key in Oracle Using Alter Table

Drop Primary Key

Syntax:

snippet
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;

Example

snippet
ALTER TABLE student DROP CONSTRAINT student_pk ;
Primary Key in Oracle Drop Primary Key

Disable Primary Key

Syntax:

snippet
ALTER TABLE table_name
DISABLE CONSTRAINT constraint_name;

Example

snippet
ALTER TABLE student DISABLE CONSTRAINT student_pk ;
Primary Key in Oracle Disable Primary Key

Enable Primary Key

Syntax:

snippet
ALTER TABLE table_name
ENABLE CONSTRAINT constraint_name;

Example

snippet
ALTER TABLE student ENABLE CONSTRAINT student_pk ;
Primary Key in Oracle Enable Primary Key
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +