You are on page 1of 3

Primary Key/Primary Key Constraint:

-----------------------------------
Primary key is consists of two constraint: Unique key cons. and Not-Null cons.
So it means if you create a primary key constraint on any column in a table, tha
t column is bound to have unique and not-null values
CREATE TABLE DEPT
( DEPT_NO numeric(10) not null,
CONSTRAINT DEPT_pk PRIMARY KEY (DEPT_NO)
);

Foreign Key/Foreign Key Constraint:
------------------------------------
Foreing key is used to maintain referential integrity b/w objects in the DB. In
simple words, foreign key is the name of a column in the reference table which
is used (column name: let's say 'dept_no') to create Foreign key constraint on t
he same column ('dept_no') in the master bable.
Using this constraint, the column (on which foreign key constraint has been crea
ted, like: dept_no) of master table is bound to keep value in same range as they
are in the reference column (dept_no) of the reference table.
CREATE TABLE EMP
( EMP_ID numeric(10) not null,
DEPT_NO numeric(10) not null,
CONSTRAINT FK_DEPTNO
FOREIGN KEY (DEPT_NO)
REFERENCES DEPT(DEPT_NO)
);
So here EMP would be master table and DEPT would be reference table. Have a look
at user_constraints and user_cons_columns.
SELECT B.COLUMN_NAME,A.CONSTRAINT_TYPE FROM USER_CONSTRAINTS A, USER_CONS_COLUMN
S B WHERE A.TABLE_NAME = B.TABLE_NAME AND B.TABLE_NAME = 'DEPT' AND A.CONSTRAINT
_NAME = B.CONSTRAINT_NAME;
Here the constraint type P is primary and R is foreign or reference.
============================
constraint
Purpose
Use a constraint to define an integrity constraint--a rule that restricts the va
lues in a database. Oracle Database lets you create six types of constraints and
lets you declare them in two ways.
The six types of integrity constraint are described briefly here and more fully
in "Semantics":
A NOT NULL constraint prohibits a database value from being null.
A unique constraint prohibits multiple rows from having the same value in th
e same column or combination of columns but allows some values to be null. A uni
que constraint is a single field or combination of fields that uniquely defines
a record. Some of the fields can contain null values as long as the combination
of values is unique.
A primary key constraint combines a NOT NULL constraint and a unique constra
int in a single declaration. That is, it prohibits multiple rows from having the
same value in the same column or combination of columns and prohibits values fr
om being null.
NOTE----> In Primaryr Key Constraint, none of the fields that are part of the pr
imary key can contain a null value. However, in Unique Key Constraint, some of t
he fields that are part of the unique constraint can contain null values as long
as the combination of values is unique.

A foreign key constraint requires values in one table to match values in
another table.
A check constraint requires a value in the database to comply with a specifi
ed condition.
A REF column by definition references an object in another object type or in
a relational table. A REF constraint lets you further describe the relationship
between the REF column and the object it references.
You can define constraints syntactically in two ways:
As part of the definition of an individual column or attribute. This is call
ed inline specification.
As part of the table definition. This is called out-of-line specification.
NOT NULL constraints must be declared inline. All other constraints can be decla
red either inline or out of line.
Constraint clauses can appear in the following statements:
CREATE TABLE (see CREATE TABLE)
ALTER TABLE (see ALTER TABLE)
CREATE VIEW (see CREATE VIEW)
ALTER VIEW (see ALTER VIEW )
View Constraints Oracle Database does not enforce view constraints. However, you
can enforce constraints on views through constraints on base tables.
You can specify only unique, primary key, and foreign key constraints on views,
and they are supported only in DISABLE NOVALIDATE mode. You cannot define view c
onstraints on attributes of an object column.
==> Unique Constraints <==
A unique constraint designates a column as a unique key. A composite unique key
designates a combination of columns as the unique key. When you define a unique
constraint inline, you need only the UNIQUE keyword. When you define a unique co
nstraint out of line, you must also specify one or more columns. You must define
a composite unique key out of line.
To satisfy a unique constraint, no two rows in the table can have the same value
for the unique key. However, the unique key made up of a single column can cont
ain nulls. To satisfy a composite unique key, no two rows in the table or view c
an have the same combination of values in the key columns. Any row that contains
nulls in all key columns automatically satisfies the constraint. However, two r
ows that contain nulls for one or more key columns and the same combination of v
alues for the other key columns violate the constraint.
When you specify a unique constraint on one or more columns, Oracle implicitly c
reates an index on the unique key. If you are defining uniqueness for purposes o
f query performance, then Oracle recommends that you instead create the unique i
ndex explicitly using a CREATE UNIQUE INDEX statement. You can also use the CREA
TE UNIQUE INDEX statement to create a unique function-based index that defines a
conditional unique constraint. See "Using a Function-based Index to Define Cond
itional Uniqueness: Example" for more information.
Restrictions on Unique Constraints Unique constraints are subject to the followi
ng restrictions:
None of the columns in the unique key can be of LOB, LONG, LONG RAW, VARRAY,
NESTED TABLE, OBJECT, REF, TIMESTAMP WITH TIME ZONE, or user-defined type. Howe
ver, the unique key can contain a column of TIMESTAMP WITH LOCAL TIME ZONE.
A composite unique key cannot have more than 32 columns.
You cannot designate the same column or combination of columns as both a pri
mary key and a unique key.
You cannot specify a unique key when creating a subview in an inheritance hi
erarchy. The unique key can be specified only for the top-level (root) view.

You might also like