Docsity

Data Integrity Constraints - Buisness Management - Lecture Notes, Study notes of Business Administration

Toggle sidebar
bg1
Oracle - Practical 11
Page 1 of 5
CONSTRAINTS
Data Integrity & Constraints:
Constraints are concerned with restricting the values that are allowed in a column within a
table. Constraints are used:
By ORACLE to enforce rules at table level whenever a row is inserted, updated or deleted
from that table. The constraint must be satisfied for that the operation to succeed.
To prevent the deletion of a table if there are dependencies from other tables.
Allow or restrict null values.
Ensure unique values for columns.
Ensure a primary identifying value for each row in a table.
There are 2 types of constraints:
Table constraints: Apply to one of more columns within a table.
Column constraints: Apply to a single column within a table and are
specified when a table is created.
The syntax for constraints varies slightly depending upon whether the constraint is a column
or table constraint.
Constraint names:
Constraint names must be unique to the owner
The constraint name appears in messages when the constraint is violated.
If a constraint name is not specified, the Oracle Server assigns a unique name
with the format SYS_C00n.
A suggested format for naming a constraint is
TABLENAME_CONSTRAINT TYPE_COLNAME e.g. CARS_NN_REGNO
NOT NULL CONSTRAINT:
The NOT NULL constraint ensures that NULL values are not permitted for columns, since
they serve as keys for operations on this table. Columns without the NOT NULL constraint
allow NULL values. NULL is the default, therefore NOT NULL must be specified when creating
a table, if required :
CREATE TABLE CARS
(REG_NO VARCHAR2(20) NOT NULL,
MAKE VARCHAR2(20) NOT NULL,
MODEL VARCHAR2(20),
OWNER VARCHAR2(20),
LAST_SERVICE DATE);
Any statement that causes a designated column to contain a NULL is rolled back and an error
is raised.
UNIQUE CONSTRAINT:
The unique constraint designates a column or combination of columns as a unique key. No
two rows in the table can have the same value for this key. NULLS are allowed if the unique
key is based on a single column.
Unique constraints may be column level, if based on a single column, or table level if based
on more than one column.
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Data Integrity Constraints - Buisness Management - Lecture Notes and more Study notes Business Administration in PDF only on Docsity!

Page 1 of 5

CONSTRAINTS

Data Integrity & Constraints:

Constraints are concerned with restricting the values that are allowed in a column within a table. Constraints are used:  By ORACLE to enforce rules at table level whenever a row is inserted, updated or deleted from that table. The constraint must be satisfied for that the operation to succeed.  To prevent the deletion of a table if there are dependencies from other tables.  Allow or restrict null values.  Ensure unique values for columns.  Ensure a primary identifying value for each row in a table.

There are 2 types of constraints:  Table constraints: Apply to one of more columns within a table.

 Column constraints: Apply to a single column within a table and are specified when a table is created.

The syntax for constraints varies slightly depending upon whether the constraint is a column or table constraint.

Constraint names:  Constraint names must be unique to the owner  The constraint name appears in messages when the constraint is violated.  If a constraint name is not specified, the Oracle Server assigns a unique name with the format SYS_C00n.  A suggested format for naming a constraint is TABLENAME_CONSTRAINT TYPE_COLNAME e.g. CARS_NN_REGNO

NOT NULL CONSTRAINT:

The NOT NULL constraint ensures that NULL values are not permitted for columns, since they serve as keys for operations on this table. Columns without the NOT NULL constraint allow NULL values. NULL is the default, therefore NOT NULL must be specified when creating a table, if required :

CREATE TABLE CARS

(REG_NO VARCHAR2(20) NOT NULL,

MAKE VARCHAR2(20) NOT NULL,

MODEL VARCHAR2(20),

OWNER VARCHAR2(20),

LAST_SERVICE DATE);

Any statement that causes a designated column to contain a NULL is rolled back and an error is raised.

