Managing Declarative Constraints

80
Declarative Constraints for Complex Business Rules and Improved Performance Carl Dudley University of Wolverhampton

Transcript of Managing Declarative Constraints

Page 1: Managing Declarative Constraints

1Carl Dudley – University of Wolverhampton, UK

Declarative Constraints for Complex Business Rules and Improved

Performance

Carl Dudley

University of Wolverhampton

Page 2: Managing Declarative Constraints

Carl Dudley – University of Wolverhampton, UK

Declarative Constraints for Complex Business Rules and Improved

Performance

Carl DudleyUniversity of Wolverhampton, UK

UKOUG Director

[email protected]

   

                                                                                                         

                  

Page 3: Managing Declarative Constraints

3Carl Dudley – University of Wolverhampton, UK

ConstraintsConstraints

Constraints – Origins and Syntax

NULLs and Foreign Key Constraints

Deferring and Enforcing Constraints

Data Dictionary Support

Complex Constraints and Query Transformations

Working with Oracle since 1986

Oracle DBA - OCP Oracle7, 8, 9, 10

Oracle DBA of the Year – 2002

Oracle ACE Director

Regular Presenter at Oracle Conferences

Consultant and Trainer

Technical Editor for a number of Oracle texts

UK Oracle User Group Director

Member of IOUC

Day job – University of Wolverhampton, UK

Page 4: Managing Declarative Constraints

4Carl Dudley – University of Wolverhampton, UK

Constraints – a Brief HistoryConstraints – a Brief History

1970 – Ted Codd— A Relational Model for Large Shared Databanks

1984 DB2— Rudimentary support

SQL 1986— Initial requirements

1987 OracleV6— Documentation support

SQL 1989— Referential Integrity

1992 Oracle7— Full support

Page 5: Managing Declarative Constraints

5Carl Dudley – University of Wolverhampton, UK

Declarative Constraints Declarative Constraints

Preserve data integrity through the use of constraints — Cover rows already present in the table plus any rows which are

subsequently created

Implement simple business rules such as ‘salaries should not exceed $5000’

— More complex business rules need to be handled by application logic within transactions or by the use of database triggers

Not independent objects — Created and manipulated only via CREATE TABLE and ALTER TABLE

Page 6: Managing Declarative Constraints

6Carl Dudley – University of Wolverhampton, UK

Primary Key Constraints Primary Key Constraints

Constraints can be used to enforce — Entity Integrity (no duplicate rows?)— Referential Integrity

EVERY table should have a primary key (to enforce entity integrity)

— The primary key column(s) will be1. UNIQUE and NOT NULL 2. Automatically indexed (use can be made of an existing index)

