



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




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
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 :
Any statement that causes a designated column to contain a NULL is rolled back and an error is raised.
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.
Syntax: CONSTRAINT constraint_name UNIQUE (column, column,…)
Table Unique Constraint: To ensure that there are never two identical owners of the same car:
Column Unique Constraint: To ensure that the registration number for each car is unique:
An index is automatically created to enforce the unique constraint. The name of the index is the same as that of the 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:
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’));
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:
(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.
or
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.