Tuesday, March 18, 2014

What is the difference between "in line" and "out of line" constraints?

The following examples create an "in line" constraint, since they both declare a constraint in the columns' definitions:

CREATE TABLE STAFF (
  PERSONELL_ID NUMBER PRIMARY KEY, 
  LNAME VARCHAR2(30),
  FNAME VARCHAR2(20)
);

An anonymous primary key constraint with a system generated name is created on the column PERSONELL_ID.
Optinally, give the constraint a specific name:

CREATE TABLE STAFF (
  PERSONELL_ID NUMBER CONSTRAINT STAFF_PK PRIMARY KEY, 
  LNAME VARCHAR2(30),
  FNAME VARCHAR2(20)
);

The following examples create an "out of line" constraint, since they are both declared separately from the columns' definitions:

CREATE TABLE STAFF (
  PERSONELL_ID NUMBER, 
  LNAME VARCHAR2(30),
  FNAME VARCHAR2(20),
  PRIMARY KEY (PERSONELL_ID)
);

An anonymous primary key constraint with a system generated name is created on the column PERSONELL_ID.
Optinally, give the constraint a specific name:

CREATE TABLE STAFF (
  PERSONELL_ID NUMBER, 
  LNAME VARCHAR2(30),
  FNAME VARCHAR2(20),
  CONSTRAINT STAFF_PK PRIMARY KEY (PERSONELL_ID)
);
The "ALTER TABLE" equivalent of the "in line" syntax is:
ALTER TABLE STAFF 
MODIFY PERSONELL_ID PRIMARY KEY;
Or with explicitly named constraint:
ALTER TABLE STAFF 
MODIFY PERSONELL_ID CONSTRAINT STAFF_ID PRIMARY KEY;

The "ALTER TABLE" equivalent of the "out of line" syntax is:
ALTER TABLE STAFF
ADD PRIMARY KEY (PERSONELL_ID);
Or with explicitly named constraint:
ALTER TABLE STAFF
ADD CONSTRAINT STAFF_PK PRIMARY KEY(PERSONELL_ID);
Note that NOT NULL constraints cannot be created "out of line".

No comments:

Post a Comment