CONSTRAINTS

CONSTRAINTS

The oracle server uses constraints to prevent invalid data entry into tables. Constraints prevent the deletion of a table if there are dependencies. The following constraints types are valid :
(1) NOT NULL
(2) UNIQUE
(3) PRIMARY KEY
(4) FOREIGN KEY
(5) CHECK


DATA CONSTRAINTS


Rules which are enforced on data being entered, and prevents the user from entering invalid data into tables are called CONSTRAINTS. Thus, constraints super control data being entered in tables for permanent storage.

Oracle permits data constraints to be attached to table columns via SQL syntax that will check data for integrity. Once data constraints are part of a table column construction, the Oracle engine checks the data being entered into a table column against the data constraints. If the data passes this check, it is stored in the table fails a constraint, the entire record is rejected and not stored in the table.

Once a constraint is attached to a table column, any SQL insert or update statement causes these constraints to be applied to the data prior to it being inserted into the tables for storage.

TYPES OF DATA CONSTRAINTS

I / O CONSTRAINTS

This data constraint determines the speed at which data can be inserted or extracted from an Oracle table.

The input / output constraint, is further divided into two distinctly different constraints.
Primary Key Constraint
Foreign Key Constraint
In addition to primary and foreign key, Oracle has NOT NULL and UNIQUE as column constraints.

Constraints can be connected to a column or a table by the CREATE TABLE or ALTER TABLE command.

Oracle allows programmers to define constraints at:
Column level
Table level

COLUMN LEVEL CONSTRAINT

If data constraints are defined along with the column definition when creating or altering a table structure, they are column level constraints.

Column level constraints are applied to the current column. The current column is the column that immediately precedes the constraint i.e. they are local to a specific column. A column level constraint cannot be applied if the data constraint spans across multiple columns in a table.

TABLE LEVEL CONSTRAINT

If data constraints are defined after defining all the table columns when creating or altering a table structure, it is a table level constraint.

Table level constraint must be applied if the data constraint spans across multiple columns in a table. Constraints are stored as a part of the global table definition by the Oracle engine in its system tables.

NULL VALUE CONCEPT

The NULL values are clubbed at the top of the column in the order in which they were entered into the table. A NULL value is different from a blank or a zero.
NULL values are treated specially by Oracle. A NULL value can be inserted into the columns of any data type.

NOT NULL CONSTRAINT
When a column is defined as NOT NULL, then that column becomes a mandatory column. It implies that a value must be entered into the column if the record is to accept for storage in the table.
The NOT NULL constraint can only be applied at column level. Although NOT NULL can be applied as be check constraint, however Oracle recommends that this be not done.

SYNTAX: -
Columnname datatype (size) NOT NULL

EXAMPLE: -
Create a table Emp with the following mandatory Fields:
Empno,Ename,job,sal and deptno columns.
SQL> CREATE TABLE Emp
(Empno number(4) NOT NULL,
Ename varchar2(10),
job varchar2(9),
sal number(7,2),
deptno number(2));


Table created.

THE UNIQUE CONSTRAINT

The purpose of a unique key is to ensure that information in the column(s) is unique, i.e. a value entered in column(s) defined in the unique constraint must not be repeated across the column(s). A table may many UNIQUE KEYS.

The UNIQUE COLUMN constraint permits multiple entries on the NULL value into the column.

UNIQUE constraint defined at the column level

SYNTAX: -
Columnname datatype(size) UNIQUE

THE PRIMARY KEY CONSTRAINT


A primary key is one or more column(s) in a table used to uniquely identify each row in the table.
A primary key column in a table has special attributes:
It defines the column, as a mandatory column i.e. the column cannot be left blank. The NOT NULL attributes are active.
The data held across the column MUST be UNIQUE.


A single column primary key is called as simple key. A multicolumn primary key is called a composite primary key. The only function of a primary key in a table is to uniquely identify a row. Only when a record cannot be uniquely identified using the value in a single column, will a composite primary key be defined.

The data constraint attached to a table column (or columns) ensure:
That the data entered in the table column (or columns) is unique across the entire column (or columns).

PRIMARY KEY constraint defined at the column level


SYNTAX: -

Columnname datatype (size) PRIMARY KEY

PRIMARY KEY constraint defined at the table level

SYNTAX: -

PRIMARY KEY (columnname[, columnname,……])

THE FOREIGN KEY CONSTRAINT


Foreign key represent relationships between tables. A Foreign key is a column whose values are derived from the PRIMARY KEY or UNIQUE KEY of some other table.
The table in which the foreign key is defined is called a FOREIGN TABLE or DETAIL TABLE. The table that defines a PRIMARY KEY or UNIQUE KEY and is referenced by the foreign key is called PRIMARY TABLE or MASTER TABLE.
This constraint establishes a relationship between records across a Master and Detail table.
This relationship ensure:
Records cannot be inserted into a detailed table if corresponding records in the master table do not exist.
Records of the master table cannot be deleted if corresponding records in the detail table exist.
The existence of a foreign key implies that the table with the foreign key is related to the Master table from which the foreign key is derived. A foreign key must have a corresponding PRIMARY KEY or UNIQUE KEY value in the master table.

FOREIGN KEY constraint defined at the column level


SYNTAX: -

Columnname datatype (size) REFERENCES Tablename[(columnname)]
[ON DELETE CASCADE]

FOREIGN KEY constraint defined at the table level

SYNTAX: -
FOREIGN KEY (columnname [, columnname])
REFERENCES tablename [(columnname [, columnname])

DROPPING INTEGRITY CONSTRAINT


We can drop an integrity constraint if the rule that if enforces is no longer true or if the constraints is no longer needed. Drop the constraint using the ALTER TABLE command with DROP clause.

EXAMPLE: -

Drop the PRIMARY KEY constraint from friend

ALTER TABLE friend
DROP PRIMARY KEY;

Drop FOREIGN KEY constraint on column city in table friend.

ALTER TABLE friend
DROP CONSTRAINT city;

Post a Comment

Previous Post Next Post