CREATE TABLE emp(empno NUMBER(4) CONSTRAINT emp_pk PRIMARY KEY,

:

Page 7: Managing Declarative Constraints

7Carl Dudley – University of Wolverhampton, UK

Primary Key ConstraintsPrimary Key Constraints

Unique and not null data values— Should not contain ‘meaningful’ data and should not be updated

• Usually numeric columns and as short as possible — Can be composite - but usually single columns

• Composite keys can be big and require complex join criteria • Cannot be assigned simple sequence numbers

Often named using primary key column or table name(s) with a ‘_pk’ suffix

Use ALTER TABLE to place a primary key on an existing table

ALTER TABLE deptADD CONSTRAINT dept_pk PRIMARY KEY(deptno);

Page 8: Managing Declarative Constraints

8Carl Dudley – University of Wolverhampton, UK

Unique and NOT NULL ConstraintsUnique and NOT NULL Constraints

Unique Constraints are like primary keys but allow NULL values— Unlimited as all are considered unique! — Any number allowed on a table — Often named with unique key column name(s) plus a ‘_uk’ suffix

NOT NULL constraints do not allow NULLs in a column— Not named and usually defined when a table is created

CREATE TABLE emp (empno NUMBER(4) NOT NULL,

ename VARCHAR2(20), sal NUMBER(7,2) NOT

NULL, comm NUMBER(7,2), deptno NUMBER(2) NOT

NULL);

ALTER TABLE empADD CONSTRAINT ename_deptno_uk UNIQUE (ename,deptno);

Page 9: Managing Declarative Constraints

9Carl Dudley – University of Wolverhampton, UK

CHECK Constraints CHECK Constraints

Simple business rules can be enforced — Based on constants and column values of only the row being updated — References to data in other tables via subqueries is not possible

• sysdate and user are not allowed due to implicit subquery— A common naming convention is to use a ‘_ck’ suffix

Optimised beneath the SQL layer

Not violated if evaluate to unknown

— A NULL sal will allow a negative comm to pass the check— In this case, it may be advisable to declare both columns as NOT NULL

ALTER TABLE emp ADD CONSTRAINT sal_ck CHECK (sal > 0 OR comm >= 0);

Page 10: Managing Declarative Constraints

10Carl Dudley – University of Wolverhampton, UK

CHECK Constraint ExamplesCHECK Constraint Examples

Ensure gender values are always uppercase ‘m’ or ‘f’

CREATE TABLE emp (gender VARCHAR2(1) CONSTRAINT gender_ckCHECK (gender IN UPPERCASE(‘m’,’f’));

ALTER TABLE emp (ADD CONSTRAINT comm_ckCHECK (comm < sal*0.25);

ALTER TABLE emp (ADD CONSTRAINT hiredate_ckCHECK (hiredate <= sysdate);

— Error because sysdate implies a subquery

Ensure that hiredate is not later than the current date

Ensure that commission is never more than ¼ of an employee’s salary

Page 11: Managing Declarative Constraints

11Carl Dudley – University of Wolverhampton, UK

Referential IntegrityReferential Integrity

Maintains integrity of master-detail relationships

— Operations on the primary key of the parent table are constrained if dependent rows exist in the child table

— Operations on the foreign key in the child table are prevented if they result in values which do not exist in the parent (referenced) table

• In Oracle, foreign keys may be set to NULL regardless of any referential constraint

empno ename mgr deptno----- -------- ---- ------7369 SMITH 7566 207499 ALLEN 7698 107521 WARD 7698 307566 JONES 7521 107654 MARTIN 7521 107698 BLAKE 30

Foreign keys

deptno dname------ ---------- 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS

Emp Dept

Page 12: Managing Declarative Constraints

12Carl Dudley – University of Wolverhampton, UK

Foreign Key (Referential) Constraints Foreign Key (Referential) Constraints

May be single or composite columns

Must match data type of the referenced column(s)

No limit to number of foreign keys

Referenced columns must already be PRIMARY KEY or UNIQUE columns — Referenced columns may be in the same table

Foreign key columns may be NULL or partly NULL (regardless of any Foreign Key constraint)

Indexes are not automatically created on the foreign key columns

Page 13: Managing Declarative Constraints

13Carl Dudley – University of Wolverhampton, UK

In-line and Out-of-line Constraints In-line and Out-of-line Constraints

An in-line constraint is specified on the same line as the column definition— Also known as a column-level constraint— Datatype definition is not actually required for foreign key

CREATE TABLE ... :

deptno NUMBER(4) CONSTRAINT emp_dept_fkREFERENCES dept(deptno);

CREATE TABLE ...:

divno NUMBER(3),deptno NUMBER(4),

:CONSTRAINT emp_dept_fk FOREIGN KEY (divno,deptno) REFERENCES dept(divno,deptno);

An out-of-line constraint is specified in a separate clause— Also known as a table-level constraint— Suppose dept has a composite key of divno and deptno

Page 14: Managing Declarative Constraints

14Carl Dudley – University of Wolverhampton, UK

General Foreign Key Constraint Actions General Foreign Key Constraint Actions

Oracle supports : Restriction of UPDATEs to referenced columns 

Restriction of DELETEs to referenced columns 

DELETE CASCADE (deletion of dependent rows)

DELETE SET NULL

No support for UPDATE CASCADE — Must be performed via triggers or application logic

The SQL standard proposes a ‘PENDANT’ facility — When the last remaining employee in a department is deleted, the

department record must also be deleted

RESTRICT SET NULL SET DEFAULT CASCADE

Four options are generally recognised for actions performed by foreign key constraints

Page 15: Managing Declarative Constraints

15Carl Dudley – University of Wolverhampton, UK

The DELETE CASCADE Action The DELETE CASCADE Action

Syntax for DELETE CASCADE :

Oracle will report only on rows which are deleted from dept when this referential action occurs

ALTER TABLE empADD CONSTRAINT emp_dept_fk FOREIGN KEY (deptno) REFERENCES dept(deptno)ON DELETE CASCADE;

DELETE FROM dept WHERE deptno IN (10,20);

2 rows deleted

Page 16: Managing Declarative Constraints

16Carl Dudley – University of Wolverhampton, UK

Conterminous Paths Conterminous Paths

Each table has one row with the value ‘x’

What would be the effect of? :

TableA

TableB TableC

Delete CascadeDelete Cascade

Delete Cascade

Delete Restrict

 

 

 

 

 

 

 

 

TableD

DELETE FROM a WHERE col = ‘x’;

Page 17: Managing Declarative Constraints

17Carl Dudley – University of Wolverhampton, UK

Enabling/Disabling ConstraintsEnabling/Disabling Constraints

— Relaxes the constraint— Often done to increase speed of DML (e.g. bulk data loads)— Drops any associated UNIQUE index by default

— Enforces the constraint— Checks rows for violations— Any violations prevent the constraint being enabled— Locks out activity on the table— Builds any associated index (may take some time)

Constraints are enabled by default on creation

ALTER TABLE emp DISABLE CONSTRAINT emp_pk;

ALTER TABLE emp ENABLE CONSTRAINT emp_pk;

Page 18: Managing Declarative Constraints

18Carl Dudley – University of Wolverhampton, UK

Foreign Key Indexes and LockingForeign Key Indexes and Locking

Most foreign keys should be indexed— If the unique or primary key is updated or deleted

• Indexes are even more important for ON DELETE CASCADE — If there are many joins between parent and child

When a foreign key is unindexed :— DML on the parent primary key results in a table level lock on the child

preventing DML on it — The child table lock is obtained and released immediately for each update of

row in parent— Lock exists for short period, but can cause significant contention on child

When a foreign key is indexed :— DML on parent primary key results in a row share table lock on child table— Prevents other transactions gaining table locks on the child table, but does

not block DML on either the parent or the child table— Only rows relating to the parent primary key are locked in the child table

Page 19: Managing Declarative Constraints

19Carl Dudley – University of Wolverhampton, UK

Indexes on Foreign KeysIndexes on Foreign Keys

Foreign keys are not indexed by default— Very significant locking implications— MONITORING USAGE does not detect use of indexes for concurrency— 11gR2 invisible indexes on FKs are also 'invisible' for concurrency

Support document 1019527.6 has a script that generates advice/report— Not accurate if foreign key columns present in an index in different order

• Interprets this as unusable indexChanging data in table DEPT will lock table EMP Create an index on table EMP with the following columns to remove lock problem Column = DEPTNO (1) Changing data in table ITEM_CATEGORIES will lock table ITEMS Create an index on table ITEMS with the following columns to remove lock problem  Column = ITEM_CAT (1)  Column = ITEM_BUS_UNIT (2)    Changing data in table EMP will lock table EMP  Create an index on table EMP with the following columns to remove lock problem    Column = MGR (1)   

Page 20: Managing Declarative Constraints

20Carl Dudley – University of Wolverhampton, UK

Constraints and Entity ModelsConstraints and Entity Models

No constraints (other than referential) on the foreign key

Additional NOT NULL constraint on the foreign key

Additional UNIQUE constraint on the foreign key

Additional UNIQUE and NOT NULL constraints on the foreign key

X Y

X Y

X Y

X Y

Page 21: Managing Declarative Constraints

21Carl Dudley – University of Wolverhampton, UK

ConstraintsConstraints

Constraints – Origins and Syntax

NULLs and Foreign Key Constraints

Deferring and Enforcing Constraints

Data Dictionary Support

Complex Constraints and Query Transformations

Page 22: Managing Declarative Constraints

22Carl Dudley – University of Wolverhampton, UK

Single column foreign key values must match primary key or be NULL

Composite foreign key values must match primary key or be wholly or partly NULL

Partly NULL keys are not checked for the integrity of the NOT NULL part

Foreign Keys and Nulls Foreign Keys and Nulls

Newdept 

Divno Deptno Desc----- ------ ----------1 1 Finance1 2 Sales2 1 Operations2 2 Design2 3 Chemicals

Newemp 

Divno Deptno Ename----- ------ ------2 1 Smith1 2 Adams2 1 Carter2 NULL BestNULL NULL CoxNULL 9 Scott1 NULL King2 5 Ford

PK = divno,deptno

FK = divno,deptno

Scott passes the integrity check, but Scott’s NULL can not be updated— The row for Ford will be checked out

Page 23: Managing Declarative Constraints

23Carl Dudley – University of Wolverhampton, UK

Matching NullsMatching Nulls

A composite foreign key may be (i) all NULL

(ii) all non-NULL

(iii) partially NULL

— There are three possible matching rules for such keys

1. Match Full All columns must be NULL or all columns must have matching values in the primary key

2. Match Partial All columns must be NULL or Some of the columns may be NULL and the

remainder must match values in their respective primary key columns

— Match none All columns must be NULL or one or more columns are NULL and the remainder may take any value

Oracle by default uses the Match None rule

Page 24: Managing Declarative Constraints

24Carl Dudley – University of Wolverhampton, UK

Matching Partial NULLs Matching Partial NULLs

Partial NULLs are allowed in foreign keys (ANSI standard)

To prevent partial NULLs – use a CHECK constraint

— This will force the ‘Match Full’ rule for NULLs — The ‘Match Partial’ rule can not be properly implemented using

declarative integrity constraints – database triggers must be used

CONSTRAINT divno_deptno_ck CHECK ( ((divno IS NOT NULL) AND (deptno IS NOT NULL)) OR (((divno IS NULL) AND (deptno IS NULL)))

Page 25: Managing Declarative Constraints

25Carl Dudley – University of Wolverhampton, UK

ConstraintsConstraints

Constraints – Origins and Syntax

NULLs and Foreign Key Constraints

Deferring and Enforcing Constraints

Data Dictionary Support

Complex Constraints and Query Transformations

Page 26: Managing Declarative Constraints

26Carl Dudley – University of Wolverhampton, UK

Handling ExceptionsHandling Exceptions

To deal with rows which are violating (and preventing) a constraint

1. Construct an exceptions table using the UTLEXCPT script

2. Issue a statement to create a constraint – for example :

This will place the ROWIDs of any offending rows in the exceptions table so that the rows can be identified

and dealt with

3. Optionally remove all rows causing violations (could be dangerous)

ALTER TABLE emp ADD CONSTRAINT emp_pkPRIMARY KEY (empno)EXCEPTIONS INTO exceptions;

ROW_ID OWNER TABLE_NAME CONSTRAINT------------------ ---------- ----------- ----------AAABFJAACAAAFA3AAN SCOTT EMP EMP_PKAAABFJAACAAAFA4AAN SCOTT EMP EMP_PK

DELETE FROM emp WHERE ROWID IN (SELECT row_id FROM exceptionsWHERE constraint = <constraint_name>);

Page 27: Managing Declarative Constraints

27Carl Dudley – University of Wolverhampton, UK

Deferred ConstraintsDeferred Constraints

Constraint checking can be deferred until end of transaction at commit time— If the constraint is violated, the entire transaction is rolled back

INITIALLY DEFERRED— On creation, constraint is not checked until commit

INITIALLY IMMEDIATE— On creation, constraint is checked after each DML statement (default)

DEFERRABLE, NOT DEFERRABLE— Governs whether behaviour of constraint can be subsequently changed to

DEFERRED or IMMEDIATE

ALTER TABLE table_name ADD CONSTRAINT ... :

[INITIALLY DEFERRED | INITIALLY IMMEDIATE][DEFERRABLE | NOT DEFERRABLE];

Page 28: Managing Declarative Constraints

28Carl Dudley – University of Wolverhampton, UK

Deferred Constraint ExampleDeferred Constraint Example

Scenario (hypothetical example for illustration only):— The dept table has a primary key on deptno

— The emp table has a deferrable foreign key on deptno referencing dept, initially set to ‘IMMEDIATE’

ALTER TABLE dept ADD CONSTRAINT dept_pk PRIMARY KEY(deptno)

ALTER TABLE emp ADD CONSTRAINT emp_dept_fkFOREIGN KEY (deptno) REFERENCES dept(deptno)INITIALLY IMMEDIATE DEFERRABLE;

Page 29: Managing Declarative Constraints

29Carl Dudley – University of Wolverhampton, UK

Deferred Constraint Example (continued)Deferred Constraint Example (continued)

It is required to update a department number from 10 to 99

Changes will need to be made by separate update statements as follows1. UPDATE dept SET deptno = 99 WHERE deptno = 10;2. UPDATE emp SET deptno = 99 WHERE deptno = 10;

The first update fails with the following error message

— The constraint is checked ‘immediately’ and therefore too early• Reversing the updates does not help

— Maybe we can change the constraint mode

ORA-02292: integrity constraint (SCOTT.EMP_DEPT_FK) violated - child record found

Page 30: Managing Declarative Constraints

30Carl Dudley – University of Wolverhampton, UK

Changing the Constraint ModeChanging the Constraint Mode

Two methods available to toggle constraint modes

1. SET CONSTRAINT— Used to change the mode for a single transaction

2. ALTER SESSION— Changes mode for all deferrable constraints for an entire session

— Reset to default (initial) validation using the keyword DEFAULT

SET CONSTRAINT constraint_name,...,constraint_nameIMMEDIATE | DEFERRED;

SET CONSTRAINTS ALL IMMEDIATE | DEFERRED;

ALTER SESSION SET CONSTRAINTS = IMMEDIATE | DEFERRED;

ALTER SESSION SET CONSTRAINTS = DEFAULT;

Page 31: Managing Declarative Constraints

31Carl Dudley – University of Wolverhampton, UK

Processing the Update TransactionProcessing the Update Transaction

1. Set the constraint to be deferred until the end of the transaction— Constraint checked when commit occurs

2. Execute both updates— These both succeed in changing deptno values in emp and dept from 10

to 99

3. Issue the commit — The entire transaction will succeed as all the data is now consistent at time

of commit

SET CONSTRAINT emp_dept_pk DEFERRED;

Page 32: Managing Declarative Constraints

32Carl Dudley – University of Wolverhampton, UK

Index Support for Deferred ConstraintsIndex Support for Deferred Constraints

Index must be non-unique for deferred constraints— Index uniqueness could be violated DURING the transaction

Dropping a deferrable constraint does not drop the index (by default)

Creating a deferred constraint will use an existing non-unique index on the intended primary key column— Uniqueness will now be enforced— Index name will not be changed to constraint name

Any constraint built on an MV should be deferrable— The refresh process requires this

Page 33: Managing Declarative Constraints

33Carl Dudley – University of Wolverhampton, UK

Enforced ConstraintsEnforced Constraints

Existing data is not checked— Checks made only on changes after enabling the constraint

Used when constraints do not apply to historical data or when it is known that existing data already complies with the constraint

Example : No new employees can have a salary > $3000

— Constraint is created even though existing rows violate it

Attempt to insert a new row which violates the constraint

— Rejected with the following error message

ALTER TABLE emp ADD CONSTRAINT sal_ck CHECK (sal <=3000) ENABLE NOVALIDATE;

INSERT INTO emp (empno,ename,sal,deptno)VALUES (8888,’COX’,5500,10);

ORA-02290: check constraint (SCOTT.EMP_CK) violated

Page 34: Managing Declarative Constraints

34Carl Dudley – University of Wolverhampton, UK

Validating Enforced ConstraintsValidating Enforced Constraints

After eliminating all violations, the constraint can be validated so that it acts upon all rows in the table

If King’s salary of $5000 is still present the following error message is generated

ALTER TABLE emp ENABLE VALIDATE CONSTRAINT sal_ck;

ORA-02293: cannot enable (SCOTT.EMP_CK) - check constraint violated

Page 35: Managing Declarative Constraints

35Carl Dudley – University of Wolverhampton, UK

Setting up Primary Keys with NOVALIDATESetting up Primary Keys with NOVALIDATE

Suppose you have a table of historical data that could already have duplicate rows which are of no immediate consequence

You want to restrict any new data to be unique

1. Create a non-unique index on the 'primary key' column(s)

2. Create a primary key constraint in NOVALIDATE state

The normal instigation of a primary key builds a unique index even in NOVALIDATE state— Any duplicate rows already present will foul the creation of the index— If no duplicates, there will still be a delay before the constraint is enforced

due to creation of the unique index

Page 36: Managing Declarative Constraints

36Carl Dudley – University of Wolverhampton, UK

Non_unique Indexes for Primary KeysNon_unique Indexes for Primary Keys

CREATE TABLE empn AS SELECT * FROM emp;

INSERT INTO empn SELECT * FROM empn WHERE empno = 7369;

ALTER TABLE empn ADD CONSTRAINT empn_pk PRIMARY KEY(EMPNO)USING INDEX (CREATE INDEX empn_pk ON empn(empno)) NOVALIDATE;

Table altered. --succeeds because non-unique index can be built

INSERT INTO empn SELECT * FROM empn;

ORA-00001: unique constraint (SCOTT.EMPN_PK) violated

ALTER TABLE empn ADD CONSTRAINT empn_pk PRIMARY KEY(empno) NOVALIDATE;

ORA-02437: cannot validate (SCOTT.EMPNOVAL_PK) - primary key violated

Build a table with duplicate data already present

Try to enforce a (non-DEFERRABLE) primary key with a unique index

Enforce primary key with non-unique index

Page 37: Managing Declarative Constraints

37Carl Dudley – University of Wolverhampton, UK

RELYRELY

Tells Oracle that it should rely on the data complying with the constraint

Basically you are asking the optimizer to trust you to guarantee the data

RELY allows the optimizer to 'use' a NOVALIDATE constraint— Main relevance is for materialized views

ALTER TABLE emp ADD CONSTRAINT sal_ck CHECK (comm = sal) ENABLE NOVALIDATE RELY;

Page 38: Managing Declarative Constraints

38Carl Dudley – University of Wolverhampton, UK

Constraints and IndexesConstraints and Indexes

Constraints are logical entities

Indexes are physical structures

Primary key and unique constraints do not theoretically require indexes— An index is used (and built if needed) to enhance performance

• Full table scan could be used to check for duplicate values

A non-unique index can support a constraint— Must be used for deferred constraints— May be used if it already exists and has the chosen primary key column(s)

as the leading edge— Remains live if the constraint is dropped, unless DROP INDEX is used— Can be selected from the set of suitable indexes with USING INDEX

• Or built on creation of the constraint with CREATE INDEX

Page 39: Managing Declarative Constraints

39Carl Dudley – University of Wolverhampton, UK

Support for Legal SQL StatementsSupport for Legal SQL Statements

UPDATE emp SET empno = empno + 5;

Empno

1

2

3

4

5

6

7

8

Empno

1

2

3

4

5

6

7

8

6^

7^

8^

9

10

11

12

13

Yet another reason why you should not attempt to substitute constraints with your own code— Non-unique index support tends to generate more redo

before after

Page 40: Managing Declarative Constraints

40Carl Dudley – University of Wolverhampton, UK

Non-Unique Indexes - Examples of UseNon-Unique Indexes - Examples of Use

ALTER TABLE emp ADD CONSTRAINT pk_emp PRIMARY KEY(empno) USING INDEX(CREATE UNIQUE INDEX twocol

ON emp(empno,ename));

ORA-14196: Specified index cannot be used to enforce the constraint.

Existing indexes can be used— Could help minimize number of required indexes— Can overload unique index with extra columns to avoid table access

Presence of additional columns works only for Non-unique indexes

ALTER TABLE emp ADD CONSTRAINT pk_emp PRIMARY KEY(empno) USING INDEX(CREATE INDEX empno_ename ON emp(empno,ename));

Default : KEEP for non-unique indexes DROP for unique indexes

ALTER TABLE emp DROP PRIMARY KEY [KEEP | DROP INDEX]; KEEP allows Nulls

Page 41: Managing Declarative Constraints

41Carl Dudley – University of Wolverhampton, UK

Efficient Use of Integrity Constraints: A ProcedureEfficient Use of Integrity Constraints: A Procedure

Using states of integrity constraints in the following order can ensure the best benefits:1. Place constraint in disable state2. Perform the DML operation (load, export, import).3. Enable the constraint in novalidate state4. Fully enable the constraint (validate)

Some benefits of using constraints in this order are:— No locks are held— All constraints can go to enable state concurrently

• Constraint enabling is done in parallel— Concurrent activity on table is permitted

Page 42: Managing Declarative Constraints

42Carl Dudley – University of Wolverhampton, UK

ConstraintsConstraints

Constraints – Origins and Syntax

NULLs and Foreign Key Constraints

Deferring and Enforcing Constraints

Data Dictionary Support

Complex Constraints and Query Transformations

Page 43: Managing Declarative Constraints

43Carl Dudley – University of Wolverhampton, UK

Constraints in the Data DictionaryConstraints in the Data Dictionary

Details of constraints can be found in user_constraints

The constraint_type column can have the following values

C : Check constraint (tables only)P : Primary key constraintR : Foreign key constraintU : Unique key constraintV : WITH CHECK OPTION constraint on a

viewO : Read only view (not table)F : Constraint involving a REF columnS : Supplemental LoggingH : Hash expression

TABLE_NAME CONSTRAINT_NAME CONSTRAINT_TYPE R_CONSTRAINT_NAME STATUS-------------- ---------------- --------------- ----------------- --------DEPT DEPT_PK P ENABLEDEMP EMP_PK P ENABLEDEMP EMP_JOB_CK C DISABLEDEMP SYS_C001415 C ENABLEDEMP EMP_DEPT_FK R DEPT_PK ENABLEDEMP SYS_C011791 ?

Page 44: Managing Declarative Constraints

44Carl Dudley – University of Wolverhampton, UK

Constraints in the Data Dictionary (continued)Constraints in the Data Dictionary (continued)

Columns suffering constraints are found in user_cons_columns

SELECT constraint_name ,table_name ,column_name ,position FROM user_cons_columns;

CONSTRAINT_NAME TABLE_NAME COLUMN_NAME POSITION----------------- ------------ ------------ --------EMP_JOB$DEPTNO_UK EMP JOB 1EMP_JOB$DEPTNO_UK EMP DEPTNO 2DEPT_PK DEPT DEPTNO 1EMP_PK EMP EMPNO 1

Page 45: Managing Declarative Constraints

45Carl Dudley – University of Wolverhampton, UK

CREATE VIEW v2 AS SELECT * FROM emp;

ALTER VIEW v2 ADD PRIMARY key(empno) DISABLE NOVALIDATE;

ALTER TABLE emp DROP PRIMARY KEY;

SELECT constraint_name ,constraint_type ,table_name ,status ,validated ,rely ,invalid ,view_related FROM user_constraints WHERE invalid IS NOT NULL;

CONSTRAINT_NAME C TABLE_NAME STATUS VALIDATED RELY INVALID VIEW_RELATED--------------- - ---------- -------- ------------- ---- ------- --------------SYS_C0011765 P V2 DISABLED NOT VALIDATED INVALID DEPEND ON VIEW

Constraints in the Data Dictionary (continued)Constraints in the Data Dictionary (continued)

Example of INVALID constraint

Page 46: Managing Declarative Constraints

46Carl Dudley – University of Wolverhampton, UK

Constraints in the data Dictionary (continued)Constraints in the data Dictionary (continued)

Supplemental logging shows up in user_constraints— Log groups are not shown— The constraint_type is shown as ‘?’ (NOT ‘S’)

ALTER TABLE emp ADD PRIMARY key(empno);

ALTER TABLE emp ADD SUPPLEMENTAL LOG DATA(PRIMARY KEY) COLUMNS;

ALTER TABLE emp ADD SUPPLEMENTAL LOG DATA(ALL) COLUMNS;

ALTER TABLE emp ADD SUPPLEMENTAL LOG GROUP sal_comm(sal,comm);

SELECT constraint_name, constraint_type, table_name FROM user_constraints WHERE table_name = 'EMP';

CONSTRAINT_NAME C TABLE_NAME------------------------------ - ------------------------------SYS_C0011794 P EMPSYS_C0011795 ? EMPSYS_C0011796 ? EMP

Page 47: Managing Declarative Constraints

47Carl Dudley – University of Wolverhampton, UK

Types of Constraints in the DictionaryTypes of Constraints in the Dictionary

Definition of dba_constraints shows some of the types as ‘?’ or not at all— type# can have a value of 1-17 in cdef$

decode(c.type#, 1, 'C', 2, 'P', 3, 'U', 4, 'R', 5, 'V', 6, 'O', 7, 'C', '?'), : :and c.type# != 8 /* don't include hash expressions */and c.type# != 12 /* don't include log groups */

Page 48: Managing Declarative Constraints

48Carl Dudley – University of Wolverhampton, UK

dbms_metadata Supportdbms_metadata Support

SELECT dbms_metadata.get_dependent_ddl ( 'REF_CONSTRAINT', 'EMP' ) fks_on_emp FROM dual;

FKS_ON_EMP------------------------------------------------------------ ALTER TABLE "SCOTT"."EMP" ADD FOREIGN KEY ("DEPTNO") REFERENCES "SCOTT"."DEPT”(“DEPTNO”) ENABLE ALTER TABLE "SCOTT"."EMP" ADD CONSTRAINT “MGR_FK” FOREIGN KEY (“MGR")REFERENCES "SCOTT".“EMP”(“EMPNO”) ENABLE

SELECT * FROM ( SELECT c.table_name, cc.column_name, cc.position column_position FROM user_constraints c, user_cons_columns cc WHERE c.constraint_name = cc.constraint_name AND c.constraint_type = 'R' MINUS SELECT i.table_name, ic.column_name, ic.column_position FROM user_indexes i, user_ind_columns ic WHERE i.index_name = ic.index_name )ORDER BY table_name, column_position;

Script showing unindexed foreign key columns

Page 49: Managing Declarative Constraints

49Carl Dudley – University of Wolverhampton, UK

ConstraintsConstraints

Constraints – Origins and Syntax

NULLs and Foreign Key Constraints

Deferring and Enforcing Constraints

Data Dictionary Support

Complex Constraints and Query Transformations

Page 50: Managing Declarative Constraints

50Carl Dudley – University of Wolverhampton, UK

Foreign Keys Referencing Non-unique ColumnsForeign Keys Referencing Non-unique Columns

Problem :

Need to enforce a foreign key constraint on the occupation column in the empdep table based on data in the emp table— Requires a reference to a non-unique column (job) in the emp table

Reference a materialized view carrying only unique values of job

EMPNO JOB----- -------- 7366 SALESMAN 7500 MANAGER 7902 MANAGER 7566 CLERK 7934 SALESMAN

JOB--------SALESMANMANAGERCLERK

ENAME OCCUPATION-------- ----------WOODS CLERKJOHNSON MANAGERCOX CLERKPITT CLERKSPINK SALESMANDRAPER MANAGER

EMP

EMP_V1

EMPDEP

primary keyforeign key

Page 51: Managing Declarative Constraints

51Carl Dudley – University of Wolverhampton, UK

Enforcing Foreign Keys Without Primary keysEnforcing Foreign Keys Without Primary keys

CREATE MATERIALIZED VIEW LOG ON emp WITH ROWID,PRIMARY KEY,SEQUENCE(job) INCLUDING NEW VALUES;

CREATE MATERIALIZED VIEW emp_v1REFRESH FAST ON COMMITENABLE QUERY REWRITEAS SELECT job FROM emp GROUP BY job;

ALTER MATERIALIZED VIEW emp_v1 ADD PRIMARY KEY (job);

ALTER TABLE empdep ADD CONSTRAINT empdep_emp_v1 FOREIGN KEY (occupation) REFERENCES emp_v1;

UPDATE empdep SET occupation = 'X';ORA-02291: integrity constraint (SCOTT.EMPDEP_EMP_V1) violated - parent key not found

job is not unique in emp

job is unique in MV

empdep must have only jobs in the emp table

Page 52: Managing Declarative Constraints

52Carl Dudley – University of Wolverhampton, UK

Enforcing Complex Constraints with Materialized ViewsEnforcing Complex Constraints with Materialized Views

Limit the total amount_sold of a single product sold through a single channel to 563000000

Create a materialized view, prod_chan_mv, which returns the maximum amount sold for any product on any channel

CREATE MATERIALIZED VIEW prod_chan_mvBUILD IMMEDIATEREFRESH FAST ON COMMITASSELECT prod_id, channel_id, SUM(amount_sold) sum_amount_soldFROM salesGROUP BY prod_id, channel_id;

— REFRESH FAST ON COMMIT is necessary• The constraint must be checked each time a change to the sales table

is committed

Page 53: Managing Declarative Constraints

53Carl Dudley – University of Wolverhampton, UK

Enforcing Complex Constraints with Materialized Views (continued)Enforcing Complex Constraints with Materialized Views (continued)

Find the maximum amount sold across all products within channels

SELECT prod_id, channel_id, sum_amount_soldFROM prod_chan_mvWHERE sum_amount_sold = (SELECT MAX(sum_amount_sold) FROM prod_chan_mv);

PROD_ID CHANNEL_ID SUM_AMOUNT_SOLD---------- ---------- --------------- 18 3 562565473

As the maximum amount sold was 562565473 it is sensible to make the constraint on the sum_amount_sold column 563000000

ALTER TABLE prod_chan_mvADD CONSTRAINT amount_sold_checkCHECK (sum_amount_sold < 563000000)DEFERRABLE;

— Constraint is checked on commit rather than on update

Page 54: Managing Declarative Constraints

54Carl Dudley – University of Wolverhampton, UK

Testing Complex Constraint Checking with Materialized Views Testing Complex Constraint Checking with Materialized Views

Insert a row with an amount_sold value of 8000000 which causes the constraint on the MV to be violated

INSERT INTO sales VALUES(18, 1, '01-jan-2002', 3, 999, 1, 8000000);

Constraint is not enforced until the commit (and refresh) takes place

SQL> COMMIT;COMMITERROR at line 1:ORA-12048: error encountered while refreshing materialized View "SH"."PROD_CHAN_MV"ORA-02290: check constraint (SH.AMOUNT_SOLD_CHECK) violated

Could lead to large numbers of rows in the MV when constraint is repeatedly NOT violated

Page 55: Managing Declarative Constraints

55Carl Dudley – University of Wolverhampton, UK

Attempt to Avoid Storage Overheads when Enforcing Constraints with Materialized Views Attempt to Avoid Storage Overheads when Enforcing Constraints with Materialized Views

A HAVING clause makes it possible to collect into the MV only those rows that have amount_sold greater than 563000000

CREATE MATERIALIZED VIEW prod_chan_mvBUILD IMMEDIATEREFRESH FAST ON COMMITASSELECT prod_id ,channel_id ,SUM(amount_sold) sum_amount_soldFROM salesGROUP BY prod_id, channel_idHAVING SUM(amount_sold) > 563000000;

BUT there is a limitation— The presence of HAVING makes the view a ‘complex MV’ which is not able

to be refreshed on commit• Still the case in Oracle11g

Page 56: Managing Declarative Constraints

56Carl Dudley – University of Wolverhampton, UK

Enforcing Complex Constraints with Materialized View JoinsEnforcing Complex Constraints with Materialized View Joins

Problem – employees are assigned to projects

Assignment period must be contained within the duration of the project

Tables :

CREATE TABLE proj ( projno NUMBER PRIMARY KEY ,start_date DATE NOT NULL ,end_date DATE NOT NULL);

CREATE TABLE proj_asst ( empno NUMBER NOT NULL ,projno NUMBER NOT NULL ,start_date DATE NOT NULL ,end_date DATE NOT NULL ,PRIMARY KEY (empno, projno, start_date));

Page 57: Managing Declarative Constraints

57Carl Dudley – University of Wolverhampton, UK

Sample Input DataSample Input Data

proj

PROJNO START_DATE END_DATE------ ---------- --------- 1 01-AUG-09 31-AUG-09 2 22-JUL-09 29-SEP-09 3 01-MAR-08 30-APR-08

proj_asst

EMPNO PROJNO START_DATE END_DATE----- ------ ---------- --------- 7369 1 12-AUG-09 19-AUG-09 7369 2 21-AUG-09 28-AUG-09 7369 2 18-AUG-09 25-SEP-09 7369 1 01-AUG-09 31-AUG-09 7369 1 02-JAN-08 31-MAY-08 7369 3 23-SEP-09 30-SEP-09 Bad rows

Page 58: Managing Declarative Constraints

58Carl Dudley – University of Wolverhampton, UK

The Materialized ViewThe Materialized View

CREATE MATERIALIZED VIEW LOG ON proj WITH ROWID(projno,start_date,end_date) INCLUDING NEW VALUES;

CREATE MATERIALIZED VIEW LOG ON proj_asst WITH ROWID(empno,projno,start_date,end_date) INCLUDING NEW VALUES;

CREATE MATERIALIZED VIEW proj_asst_mv REFRESH FAST ON COMMIT AS SELECT pa.projno ,pa.start_date pa_start_date ,pa.end_date pa_end_date ,p.start_date p_start_date ,p.end_date p_end_date ,pa.rowid pa_rowid ,p.rowid p_rowid FROM proj_asst pa ,proj p WHERE pa.projno = p.projno AND NOT (pa.start_date >= p.start_date AND pa.end_date <= p.end_date);

View designed to collect only the 'bad' rows

Page 59: Managing Declarative Constraints

59Carl Dudley – University of Wolverhampton, UK

The Constraint to Avoid Storage OverhaeadThe Constraint to Avoid Storage Overhaead

MV is designed to capture as part of the transaction, only the invalid rows

The CHECK constraint prevents these rows being inserted into the view— Thus causing the initiating transaction to fail

• Valid rows are not considered for the MV – no storage overhead

ALTER TABLE proj_asst_mv ADD CONSTRAINT proj_asst_mv_ckdates CHECK (pa_start_date >= p_start_date AND pa_end_date <= p_end_date) DEFERRABLE;

The MV will always be empty — On 11g Release2 with DEFERRED_SEGMENT_CREATION it may never be

built!

Page 60: Managing Declarative Constraints

60Carl Dudley – University of Wolverhampton, UK

Complex 'Constraints' with Unique IndexesComplex 'Constraints' with Unique Indexes

Suppose we can have a maximum of only one manager in each department

CREATE UNIQUE INDEX emp_ind ON emp (CASE WHEN job = 'MANAGER' THEN TO_CHAR(deptno)||job ELSE NULL END);

Ensures unique combinations of job and deptno columns only for managers

— But ---Index can be dropped— Need to think about Null values

UPDATE emp SET job = 'MANAGER' WHERE ename = 'SMITH';ORA-00001: unique constraint (SCOTT.EMP_IND) violated

UPDATE emp SET job = 'CLERK';14 rows updated.

Page 61: Managing Declarative Constraints

61Carl Dudley – University of Wolverhampton, UK

NOT NULL Constraints Effects on QueriesNOT NULL Constraints Effects on Queries

Specify NOT NULL constraints wherever possible— Cuts down tests for, and coping with, possibility of NULLs— Could allow greater use of indexes

• Query transformations• Fast Full Index scans

• Indexes are 'skinny'• Could eliminate sorts

Inserting nulls into NOT NULL columns

Inserting nulls into NOT NULL DEFERRABLE columns

ORA-01400: cannot insert NULL into ("SCOTT"."EMP"."DEPTNO")

ORA-02290: check constraint (SCOTT.SYS_C0012066) violated

Page 62: Managing Declarative Constraints

62Carl Dudley – University of Wolverhampton, UK

Behaviour with NOT NULL ConstraintsBehaviour with NOT NULL Constraints

Table empn has ~ 60000 rows— job column does not contain any nulls and is not indexed

Constraint type No constraint

NOT NULL NOT NULL NOVALIDATE

NOT NULL DEFERRED

Consistent gets 397 0 397 397

NOT NULL constraint has prevented an entire table access for this query

NULL IS NOT NULL is added to the query as a filter— If in NOVALIDATE or DEFERRED state, the optimization is lost

• Think carefully before using these states— Does not seem to apply to virtual columns --??

Enforcing not null via CHECK syntax does not have this optimization effect— null$ in col$ remains set to 0

SELECT * FROM empn WHERE job IS NULL;

Page 63: Managing Declarative Constraints

63Carl Dudley – University of Wolverhampton, UK

NOT NULL Constraints and the OptimizerNOT NULL Constraints and the Optimizer

Wide 1m row emp table with 500 extra characters in each row— Five different jobs in an indexed job column— Two queries under test

SELECT DISTINCT job FROM emp; --Q1SELECT job,COUNT(*) FROM emp GROUP BY job; --Q2

Q1 No constraint on job NOT NULL constraint on job

Query Plan HASH UNIQUE TABLE ACCESS FULL EMP

HASH UNIQUE INDEX FAST FULL SCAN EMP$JOB

Disk Reads 70805 2419

CPU Time 0.87s 0.46s

Elapsed Time

12.19s 0.85s

fat table skinny index

Page 64: Managing Declarative Constraints

64Carl Dudley – University of Wolverhampton, UK

NOT NULL Constraints and the Optimizer (continued)NOT NULL Constraints and the Optimizer (continued)

If OPTIMIZER_MODE = FIRST_ROWS_1, Q1 with job as NOT NULL gives :

SELECT job,COUNT(*) FROM emp GROUP BY job; --Q2

SORT GROUP BY NOSORT INDEX FULL SCAN EMP$JOB

Q2 No constraint on job NOT NULL constraint on job

Query Plan HASH GROUP BY TABLE ACCESS FULL EMP

HASH GROUP BY INDEX FAST FULL SCAN EMP$JOB

Disk Reads 70805 2419

CPU Time 0.95s 0.65s

Elapsed Time

12.40s 1.10s

Page 65: Managing Declarative Constraints

65Carl Dudley – University of Wolverhampton, UK

NOT IN subqueriesNOT IN subqueries

SELECT * FROM dept WHERE deptno NOT IN (SELECT deptno FROM emp);

No constraint on deptno NOT NULL constraint on deptno

Query Plan

HASH JOIN ANTI NA TABLE ACCESS FULL EMP TABLE ACCESS FULL DEPT

HASH JOIN ANTI SNA TABLE ACCESS FULL DEPT INDEX FAST FULL SCAN EMP$DEPTNO

Disk Reads

70811 1801

CPU Time

0.84s 0.53s

Elapsed Time

12.24s 0.83s

— NA : NULL Aware anti-join— SNA : Single NULL Aware anti-join

10g does :

Both are 11g features

FILTER TABLE ACCESS FULL DEPT TABLE ACCESS FULL EMP

Page 66: Managing Declarative Constraints

66Carl Dudley – University of Wolverhampton, UK

Counting the Rows in a TableCounting the Rows in a Table

NULL values not stored in Btree indexes— Oracle cannot guarantee that the count of index entries is equal to number

of rows in table— Full table scan must be executed - slow

SELECT COUNT(*) FROM s;

COUNT(*)-------- 918843

Elapsed: 00:00:03.17

-------------------------------------------------------------------| Id | Operation | Name | Rows | Cost (%CPU)| Time |-------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 5250 (1)| 00:01:03 || 1 | SORT AGGREGATE | | 1 | | || 2 | TABLE ACCESS FULL| S | 918K| 5250 (1)| 00:01:03 |-------------------------------------------------------------------

Statistics----------------------------------------------------------19214 consistent gets

Page 67: Managing Declarative Constraints

67Carl Dudley – University of Wolverhampton, UK

Counting the Rows in a Table (continued)Counting the Rows in a Table (continued)

Inform Oracle that there cannot be any NULLs in seqid via a constraint

Index will now be used to count the rows – fast— Oracle knows that scanning the index will definitely give the right answer

SELECT COUNT(*) FROM s;

COUNT(*)-------- 918843

Elapsed: 00:00:00.07-------------------------------------------------------------------------| Id | Operation | Name | Rows | Cost (%CPU)| Time |-------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 562 (2)| 00:00:07 || 1 | SORT AGGREGATE | | 1 | | || 2 | INDEX FAST FULL SCAN| S$SEQID | 918K| 562 (2)| 00:00:07 |-------------------------------------------------------------------------

Statistics----------------------------------------------------------2062 consistent gets

ALTER TABLE s MODIFY seqid NOT NULL;

CHECK (sal IS NOT NULL)does not work in this way

Page 68: Managing Declarative Constraints

68Carl Dudley – University of Wolverhampton, UK

Constraints and Query TransformationConstraints and Query Transformation

Constrain jobs in a top_jobs table

SELECT * FROM top_jobs;

JOB_ID JOB MAXSAL------ --------- ------ 1 PRESIDENT 6000 2 MANAGER 4000 3 ANALYST 3500

ALTER TABLE top_jobs ADD CONSTRAINT ck_top_jobs CHECK (job IN ('PRESIDENT','MANAGER','ANALYST'));

CREATE INDEX emp$job ON emp(job);

Index the job column in the emp table

SELECT ename,job,sal,t.maxsal FROM emp e,top_jobs t WHERE t.job = e.job AND e.deptno = 10;

Join top_jobs to emp

Page 69: Managing Declarative Constraints

69Carl Dudley – University of Wolverhampton, UK

Constraints and Query Transformation (continued)Constraints and Query Transformation (continued)

Query transformation takes place

NESTED LOOPS NESTED LOOPS TABLE ACCESS FULL TOP_JOBS INDEX RANGE SCAN EMP$JOB TABLE ACCESS BY INDEX ROWID EMPaccess("T"."JOB"="E"."JOB")filter("E"."DEPTNO"10)

HASH JOIN INLIST ITERATOR TABLE ACCESS BY INDEX ROWID EMP INDEX RANGE SCAN EMP$JOB TABLE ACCESS FULL TOP_JOBS access("T"."JOB"="E"."JOB") filter("E"."DEPTNO"=10) access("E"."JOB"='ANALYST' OR "E"."JOB"='MANAGER' OR "E"."JOB"='PRESIDENT')

If the constraint is removed a very different plan is obtained

Page 70: Managing Declarative Constraints

70Carl Dudley – University of Wolverhampton, UK

Conditional Constraints using DECODE or CASEConditional Constraints using DECODE or CASE

An active project has status = 1, otherwise it is archived

The job has to be unique in the same teamid for the active projects— Means teamid and job have to be unique while status = 1

Solved with a function based index (FBI) using DECODE or CASE

CREATE TABLE project ( project_ID NUMBER PRIMARY KEY ,teamid NUMBER ,job VARCHAR2(100) ,status NUMBER(1));

CREATE UNIQUE INDEX project_idx ON project ( DECODE(status, 1, teamid, NULL ), DECODE(status, 1, job, NULL ));

CREATE UNIQUE INDEX project_idx ON project ( CASE WHEN status = 1 THEN teamid ELSE NULL END, CASE WHEN status = 1 THEN job ELSE NULL END);

Page 71: Managing Declarative Constraints

71Carl Dudley – University of Wolverhampton, UK

Join EliminationJoin Elimination

emp table has deptno column as a foreign key referencing dept

SELECT e,empno, e.enameFROM emp e, dept dWHERE e.deptno = d.deptno;

---------------------------------|Operation | Name |Rows |---------------------------------|SELECT STATEMENT | | 14|| TABLE ACCESS FULL| EMP | 14|---------------------------------Predicate Information-----------------------------1 - filter("EMP"."DEPTNO" IS NOT NULL)

— Join to dept table is eliminated in 10.2

SELECT e.empno, e.enameFROM emp eWHERE NOT EXISTS (SELECT 1 FROM dept d WHERE d.deptno = e.deptno)

— Anti-Join to dept table is eliminated in 11.1

Predicate added by Oracle

Page 72: Managing Declarative Constraints

72Carl Dudley – University of Wolverhampton, UK

Outer Join EliminationOuter Join Elimination

Can occur even without PK-FK constraints— dept table simply has a unique constraint on deptno

Find employees whether or not in a department

SELECT e.empno, e.enameFROM emp e, dept dWHERE e.deptno = d.deptno(+);

---------------------------------|Operation | Name |Rows |---------------------------------|SELECT STATEMENT | | 14|| TABLE ACCESS FULL| EMP | 14|---------------------------------

— Join to dept table is eliminated in 11.1

— EVERY row in emp is guaranteed to appear ONCE in output• Unique constraint on dept.deptno ensures this

Page 73: Managing Declarative Constraints

73Carl Dudley – University of Wolverhampton, UK

Join Elimination in ViewsJoin Elimination in Views

Badly written queries can benefit from join elimination— Unlikely situation?

Queries are often actioned on views

------------------------------------------------------| Operation | Name | Rows |------------------------------------------------------| SELECT STATEMENT | | 1 || NESTED LOOPS | | || NESTED LOOPS | | 1 || TABLE ACCESS FULL | EMP | 14 || INDEX UNIQUE SCAN | PK_PROJECTS | 1 || TABLE ACCESS BY INDEX ROWID| PROJECTS | 1 |------------------------------------------------------

http://optimizermagic.blogspot.com/2008/06/why-are-some-of-tables-in-my-query.html

CREATE VIEW ed AS SELECT e.empno, e.ename, e.sal, d.dname, d.loc, p.pnameFROM emp e, dept d, projects pWHERE e.deptno = d.deptnoAND e.proj_id = p.proj_id;

pk-fk constraints are present

No join to dept table

Page 74: Managing Declarative Constraints

74Carl Dudley – University of Wolverhampton, UK

Updatable ViewsUpdatable Views

If dept has primary key on deptno column, emp is key-preserved— Columns derived from emp become key-preserved

• 14 rows in the view and 14 rows in the emp table

CREATE OR REPLACE VIEW deptemp AS SELECT empno ,ename ,job ,mgr ,hiredate ,sal ,comm ,dept.deptno ,dname ,loc FROM emp ,dept WHERE dept.deptno = emp.deptno

Views on more than one table (join-views) have restrictions for updates— Columns which are updatable must be in key-preserved tables— known 1:1 mapping of rows in the view to rows in the underlying base table

Page 75: Managing Declarative Constraints

75Carl Dudley – University of Wolverhampton, UK

Primary Keys and Key-Preserved TablesPrimary Keys and Key-Preserved Tables

Example scenario with no primary key on dept table— Duplicate rows are possible

dept

deptno dname loc------ ------- ----- 30 SALES LEEDS 50 FINANCE YORK 50 DESIGN BATH

emp

empno ename ... deptno------ ----- --- ------ 2561 COOK 30 4590 BROWN 30 1695 GREEN 50

Result of the equi-join on emp and dept— Note that no table is key-preserved, so no columns are updatable

• deptno values in emp map to more than one row in dept table

empno ename ... deptno dname loc------ ----- --- ------ ------- ----- 2561 COOK 30 SALES LEEDS 4590 BROWN 30 SALES LEEDS 1695 GREEN 50 FINANCE YORK 1695 GREEN 50 DESIGN BATH

Page 76: Managing Declarative Constraints

76Carl Dudley – University of Wolverhampton, UK

Updates on ViewsUpdates on Views

SQL> SELECT * FROM deptemp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DNAME LOC----- ------ --------- ---- --------- ---- ---- ------ ---------- -------- 7934 MILLER CLERK 7782 23-JAN-82 1300 10 ACCOUNTING NEW YORK 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 ACCOUNTING NEW YORK 7839 KING PRESIDENT 17-NOV-81 5000 10 ACCOUNTING NEW YORK 7369 SMITH CLERK 7902 17-DEC-80 800 20 RESEARCH DALLAS 7566 JONES MANAGER 7839 02-APR-81 2975 20 RESEARCH DALLAS 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 RESEARCH DALLAS 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 RESEARCH DALLAS 7902 FORD ANALYST 7566 03-DEC-81 3000 20 RESEARCH DALLAS 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 SALES CHICAGO 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 SALES CHICAGO 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 SALES CHICAGO 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 SALES CHICAGO 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 SALES CHICAGO 7900 JAMES CLERK 7698 03-DEC-81 950 30 SALES CHICAGO

The dept table has a primary key on deptno

deptno, dname and loc (columns from dept) are non-updatable

Rows can be deleted but not inserted

Page 77: Managing Declarative Constraints

77Carl Dudley – University of Wolverhampton, UK

DELETE FROM deptempWHERE deptno = 10;

3 rows deleted.

UPDATE deptemp SET job = 'CLERK’WHERE ename = 'FORD';

1 row updated.

UPDATE deptemp SET dname = 'ACCOUNTING' WHERE ename = 'FORD'; ERROR at line 1:ORA-01779: cannot modify a column which maps to a non key-preserved table

Primary key is necessary on the non key-preserved table (dept)— Otherwise no updates or deletes are possible

Updates on Views (continued)Updates on Views (continued)

Page 78: Managing Declarative Constraints

78Carl Dudley – University of Wolverhampton, UK

SummarySummary

Constraints

— Are never circumvented

— Apply to all rows and all applications

— Simple to specify - ‘Once only’ definition

— Efficient

— Cannot be used for complex business rules

— Are not checked until data is written to the database

— Do not implement all referential actions

— Are often incompatible with database triggers

— Can be used by the optimizer to eliminate unnecessary operations

• Join elimination

• SET operator elimination

• Eliminate entire table access(es)

• Maximize the use of Materialized Views

Page 79: Managing Declarative Constraints

Carl Dudley – University of Wolverhampton, UK

Declarative Constraints for Complex Business Rules and Improved

Performance Carl Dudley

University of Wolverhampton, UK

UKOUG SIG Director

[email protected]

Page 80: Managing Declarative Constraints

80Carl Dudley – University of Wolverhampton, UK

Declarative Constraints for Complex Business Rules and Improved

Performance Please Fill Out Your Evaluations

Carl Dudley

University of Wolverhampton