UNIQUE CONSTRAINT:

The unique constraint designates a column or combination of columns as a unique key. No two rows in the table can have the same value for this key. NULLS are allowed if the unique key is based on a single column.

Unique constraints may be column level, if based on a single column, or table level if based on more than one column.

Page 2 of 5

Syntax: CONSTRAINT constraint_name UNIQUE (column, column,…)

Table Unique Constraint: To ensure that there are never two identical owners of the same car:

CREATE TABLE CARS

(REG_NO VARCHAR2(20) NOT NULL,

MAKE VARCHAR2(20) NOT NULL,

MODEL VARCHAR2(20),

OWNER VARCHAR2(20),

LAST_SERVICE DATE,

CONSTRAINT CARS_UK_REGNO_OWN UNIQUE

(REG_NO, OWNER));

Column Unique Constraint: To ensure that the registration number for each car is unique:

CREATE TABLE CARS

(REG_NO VARCHAR2(20) CONSTRAINT

CARS_UK_REGNO UNIQUE,

MAKE VARCHAR2(20) NOT NULL,

MODEL VARCHAR2(20),

OWNER VARCHAR2(20),

LAST_SERVICE DATE);

An index is automatically created to enforce the unique constraint. The name of the index is the same as that of the constraint.

CHECK CONSTRAINT:

The CHECK constraint explicitly defines a condition that each row must satisfy.

Syntax:

COLUMN CONSTRAINT constraint_name CHECK (condition),….

For example to ensure that the MAKE column is always entered in upper case, we can alter the table as follows:

ALTER TABLE CARS

ADD CONSTRAINT CARS_CHECK_MAKE

CHECK(MAKE=UPPER(MAKE));

OR

ALTER TABLE CARS

ADD CHECK (MAKE=UPPER(MAKE));

You could also define a check constraint on the MAKE column to ensure only certain makes are entered e.g. ALTER TABLE CARS ADD CONSTRAINT CARS_CHECK_MAKE CHECK(make IN (‘OPEL’,’MAZDA’,’FIAT’));

  • Page 4 of

Page 5 of 5

FOREIGN KEY CONSTRAINT:

The foreign key constraint provides referential integrity rules either within a table or between tables. What this means is that you can only place a value in table B if the values exists as a primary key in TABLE A.

Syntax: CONSTRAINT constraint_name FOREIGN KEY (column, column,…) REFERENCES table (column, column,…)

Table Primary Key Constraint:

For example, say we have created an employee table and a department table:

CREATE TABLE EMPLOYEE

(EMP_NO NUMBER(4) NOT NULL,

EMP_NAME VARCHAR2(20),

JOB VARCHAR2(20),

DEPT_NO NUMBER(2));

CREATE TABLE DEPARTMENT

(DEPT_NO NUMBER(2) CONSTRAINT dept_pk_deptno PRIMARY KEY, DEPT_NAME VARCHAR2(20));

Now we need to ensure that for each DEPT_NO in the EMPLOYEE table, there is a matching DEPT_NO in the DEPARTMENT table.

ALTER TABLE EMPLOYEE

ADD CONSTRAINT EMP_FK_DEPTNO FOREIGN KEY (DEPT_NO)

REFERENCES DEPARTMENT(DEPT_NO);

or

ALTER TABLE EMPLOYEE

ADD FOREIGN KEY (DEPT_NO) REFERENCES DEPARTMENT(DEPT_NO);

ADDING/DISABLING CONSTRAINTS:

In most of the examples so far we have shown how a constraint is added to a table or column when creating the table. However a constraint can be added after the table has been created.

When you add a constraint to a table or column this it is automatically enabled, unless it is stated otherwise.

We use the ALTER TABLE command to disable and re-enable constraints.

ALTER TABLE CARS

DISABLE CONSTRAINT CHECK_MAKE;

ALTER TABLE CARS

ENABLE CONSTRAINT CHECK_MAKE;