37
Integrity 7. 1 CSE2316/3316 Database Management Systems Database Integrity

Integrity 7. 1 CSE2316/3316 Database Management Systems Database Integrity

  • View
    229

  • Download
    3

Embed Size (px)

Citation preview

Integrity 7. 1

CSE2316/3316 Database Management Systems

Database Integrity

Integrity 7. 2

Integrity

• Components of a Data Model

– Structure

– Operators

– Integrity

Integrity 7. 3

Dangers to DataA DBMS must protect data from several dangers.

- Accidents (programming errors and miskeying) :these are integrity issues.

- Malicious Use : A security issue.

- Hardware and Software Failures

:these are concurrency and restart issues.

Integrity 7. 4

Definitions of IntegrityData integrity requires the database to be an

accurate reflection of the real world.

Data should be valid and complete.

Currently integrity issues have been handled external to the database in the application code.

Codd (1985) states that integrity constraints specific to a particular RDBMS must be definable in SQL(e.g.) and stored in catalog.

Integrity 7. 5

A DBMS Enforced Integrity

EMPLOYEE Table

EMP# INTEGER NOT NULL

Attempt to add an employee without an EMP#

INSERT INTO EMPLOYEE

( EMP_NAME,AGE,SALARY)

VALUES (‘Smith’,22,35000)

This statement is rejected by the DBMS

Integrity 7. 6

An Application Based Integrity

IF AGE > 16 OR AGE < 99

THEN O-K

ELSE

REJECT ‘Age Invalid’

This represents a segment of program code in an application program.

Integrity 7. 7

Integrity Enforcement

Integrity enforcement is usually split between the

DBMS and the application programs.

Using application programs for integrity

assertions has disadvantages.

- Programming is more complex

- Integrity constraints may be repeated

- Change management is difficult

- Constraints may contradict

- Ad hoc operations may avoid the constraints

Integrity 7. 8

Codd’s CURED or CRUDE Classification of Integrities

Type E - Entity Integrity

Type R - Referential Integrity

Type D - Domain Integrity :A user defined datatype

Type C - Column Integrity:Linked to Domain integrity

Type U - User Defined Integrity

Integrity 7. 9

Domain IntegrityA domain is a conceptual pool of values from which

one or more attributes draw their actual values.

DOMAIN AGE RANGE 0-127

ATTRIBUTE EMPLOYEE.AGE 16-65

ATTRIBUTE DEPENDENT.AGE 0-60

Two values can only be compared if they come from

the same domain.

Integrity 7. 10

Many things need to be considered when defining a domain

A Domain Definition

DOMAIN GENDER

- Data Type: Character

- Length: 6 bytes

- Allowable Values: Male, Female, Null

- Storage Format: Uppercase

- Operations Allowed:

- Inherited Operators: String, Unstring, =

- Input Editing: Nil

- Extra Functions: Is_ Male, Is_Female,What_Gender

Integrity 7. 11

To define the EMP table owned by SCOTT, you could issue the following statement:

CREATE TABLE scott.emp (empno NUMBER CONSTRAINT pk_emp PRIMARY KEY, ename VARCHAR2(10) CONSTRAINT nn_ename NOT NULL CONSTRAINT upper_ename CHECK (ename = UPPER(ename)), job VARCHAR2(9), mgr NUMBER CONSTRAINT fk_mgr REFERENCES scott.emp(empno), hiredate DATE DEFAULT SYSDATE, sal NUMBER(10,2) CONSTRAINT ck_sal CHECK (sal > 500), comm NUMBER(9,0) DEFAULT NULL, deptno NUMBER(2) CONSTRAINT nn_deptno NOT NULL

CONSTRAINT fk_deptno REFERENCES scott.dept(deptno) ) ;

This table contains 8 columns. For example, the EMPNO column is of datatype NUMBER and has an associated integrity constraint named PK_EMP. The HIRDEDATE column is of datatype DATE and has a default value of SYSDATE.

