Download presentation
Presentation is loading. Please wait.
1
Carl Dudley Tradba Ltd UKOUG Official carl.dudley@wlv.ac.uk
Declarative Constraints for Complex Business Rules and Improved Performance Carl Dudley Tradba Ltd UKOUG Official
2
Constraints 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 – Tradba Ltd Constraints – Origins and Syntax NULLs and Foreign Key Constraints Deferring and Enforcing Constraints Data Dictionary Support Complex Constraints and Query Transformations
3
Constraints – 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
4
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
5
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 be UNIQUE and NOT NULL Automatically indexed (use can be made of an existing index) CREATE TABLE emp (empno NUMBER(4) CONSTRAINT emp_pk PRIMARY KEY, :
6
Primary 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 dept ADD CONSTRAINT dept_pk PRIMARY KEY(deptno);
7
Unique 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 ALTER TABLE emp ADD CONSTRAINT ename_deptno_uk UNIQUE (ename,deptno); 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);
8
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);
9
CHECK Constraint Examples
Ensure gender values are always uppercase ‘m’ or ‘f’ CREATE TABLE emp ( gender VARCHAR2(1) CONSTRAINT gender_ck CHECK (gender IN UPPERCASE(‘m’,’f’)); Ensure that commission is never more than ¼ of an employee’s salary ALTER TABLE emp ( ADD CONSTRAINT comm_ck CHECK (comm < sal*0.25); Ensure that hiredate is not later than the current date ALTER TABLE emp ( ADD CONSTRAINT hiredate_ck CHECK (hiredate <= sysdate); Error because sysdate implies a subquery
10
Referential 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 deptno dname 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS empno ename mgr deptno SMITH ALLEN WARD JONES MARTIN BLAKE Foreign keys Emp Dept
11
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
12
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_fk REFERENCES dept(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 CREATE TABLE ... : divno NUMBER(3), deptno NUMBER(4), CONSTRAINT emp_dept_fk FOREIGN KEY (divno,deptno) REFERENCES dept(divno,deptno);
13
General Foreign Key Constraint Actions
RESTRICT SET NULL SET DEFAULT CASCADE Four options are generally recognised for actions performed by foreign key constraints 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
14
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 emp ADD CONSTRAINT emp_dept_fk FOREIGN KEY (deptno) REFERENCES dept(deptno) ON DELETE CASCADE; DELETE FROM dept WHERE deptno IN (10,20); 2 rows deleted
15
Conterminous Paths TableA Delete Cascade Delete Cascade TableB TableC
Conterminous Paths TableA Delete Cascade Delete Cascade TableB TableC Delete Cascade Delete Restrict TableD Each table has one row with the value ‘x’ What would be the effect of? : DELETE FROM a WHERE col = ‘x’;
16
Enabling/Disabling Constraints
ALTER TABLE emp DISABLE CONSTRAINT emp_pk; 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 ENABLE CONSTRAINT emp_pk;
17
Foreign 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
18
Indexes 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 has a script that generates advice/report Not accurate if foreign key columns present in an index in different order Interprets this as unusable index Changing 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 remove lock problem Column = MGR (1)
19
Constraints 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
20
Constraints Constraints – Origins and Syntax
NULLs and Foreign Key Constraints Deferring and Enforcing Constraints Data Dictionary Support Complex Constraints and Query Transformations
21
Foreign Keys and Nulls 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 Newdept Divno Deptno Desc Finance Sales Operations Design Chemicals Newemp Divno Deptno Ename Smith Adams Carter 2 NULL Best NULL NULL Cox NULL Scott 1 NULL King 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
22
Matching 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
23
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)))
24
Constraints Constraints – Origins and Syntax
NULLs and Foreign Key Constraints Deferring and Enforcing Constraints Data Dictionary Support Complex Constraints and Query Transformations
25
Handling Exceptions ALTER TABLE emp ADD CONSTRAINT emp_pk
To deal with rows which are violating (and preventing) a constraint Construct an exceptions table using the UTLEXCPT script 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_pk PRIMARY KEY (empno) EXCEPTIONS INTO exceptions; ROW_ID OWNER TABLE_NAME CONSTRAINT AAABFJAACAAAFA3AAN SCOTT EMP EMP_PK AAABFJAACAAAFA4AAN SCOTT EMP EMP_PK DELETE FROM emp WHERE ROWID IN (SELECT row_id FROM exceptions WHERE constraint = <constraint_name>);
26
Deferred Constraints ALTER TABLE table_name ADD CONSTRAINT ... :
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];
27
Deferred 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_fk FOREIGN KEY (deptno) REFERENCES dept(deptno) INITIALLY IMMEDIATE DEFERRABLE;
28
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 follows 1. 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
29
Changing the Constraint Mode
Two methods available to toggle constraint modes SET CONSTRAINT Used to change the mode for a single transaction 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_name IMMEDIATE | DEFERRED; SET CONSTRAINTS ALL IMMEDIATE | DEFERRED; ALTER SESSION SET CONSTRAINTS = IMMEDIATE | DEFERRED; ALTER SESSION SET CONSTRAINTS = DEFAULT;
30
Processing the Update Transaction
Set the constraint to be deferred until the end of the transaction Constraint checked when commit occurs Execute both updates These both succeed in changing deptno values in emp and dept from 10 to 99 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;
31
Index 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
32
Enforced Constraints ALTER TABLE emp ADD CONSTRAINT sal_ck
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
33
Validating 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
34
Setting 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 Create a non-unique index on the 'primary key' column(s) 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
35
Non_unique Indexes for Primary Keys
Build a table with duplicate data already present CREATE TABLE empn AS SELECT * FROM emp; INSERT INTO empn SELECT * FROM empn WHERE empno = 7369; Try to enforce a (non-DEFERRABLE) primary key with a unique index ALTER TABLE empn ADD CONSTRAINT empn_pk PRIMARY KEY(empno) NOVALIDATE; ORA-02437: cannot validate (SCOTT.EMPNOVAL_PK) - primary key violated Enforce primary key with non-unique index 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
36
RELY 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;
37
Constraints 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
38
Support for Legal SQL Statements
UPDATE emp SET empno = empno + 5; before after 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
39
Non-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. 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 Existing indexes can be used Could help minimize number of required indexes Can overload unique index with extra columns to avoid table access
40
Efficient Use of Integrity Constraints: A Procedure
Using states of integrity constraints in the following order can ensure the best benefits: Place constraint in disable state Perform the DML operation (load, export, import). Enable the constraint in novalidate state 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
41
Constraints Constraints – Origins and Syntax
NULLs and Foreign Key Constraints Deferring and Enforcing Constraints Data Dictionary Support Complex Constraints and Query Transformations
42
Constraints 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 constraint R : Foreign key constraint U : Unique key constraint V : WITH CHECK OPTION constraint on a view O : Read only view (not table) F : Constraint involving a REF column S : Supplemental Logging H : Hash expression TABLE_NAME CONSTRAINT_NAME CONSTRAINT_TYPE R_CONSTRAINT_NAME STATUS DEPT DEPT_PK P ENABLED EMP EMP_PK P ENABLED EMP EMP_JOB_CK C DISABLED EMP SYS_C C ENABLED EMP EMP_DEPT_FK R DEPT_PK ENABLED EMP SYS_C ?
43
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 EMP_JOB$DEPTNO_UK EMP DEPTNO DEPT_PK DEPT DEPTNO EMP_PK EMP EMPNO
44
Constraints in the Data Dictionary (continued)
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_C P V DISABLED NOT VALIDATED INVALID DEPEND ON VIEW Example of INVALID constraint
45
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_C P EMP SYS_C ? EMP SYS_C ? EMP
46
Types 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# != /* don't include hash expressions */ and c.type# != /* don't include log groups */
47
dbms_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 Script showing unindexed foreign key columns 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;
48
Constraints Constraints – Origins and Syntax
NULLs and Foreign Key Constraints Deferring and Enforcing Constraints Data Dictionary Support Complex Constraints and Query Transformations
49
Foreign 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 SALESMAN MANAGER CLERK ENAME OCCUPATION WOODS CLERK JOHNSON MANAGER COX CLERK PITT CLERK SPINK SALESMAN DRAPER MANAGER EMP EMP_V1 EMPDEP primary key foreign key
50
Enforcing Foreign Keys Without Primary keys
CREATE MATERIALIZED VIEW LOG ON emp WITH ROWID,PRIMARY KEY,SEQUENCE(job) INCLUDING NEW VALUES; CREATE MATERIALIZED VIEW emp_v1 REFRESH FAST ON COMMIT ENABLE QUERY REWRITE AS 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
51
Enforcing Complex Constraints with Materialized Views
Limit the total amount_sold of a single product sold through a single channel to Create a materialized view, prod_chan_mv, which returns the maximum amount sold for any product on any channel CREATE MATERIALIZED VIEW prod_chan_mv BUILD IMMEDIATE REFRESH FAST ON COMMIT AS SELECT prod_id, channel_id, SUM(amount_sold) sum_amount_sold FROM sales GROUP 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
52
Enforcing Complex Constraints with Materialized Views (continued)
Find the maximum amount sold across all products within channels SELECT prod_id, channel_id, sum_amount_sold FROM prod_chan_mv WHERE sum_amount_sold = (SELECT MAX(sum_amount_sold) FROM prod_chan_mv); PROD_ID CHANNEL_ID SUM_AMOUNT_SOLD As the maximum amount sold was it is sensible to make the constraint on the sum_amount_sold column ALTER TABLE prod_chan_mv ADD CONSTRAINT amount_sold_check CHECK (sum_amount_sold < ) DEFERRABLE; Constraint is checked on commit rather than on update
53
Testing Complex Constraint Checking with Materialized Views
Insert a row with an amount_sold value of which causes the constraint on the MV to be violated INSERT INTO sales VALUES(18, 1, '01-jan-2002', 3, 999, 1, ); Constraint is not enforced until the commit (and refresh) takes place SQL> COMMIT; COMMIT ERROR 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
54
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 CREATE MATERIALIZED VIEW prod_chan_mv BUILD IMMEDIATE REFRESH FAST ON COMMIT AS SELECT prod_id ,channel_id ,SUM(amount_sold) sum_amount_sold FROM sales GROUP BY prod_id, channel_id HAVING SUM(amount_sold) > ; 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
55
Enforcing 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));
56
Sample Input Data Bad rows proj PROJNO START_DATE END_DATE
1 01-AUG AUG-09 2 22-JUL SEP-09 3 01-MAR APR-08 proj_asst EMPNO PROJNO START_DATE END_DATE AUG AUG-09 AUG AUG-09 AUG SEP-09 AUG AUG-09 JAN MAY-08 SEP SEP-09 Bad rows
57
View designed to collect
The 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
58
The 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!
59
Complex '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.
60
NOT 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_C ) violated
61
Behaviour with NOT NULL Constraints
Table empn has ~ rows job column does not contain any nulls and is not indexed SELECT * FROM empn WHERE job IS NULL; Constraint type No constraint NOT NULL NOT NULL NOVALIDATE NOT NULL DEFERRED Consistent gets 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
62
NOT 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; Q1 SELECT 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 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
63
NOT NULL Constraints and the Optimizer (continued)
SELECT job,COUNT(*) FROM emp GROUP BY job; --Q2 Q2 No constraint on job NOT NULL constraint on job Query Plan HASH GROUP BY TABLE ACCESS FULL EMP INDEX FAST FULL SCAN EMP$JOB Disk Reads 70805 2419 CPU Time 0.95s 0.65s Elapsed Time 12.40s 1.10s If OPTIMIZER_MODE = FIRST_ROWS_1, Q1 with job as NOT NULL gives : SORT GROUP BY NOSORT INDEX FULL SCAN EMP$JOB
64
NOT IN subqueries Both are 11g features
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 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
65
Counting 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; Oracle has to go to the table to count the rows. The s$seqid index cannot be used because seqid could have null values which would not be held in the index and therefore not counted in an index search. COUNT(*) 918843 Elapsed: 00:00:03.17 | Id | Operation | Name | Rows | Cost (%CPU)| Time | | 0 | SELECT STATEMENT | | | (1)| 00:01:03 | | 1 | SORT AGGREGATE | | | | | | 2 | TABLE ACCESS FULL| S | 918K| (1)| 00:01:03 | Statistics consistent gets
66
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 ALTER TABLE s MODIFY seqid NOT NULL; SELECT COUNT(*) FROM s; Oracle now knows that the number of nodes (entries) in the index MUST equal the number of rows in the table. (There can be no NULLs in the seqid column in the table) So it chooses the smaller index structure to count the rows in the table and hence the consistent gets (reads of Oracle blocks) is now much lower. COUNT(*) 918843 Elapsed: 00:00:00.07 | Id | Operation | Name | Rows | Cost (%CPU)| Time | | 0 | SELECT STATEMENT | | | (2)| 00:00:07 | | 1 | SORT AGGREGATE | | | | | | 2 | INDEX FAST FULL SCAN| S$SEQID | 918K| (2)| 00:00:07 | Statistics 2062 consistent gets CHECK (sal IS NOT NULL) does not work in this way
67
Constraints and Query Transformation
Constrain jobs in a top_jobs table SELECT * FROM top_jobs; JOB_ID JOB MAXSAL 1 PRESIDENT 2 MANAGER 3 ANALYST ALTER TABLE top_jobs ADD CONSTRAINT ck_top_jobs CHECK (job IN ('PRESIDENT','MANAGER','ANALYST')); Index the job column in the emp table CREATE INDEX emp$job ON emp(job); Join top_jobs to emp SELECT ename,job,sal,t.maxsal FROM emp e,top_jobs t WHERE t.job = e.job AND e.deptno = 10;
68
Constraints and Query Transformation (continued)
Query transformation takes place 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 NESTED LOOPS TABLE ACCESS FULL TOP_JOBS INDEX RANGE SCAN EMP$JOB TABLE ACCESS BY INDEX ROWID EMP access("T"."JOB"="E"."JOB") filter("E"."DEPTNO"10)
69
Conditional Constraints using DECODE or CASE
CREATE TABLE project ( project_ID NUMBER PRIMARY KEY ,teamid NUMBER ,job VARCHAR2(100) ,status NUMBER(1)); 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 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);
70
Predicate added by Oracle
Join Elimination emp table has deptno column as a foreign key referencing dept SELECT e,empno, e.ename FROM emp e, dept d WHERE 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 Predicate added by Oracle SELECT e.empno, e.ename FROM emp e WHERE NOT EXISTS (SELECT 1 FROM dept d WHERE d.deptno = e.deptno) Anti-Join to dept table is eliminated in 11.1
71
Outer 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.ename FROM emp e, dept d WHERE e.deptno = d.deptno(+); Join to dept table is eliminated in 11.1 |Operation | Name |Rows | |SELECT STATEMENT | | 14| | TABLE ACCESS FULL| EMP | 14| EVERY row in emp is guaranteed to appear ONCE in output Unique constraint on dept.deptno ensures this
72
Join Elimination in Views
Badly written queries can benefit from join elimination Unlikely situation? Queries are often actioned on views CREATE VIEW ed AS SELECT e.empno, e.ename, e.sal, d.dname, d.loc, p.pname FROM emp e, dept d, projects p WHERE e.deptno = d.deptno AND e.proj_id = p.proj_id; pk-fk constraints are present | Operation | Name | Rows | | SELECT STATEMENT | | | | NESTED LOOPS | | | | NESTED LOOPS | | | | TABLE ACCESS FULL | EMP | | | INDEX UNIQUE SCAN | PK_PROJECTS | | | TABLE ACCESS BY INDEX ROWID| PROJECTS | | No join to dept table
73
Updatable Views 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 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 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
74
Primary 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 4590 BROWN 1695 GREEN 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 SALES LEEDS 4590 BROWN SALES LEEDS 1695 GREEN FINANCE YORK 1695 GREEN DESIGN BATH
75
Updates on Views 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 SQL> SELECT * FROM deptemp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DNAME LOC 7934 MILLER CLERK JAN ACCOUNTING NEW YORK 7782 CLARK MANAGER JUN ACCOUNTING NEW YORK 7839 KING PRESIDENT NOV ACCOUNTING NEW YORK 7369 SMITH CLERK DEC RESEARCH DALLAS 7566 JONES MANAGER APR RESEARCH DALLAS 7788 SCOTT ANALYST APR RESEARCH DALLAS 7876 ADAMS CLERK MAY RESEARCH DALLAS 7902 FORD ANALYST DEC RESEARCH DALLAS 7499 ALLEN SALESMAN FEB SALES CHICAGO 7521 WARD SALESMAN FEB SALES CHICAGO 7654 MARTIN SALESMAN SEP SALES CHICAGO 7698 BLAKE MANAGER MAY SALES CHICAGO 7844 TURNER SALESMAN SEP SALES CHICAGO 7900 JAMES CLERK DEC SALES CHICAGO
76
Updates on Views (continued)
DELETE FROM deptemp WHERE 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
77
Summary 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
78
Carl Dudley Tradba Ltd UKOUG Official carl.dudley@wlv.ac.uk
Declarative Constraints for Complex Business Rules and Improved Performance Carl Dudley Tradba Ltd UKOUG Official
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.