Oracle SQL Constraints
Oracle SQL Constraints
Yanamala
INTEGRITY CONSTRAINTS
Integrity Constraints are used to prevent the entry of invalid information into
tables. There are five Integrity Constraints Available in Oracle. They are :
• Not Null
• Primary Key
• Foreign Key
• Check
• Unique
Not Null
By default all columns in a table can contain null values. If you want to ensure
that a column must always have a value, i.e. it should not be left blank, then
define a NOT NULL constraint on it.
Primary Key
Each table can have one primary key, which uniquely identifies each row in a
table and ensures that no duplicate rows exist. Use the following guidelines when
selecting a primary key:
• Whenever practical, use a column containing a sequence number. It is a
simple way to satisfy all the other guidelines.
• Minimize your use of composite primary keys. Although composite primary
keys are allowed, they do not satisfy all of the other recommendations. For
example, composite primary key values are long and cannot be assigned
by sequence numbers.
• Choose a column whose data values are unique, because the purpose of
a primary key is to uniquely identify each row of the table.
• Choose a column whose data values are never changed. A primary key
value is only used to identify a row in the table, and its data should never
be used for any other purpose. Therefore, primary key values should
rarely or never be changed.
• Choose a column that does not contain any nulls. A PRIMARY KEY
constraint, by definition, does not allow any row to contain a null in any
column that is part of the primary key.
• Choose a column that is short and numeric. Short primary keys are easy
to type. You can use sequence numbers to easily generate numeric
primary keys.
For example in EMP table EMPNO column is a good candidate for PRIMARY
KEY.
To define a primary key on a table give the following command.
The above command will succeed only if the existing values are compliant i.e. no
duplicates are there in EMPNO column. If EMPNO column contains any
duplicate value then the above command fails and Oracle returns an error
indicating of non compliant values.
FOREIGN KEY
On whichever column you put FOREIGN KEY constraint then the values in that
column must refer to existing values in the other table. A foreign key column can
refer to primary key or unique key column of other tables. This Primary key and
foreign key relationship is also known as PARENT-CHILD relationship i.e. the
table which has Primary Key is known as PARENT table and the table which has
foreign key is known as CHILD table. This relationship is also known as
REFERENTIAL INTEGRITY.
• You cannot delete a parent record if any existing child record is there. If
you have to first delete the child record before deleting the parent record.
In the above example you cannot delete row of employee no. 101 since
it’s child exist in the ATTENDANCE table. However, you can delete the
row of employee no. 103 since no child record exist for this employee in
ATTENDANCE table. If you define the FOREIGN KEY with ON DELETE
CASCADE option then you can delete the parent record and if any child
record exist it will be automatically deleted.
In Oracle 9i oracle has also given a new feature i.e. ON DELETE SET NULL .
That is it sets the value for foreign key to null whenever the parent record is
deleted.
To define a foreign key constraint with ON DELETE SET NULL option give
the following command.
• You also cannot drop the parent table without first dropping the FOREIGN
KEY constraint from attendance table. However if you give CASCADE
CONSTRAINTS option in DROP TABLE statement then Oracle will
automatically drop the references and then drops the table.
CHECK
Use the check constraint to validate values entered into a column. For example
in the above ATTENDANCE table, the DAYS column should not contain any
value more than 31. For this you can define a CHECK constraint as given below
Similarly if you want the salaries entered in to SAL column of employee table
should be between 1000 and 20000 then you can define a CHECK constraint on
EMP table as follows
Alter table EMP add constraint sal_check
Check (Sal between 1000 and 20000);
You can define as many check constraints on a single column as you want there
is no restrictions on number of check constraints.
UNIQUE KEY
Unique Key constraint is same as primary key i.e. it does not accept duplicate
values, except the following differences
there can be only on Primary key per table. Whereas, you can have
as many Unique Keys per table as you want.
Primary key does not accept NULL values whereas, unique key
columns can be left blank.
You can also refer to Unique key from foreign key of other tables.
Again the above command will execute successfully if IDNO column contains
complying values otherwise you have to remove non complying values and then
add the constraint.
DEFAULT
You can also specify the DEFAULT value for columns i.e. when user does not
enter anything in that column then that column will have the default value. For
example in EMP table suppose most of the employees are from Hyderabad, then
you can put this as default value for CITY column. Then while inserting records if
user doesn’t enter anything in the CITY column then the city column will have
Hyderabad.
To define default value for columns create the table as given below
create table emp (empno number(5),
name varchar2(20),
sal number(10,2),
city varchar2(20) default ‘Hyd’);
Then the city column will have value ‘Bom ‘. But when user inserts a record like
this
Then the city column will have value ‘Hyd’. Since it is the default.
Examples
The name of the constraints are optional. If you don’t define the names then
oracle generates the names randomly like ‘SYS_C1234’
You may wish to defer constraint checks on UNIQUE and FOREIGN keys if the
data you are working with has any of the following characteristics:
When dealing with bulk data being manipulated by outside applications, you can
defer checking constraints for validity until the end of a transaction.
After you have identified and selected the appropriate tables, make sure their
FOREIGN, UNIQUE and PRIMARY key constraints are created deferrable. You
can do so by issuing a statement similar to the following:
You can check for constraint violations before committing by issuing the SET
CONSTRAINTS ALL IMMEDIATE statement just before issuing the COMMIT. If
there are any problems with a constraint, this statement will fail and the
constraint causing the error will be identified. If you commit while constraints are
violated, the transaction will be rolled back and you will receive an error
message.
For example to disable primary key of EMP table give the following statement
Dropping constraints.
You can drop constraint by using ALTER TABLE DROP constraint statement.
For example to drop Unique constraint from emp table, give the following
statement
The above statement will succeed only if the foreign key is first dropped
otherwise you have to first drop the foreign key and then drop the primary key. If
you want to drop primary key along with the foreign key in one statement then
CASCADE CONSTRAINT statement like this
To see information about constraints, you can query the following data dictionary
tables.
select * from user_constraints;
select * from user_cons_columns;