Oracle Support for Integrity: An Example

Integrity 7. 12

An integrity constraint is a rule that restricts the values for one or more columns in a table. Column CONSTRAINT clauses can appear in either CREATE TABLE or ALTER TABLE commands.

[CONSTRAINT constraint] [[NOT] NULL | UNIQUE | PRIMARY KEY ] [REFERENCES [user.] table[ (column) ] [ON DELETE CASCADE] [CHECK (condition) ] [EXCEPTIONS INTO [user.] table] [DISABLE ]

Column_constraint syntax that appears in an ALTER TABLE statement can only define or remove a NOT NULL constraint.

Oracle Integrity Constraints: Column

Integrity 7. 13

A table CONSTRAINT is identical to a column constraint except that it can reference multiple columns with a single constraint

[CONSTRAINT constraint] {[NOT] NULL | [ {UNIQUE | PRIMARY KEY} (column[, column])[FOREIGN KEY (column[, column]) [REFERENCES [user.] table[ (column[, column]) ] [ON DELETE CASCADE] [CHECK (condition) ] [EXCEPTIONS INTO [user.] table] [DISABLE ]

Oracle Integrity Constraints: Table

Integrity 7. 14

The following statement alters the EMP table and defines and enables a NOT NULL constraint on the SAL column:

ALTER TABLE emp MODIFY (sal NUMBER CONSTRAINT nn_sal NOT NULL)

NN_SAL ensures that no employee in the table has a null salary.

Example of an Alter Table

Integrity 7. 15

CREATE TABLE dept (deptno NUMBER(2), dname VARCHAR2(9) CONSTRAINT unq_dname UNIQUE, loc VARCHAR2(10) ) ;

The constraint UNQ_DNAME identifies the DNAME column as a unique key. This constraint ensures that no two departments in the table have the same name. However, the constraint does allow departments without names.

Alternatively, you can define and enable this constraint with the table_constraint syntax:

CREATE TABLE dept (deptno NUMBER(2), dname VARCHAR2(9), loc VARCHAR2(10), CONSTRAINT unq_dname UNIQUE (dname)) ;

Examples of Unique in Oracle

Integrity 7. 16

The UNIQUE constraint designates a column or combination of columns as a unique key. To satisfy a UNIQUE constraint, no two rows in the table can have thesame value for the unique key. However, the unique key made up of a single column can contain nulls.

A unique key column cannot be of datatype LONG or LONG RAW. You cannot designate the same column or combination of columns as both a unique key and a primary key or as both a unique key and a cluster key. However, you can designate the same column or combination of columns as both a unique key and a foreign key.

You can define a unique key on a single column with column_constraint syntax.

UNIQUE Constraints in Oracle

Integrity 7. 17

To define a composite unique key, you must use table_constraint syntax, rather than column_constraint syntax.

To satisfy a constraint that designates a composite unique key, no two rows in the table can have the same combination of values in the key columns. Also, any row that contains nulls in all key columns automatically satisfies the constraint. However, two rows that contain nulls for one or more key columns and the same combination of values for the other key columns violate the constraint.

The following statement defines and enables a composite unique key on the combination of the CITY and STATE columns of the CENSUS table:

ALTER TABLE census ADD CONSTRAINT unq_city_state UNIQUE (city, state) EXCEPTIONS INTO bad_keys_in_ship_cont

Defining Composite Unique Keys

Integrity 7. 18

A PRIMARY KEY constraint designates a column or combination of columns as the table's primary key. To satisfy a PRIMARY KEY constraint, both of the following conditions must be true: No primary key value can appear in more than one row in the table. No column that is part of the primary key can contain a null.

A table can have only one primary key.

A primary key column cannot be of datatype LONG or LONG RAW. You cannot designate the same column or combination of columns as both a primary key and a unique key or as both a primary key and a cluster key. However, you can designate the same column or combination of columns as both a primary key and a foreign key.

PRIMARY KEY Constraints in Oracle

Integrity 7. 19

You can use the column_constraint syntax to define a primary key on a single column.

The following statement creates the DEPT table and defines and enables a primary key on the DEPTNO column:

CREATE TABLE dept (deptno NUMBER(2) CONSTRAINT pk_dept PRIMARY KEY, dname VARCHAR2(9), loc VARCHAR2(10) )

The PK_DEPT constraint identifies the DEPTNO column as the primary key of the DEPTNO table. This constraint ensures that no two departments in the tablehave the same department number and that no department number is NULL.

Defining Primary Keys in Oracle

Integrity 7. 20

Alternatively, you can define and enable this constraint with table_constraint syntax:

CREATE TABLE dept (deptno NUMBER(2), dname VARCHAR2(9), loc VARCHAR2(10), CONSTRAINT pk_dept PRIMARY KEY (deptno) )

Defining Composite Primary Keys

A composite primary key is a primary key made up of a combination of columns. Because Oracle creates an index on the columns of a primary key, a composite primary key can contain a maximum of 16 columns. To define a composite primary key, you must use the table_constraint syntax, rather than the column_constraint syntax.

Defining Primary Keys in Oracle

Integrity 7. 21

The following statement defines a composite primary key on the combination of the SHIP_NO and CONTAINER_NO columns of the SHIP_CONT table:

ALTER TABLE ship_cont ADD PRIMARY KEY (ship_no, container_no) DISABLE

This constraint identifies the combination of the SHIP_NO and CONTAINER_NO columns as the primary key of the SHIP_CONTAINER. The constraintensures that no two rows in the table have the same values for both the SHIP_NO column and the CONTAINER_NO column.

The CONSTRAINT clause also specifies the following properties of the constraint:

Since the constraint definition does not include a constraint name, Oracle generates a name for the constraint. The DISABLE option causes Oracle to define the constraint but not enforce it. At a later time the ENABLE clause can be used to enable a single integrity constraint or all triggers associated with the table.

Use of the Disable Option in Oracle

Integrity 7. 22

DEPTEMP

Dno DnameEno Ename Dno

D5 ResearchD6 AdvertisingD7 Newprojects

E1 Smith D5E2 Black D6E3 Jones D6

E4 Brown is to be inserted. What check must be made to maintain integrity?An attempt to delete D5 Research occurs. What possible actions might take place to maintain integrity?

DEPT

EMP

Referential Integrity

Integrity 7. 23

To Maintain Referential IntegrityEvent

Delete corresponding Child records(Cascading Delete) ORUpdate Foreign Key of corresponding Child records (Cascading Update)

Delete of Parent

Update of PrimaryKey of Parent

Insert of Child

Action

Parent

Child

Set the Foreign Key to null in the corresponding Child records(Delete or Update Nullifies)

Do not allow the delete or update of the Parent record if any corresponding Child records exist(ie. the event fails to proceed)(Restricted Delete or Update)

Check that null or a valid Primary Key from the Parent has been specified for the Foreign Key

Integrity 7. 24

The following statement creates the EMP table and defines and enables a foreign key on the DEPTNO column that references the primary key on the DEPTNO column of the DEPT table:

CREATE TABLE emp (empno NUMBER(4), ename VARCHAR2(10), job VARCHAR2(9), mgr NUMBER(4), hiredate DATE, sal NUMBER(7,2), comm NUMBER(7,2), deptno NUMBER(2) CONSTRAINT fk_deptno REFERENCES dept(deptno) )

Oracle Referential Integrity Constraints

Integrity 7. 25

ON DELETE CASCADE allows deletion of referenced key values in the parent table that have dependent rows in the child table and causes Oracle to automatically delete dependent rows from the child table to maintain referential integrity.

If you omit this option, Oracle forbids deletions of referenced key values in the parent table that have dependent rows in the child table.

You can define multiple foreign keys in a table. Also, a single column can be part of more than one foreign key.

Keywords in Oracle Referential Integrities

Integrity 7. 26

CREATE TABLE emp (empno NUMBER(4), ename VARCHAR2(10), job VARCHAR2(9), mgr NUMBER(4), hiredate DATE, sal NUMBER(7,2), comm NUMBER(7,2), deptno NUMBER(2), CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES dept(deptno) ) Note that the foreign key definitions in both of the above statements omit the ON DELETE CASCADE option, causing Oracle to forbid the deletion of a department if any employee works in that department. If you use the ON DELETE CASCADE option, Oracle permits deletions of referenced key values in the parent table and automatically deletes dependent rows in the child table to maintain referential integrity.

Example With Table_constraint Syntax

Integrity 7. 27

This example creates the EMP table, defines and enables the referential integrity constraint FK_DEPTNO, and uses the ON DELETE CASCADE option:

CREATE TABLE emp (empno NUMBER(4), ename VARCHAR2(10), job VARCHAR2(9), mgr NUMBER(4), hiredate DATE, sal NUMBER(7,2), comm NUMBER(7,2), deptno NUMBER(2) CONSTRAINT fk_deptno REFERENCES dept(deptno) ON DELETE CASCADE )

The ON DELETE CASCADE Option

Integrity 7. 28

ALTER TABLE phone_calls ADD CONSTRAINT fk_areaco_phoneno FOREIGN KEY (areaco, phoneno) REFERENCES customers(areaco, phoneno) EXCEPTIONS INTO wrong_numbers

The constraint FK_AREACO_PHONENO ensures that all the calls in the PHONE_CALLS table are made from phone numbers that are listed in the CUSTOMERS table. Before you define and enable this constraint, you must define and enable a constraint that designates the combination of the AREACO and PHONENO columns of the CUSTOMERS table as a primary or unique key.

The EXCEPTIONS option causes Oracle to write information into WRONG_NUMBERS table about any rows in the PHONE_CALLS table that violate the constraint.

A Foreign Key Constraint on two Columns

Integrity 7. 29

CREATE TABLE dept (deptno NUMBER CONSTRAINT check_deptno CHECK (deptno BETWEEN 10 AND 99) DISABLE, dname VARCHAR2(9) CONSTRAINT check_dname CHECK (dname = UPPER(dname)) DISABLE, loc VARCHAR2(10) CONSTRAINT check_loc CHECK (loc IN ('DALLAS','BOSTON', 'NEW YORK','CHICAGO')) DISABLE)

Unlike other types of constraints, a CHECK constraint defined with column_constraint syntax can impose rules on any column in the table, rather than only on the column in which it is defined. Because each CONSTRAINT clause contains the DISABLE option, Oracle only defines the constraints and does not enforce them.

Example of a CHECK Constraint on a Column

Integrity 7. 30

The following statement creates the EMP table and uses a table constraint clause to define and enable a CHECK constraint:

CREATE TABLE emp (empno NUMBER(4), ename VARCHAR2(10), job VARCHAR2(9), mgr NUMBER(4), hiredate DATE, sal NUMBER(7,2), comm NUMBER(7,2), deptno NUMBER(2), CHECK (sal + comm <= 5000) )

Example of a CHECK Constraint on a Table

Integrity 7. 31

CREATE TABLE order_detail (CONSTRAINT pk_od PRIMARY KEY (order_id, part_no), order_id NUMBER CONSTRAINT fk_oid REFERENCES scott.order (order_id), part_no NUMBER CONSTRAINT fk_pno REFERENCES scott.part (part_no), quantity NUMBER CONSTRAINT nn_qty NOT NULL CONSTRAINT check_qty_low CHECK (quantity > 0), cost NUMBER CONSTRAINT check_cost CHECK (cost > 0) )

More Than One CHECK Constraint on a Table

Integrity 7. 32

This example creates a BEFORE statement trigger named EMP_PERMIT_CHANGES in the schema SCOTT. This trigger ensures that changes to employee records are only made during business hours on working days:

CREATE TRIGGER scott.emp_permit_changes BEFORE DELETE OR INSERT OR UPDATE ON scott.emp DECLARE dummy INTEGER; BEGIN /* If today is a Saturday or Sunday, then return an error.*/ IF (TO_CHAR(SYSDATE, 'DY') = 'SAT' OR TO_CHAR(SYSDATE, 'DY') = 'SUN') THEN raise_application_error( -20501, 'May not change employee table during the weekend'); END IF;

Example of a Trigger

Integrity 7. 33

/* Compare today's date with the dates of all company holidays. If today is a company holiday, then return an error. */ SELECT COUNT(*) INTO dummy FROM company_holidays WHERE day = TRUNC(SYSDATE); IF dummy > 0 THEN raise_application_error( -20501, 'May not change employee table during a holiday'); END IF; /* If the current time is before 8:00AM or after 6:00PM, then return an error.

*/ IF (TO_CHAR(SYSDATE, 'HH24') < 8 OR TO_CHAR(SYSDATE, 'HH24') >= 18) THEN raise_application_error( -20502, 'May only change employee table during working hours'); END IF; END;

Integrity 7. 34

This example creates a BEFORE row trigger named SALARY_CHECK in the schema SCOTT. Whenever a new employee is added to the employee table or an existing employee's salary or job is changed, this trigger guarantees that the employee's salary falls within the established salary range for the employee's job:

CREATE TRIGGER scott.salary_check BEFORE INSERT OR UPDATE OF sal, job ON scott.emp FOR EACH ROW WHEN (new.job <> 'PRESIDENT') DECLARE minsal NUMBER; maxsal NUMBER;

A Second Example of a Trigger

Integrity 7. 35

BEGIN /* Get the minimum and maximum salaries for the employee's job from the SAL_GUIDE table. */ SELECT minsal, maxsal INTO minsal, maxsal FROM sal_guide WHERE job = :new.job; /* If the employee's salary is below the minimum or */ /* above the maximum for the job, then generate an */ /* error. */ IF (:new.sal < minsal OR :new.sal > maxsal) THEN raise_application_error( -20601, 'Salary ' || :new.sal || ' out of range for job ' || :new.job || ' for employee ' || :new.ename ); END IF; END;

Integrity 7. 36

OR REPLACE recreates the trigger if it already exists. You can use this option to change the definition of an existing trigger without first dropping it. BEFORE / AFTERindicates that Oracle fires the trigger before or after executing the triggering statement. For row triggers, this is a separate firing before or after each affected row is changed.DELETE indicates that Oracle fires the trigger whenever a DELETE statement removes a row from the table. INSERT indicates that Oracle fires the trigger whenever an INSERT statement adds a row to table. UPDATE OF indicates that Oracle fires the trigger whenever an UPDATE statement changes a value in one of the columns specified in the OF clause. If you omit the OF clause, Oracle fires the trigger whenever an UPDATE statement changes a value in any column of the table.

Oracle CREATE TRIGGER Keywords

Integrity 7. 37

ON specifies the schema and name of the table on which the trigger is to be created. If you omit schema, Oracle assumes the table is in your own schema. You cannot create a trigger on a table in the schema SYS. REFERENCING specifies correlation names. You can use correlation names in the PL/SQL block and WHEN clause of a row trigger to refer specifically to old and new values of the current row. The default correlation names are OLD and NEW. If your row trigger is associated with a table named OLD or NEW, you can use this clause to specify different correlation names. FOR EACH ROW designates the trigger to be a row trigger. Oracle fires a row trigger once for each row that is affected by the trigger statement and meets the optional trigger constraint (defined in WHEN) WHEN clause. specifies the trigger restriction. The trigger restriction contains a SQL condition that must be satisfied for Oracle to fire the trigger. This condition must contain correlation names and cannot contain a query. You can only specify a trigger restriction for a row trigger. Oracle evaluates this condition for each row affected by the triggering statement.

Oracle CREATE TRIGGER Keywords