Add Primary Key Constraint example

Submitted by:David Villa

Date added:10 September, 2011

Category:PL SQL

Example code showing how to add a primary constraint. This Oracle constraint is used to identify the primary key for a table.

A primary key is a single field or combination of fields that uniquely defines a record. None of the fields that are part of the primary key can contain a null value. A table can have only one primary key.

This operation requires that the primary columns are unique, and this Oracle constraint will create a unique index on the target primary key.

Tags: primary key constraint

Code Snippet:

CREATE TABLE table_name
(column1 datatype NULL/NOT NULL,
column2 datatype NULL/NOT NULL,
...
CONSTRAINT constraint_name PRIMARY KEY (column1, column2, . column_n)
);


spool pkeys.LOG

PROMPT Adding PRIMARY CONSTRAINT TO CT_ADD_NAMES TABLE

ALTER TABLE OPS$SQLTIME.CT_ADD_NAMES
DROP PRIMARY KEY
/

ALTER TABLE OPS$SQLTIME.CT_ADD_NAMES
ADD CONSTRAINT CT_ADD_NAMES_PK PRIMARY KEY (CT_ID_NO)
USING INDEX
TABLESPACE CTIDX
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (INITIAL 4096K NEXT 512K MINEXTENTS 1 MAXEXTENTS 100 PCTINCREASE 0 FREELISTS 1)
/

spool off
 
 

Comments