session 11

advertisement
Oracle - Practical 11
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
MAKE
MODEL
OWNER
LAST_SERVICE
VARCHAR2(20) NOT NULL,
VARCHAR2(20) NOT NULL,
VARCHAR2(20),
VARCHAR2(20),
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 1 of 5
Oracle - Practical 11
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
(REG_NO, OWNER));
UNIQUE
Column Unique Constraint:
To ensure that the registration number for each car is unique:
CREATE TABLE
CARS
(REG_NO
MAKE
MODEL
OWNER
LAST_SERVICE
VARCHAR2(20) CONSTRAINT
CARS_UK_REGNO UNIQUE,
VARCHAR2(20) NOT NULL,
VARCHAR2(20),
VARCHAR2(20),
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 2 of 5
Oracle - Practical 11
Note: This constraint only affects new data entered into the table – does not affect any
existing data.
Check constraints cannot (but triggers can) :
 Include a subquery
 Include the SYSDATE, UID, USER functions, because they vary over time.
PRIMARY KEY CONSTRAINT:
As with the unique keys, a primary key constraint enforces uniqueness of the column and a
unique index is automatically created to manage this. There can only be one primary key per
table, and NULLS are not allowed in primary key columns.
We can create a primary key for a table when we create the table or it can be added to the
table after creation by using the ALTER TABLE statement.
Syntax:
CONSTRAINT constraint_name PRIMARY KEY (column, column..)
Table Primary Key Constraint:
CREATE TABLE
CARS
(REG_NO
VARCHAR2(20),
MAKE
VARCHAR2(20),
MODEL
VARCHAR2(20),
OWNER
VARCHAR2(20),
LAST_SERVICE
DATE,
CONSTRAINT
CARS_PK_REGNO_OWN
KEY(REGNO,OWNER));
PRIMARY
Column Primary Key Constraint:
CREATE TABLE
CARS
(REG_NO
MAKE
MODEL
OWNER
LAST_SERVICE
VARCHAR2(20) CONSTRAINT
CARS_PK_REGNO PRIMARY KEY,
VARCHAR2(20),
VARCHAR2(20),
VARCHAR2(20),
DATE);
Using the ALTER TABLE command:
ALTER TABLE CARS
ADD CONSTRAINT CARS_PK_REGNO PRIMARY KEY(REG_NO);
or
ALTER TABLE CARS
ADD CONSTRAINT PRIMARY KEY(REG_NO);
Ideal columns for the primary key :
 Columns whose data values are unique
 Columns whose data types are rarely changed.
 Columns that do not contain nulls
 Columns that are numeric.
Page 3 of 5
Oracle - Practical 11
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
EMP_NAME
JOB
DEPT_NO
NUMBER(4)
NOT NULL,
VARCHAR2(20),
VARCHAR2(20),
NUMBER(2));
CREATE TABLE DEPARTMENT
(DEPT_NO
NUMBER(2)
CONSTRAINT
PRIMARY KEY,
DEPT_NAME VARCHAR2(20));
dept_pk_deptno
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;
Page 4 of 5
Oracle - Practical 11
ALTER TABLE CARS
DROP CONSTRAINT CHECK_MAKE;
Note: Disabling a Primary Key and then enabling it again will recreate its index.
Disabling Constraint Checking:
 When a constraint is disabled, the constraint is not enforced.
 When a constraint is disabled, associated indexes are dropped.
 Disable constraints when loading large amounts of data, performing batch operations,
or importing or exporting one table at a time.
 When a constraint is disabled, the speed for a bulk load increases.
Enabling Constraint Checking:
 When a constraint is enabled, the constraint is enforced.
 When a constraint is enabled, associated indexes are created.
 When constraints are enabled, the rows of a table are checked for violations.
 The table is locked until the check is complete.
 Constraint violations are reported. If any rows violate a constraint, the statement fails
and the constraints remain disabled.
 By default, constraints are enabled when they are created.
DATA DICTIONARY VIEWS FOR CONSTRAINTS:
We have already covered the Data Dictionary in earlier tutorials – it is the central reference
point to everything about the Oracle database.
It stores information on:
 The names of Oracle users
 All rights and privileges granted to users
 Names of database objects (tables, views, indexes etc)
 Constraints applied to a table
 Indexes applied to a table
 Auditing information, such as who has accessed or updated specified database objects
To view the Constraints we have applied to a table we can type the following:
SELECT
FROM
WHERE
*
USER_CONSTRAINTS
TABLE_NAME = ‘CARS’;
This will show us the types of constraints applied to the CARS table.
Other Data Dictionary Views for Constraints:
 USER_CONS_COLUMNS:
Information about columns in constraint definitions
owned by the user.
 ALL_CONSTRAINTS:
Constraint definitions on accessible tables
 ALL_CONS_COLUMNS:
Information about accessible columns in constraint
definitions
 DBA_CONSTRAINTS:
Constraint definitions on all tables.
 DBA_CONS_COLUMNS:
Information about all columns in constraint definitions
Page 5 of 5
Download