Saturday, 7 October 2017

PRIMARY KEY Constraint

  • A PRIMARY KEY is the candidate key that is used by the database designer for identifying an entity.
  • There is no fiex rule for choosing the primary key, but generally it is the key that can be controlled by the user.
  • Primary Key columns do not accept null values
  • Primary Key columns do not accept duplicate values, that mean uniqueness of data.
  • Oracle recommended Primary Key columns should be short and numeric.
  • Primary Key constraint is a combination of NOT NULL+UNIQUE.

Restrictions :
  • A table or view can have only one primary key.
  • A composite primary key cannot have more than 32 columns.
  • the same column or combination of columns cannot be declare as  primary key and a unique key.
  • Primary key con't be implemented on columns having  LOB, LONG, VARRAY, NESTED TABLE, OBJECT, TIMESTAMP WITH TIME ZONE.
Syntax :

Column Level PRIMARY KEY Constraint :

CREATE TABLE <TABLE_NAME>
(COLUMN_NAME1 <DATA_TYPE>(WIDTH) ,
 COLUMN_NAME2 <DATA_TYPE>(WIDTH) CONSTRAINT Constraint_Name PRIMARY KEY,
 COLUMN_NAMEn <DATA_TYPE>(WIDTH));


Table Level PRIMARY KEY Constraint :

CREATE TABLE <TABLE_NAME>
(COLUMN_NAME1 <DATA_TYPE>(WIDTH) ,
 COLUMN_NAME2 <DATA_TYPE>(WIDTH),
 COLUMN_NAMEn <DATA_TYPE>(WIDTH),
 CONSTRAINT Constraint_Name PRIMARY KEY(Column_name));

Composite PRIMARY KEY Constraint (always declare in Table level):

CREATE TABLE <TABLE_NAME>
(COLUMN_NAME1 <DATA_TYPE>(WIDTH) ,
 COLUMN_NAME2 <DATA_TYPE>(WIDTH),
 COLUMN_NAMEn <DATA_TYPE>(WIDTH),
 CONSTRAINT Constraint_Name PRIMARY KEY(Column_name1,Column_name2));


See The Example:








No comments:

Post a Comment