1 / 17

Maintaining Data Integrity

Maintaining Data Integrity. Objectives. After completing this lesson, you should be able to do the following: Implement data integrity constraints Maintain integrity constraints Obtain constraint information from the data dictionary. Data Integrity. Database trigger. Integrity constraint.

marivelc
Download Presentation

Maintaining Data Integrity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Maintaining Data Integrity

  2. Objectives • After completing this lesson, you should be able to do the following: • Implement data integrity constraints • Maintain integrity constraints • Obtain constraint information from the data dictionary

  3. Data Integrity Databasetrigger Integrityconstraint Data Applicationcode Table

  4. Types of Constraints Constraint NOT NULL UNIQUEPRIMARY KEYFOREIGN KEY CHECK Description Specifies that a column cannot contain null values Designates a column or combination of columns as unique Designates a column or combination of columns as the table’s primary keyDesignates a column or combination of columns as the foreign key in a referential integrity constraintSpecifies a condition that each row of the table must satisfy

  5. Constraint States DISABLENOVALIDATE ENABLENOVALIDATE DISABLEVALIDATE ENABLEVALIDATE = = New data Existing data

  6. DML statement Constraint Checking Check nondeferred constraints COMMIT Check deferred constraints

  7. Defining Constraints Immediate or Deferred • Use the SET CONSTRAINTS statement to make constraints either DEFERRED or IMMEDIATE. • The ALTER SESSION statement also has clauses to SET CONSTRAINTS to DEFERRED or IMMEDIATE.

  8. Primary and Unique Key Enforcement Yes Yes Yes Is an index available for use? Constraint Deferrable? Keyenabled? Is the index nonunique? No/Yes No No Use existing index Yes Constraintdeferrable? No No Do not use index Create unique index Create nonunique index

  9. Desired Action • Appropriate Solution • Drop parent table Cascade constraints • Truncate parent table Disable or drop foreign key • Perform DML on child table Ensure that the tablespace containing the parent key is online Drop tablespace containingparent table Use the CASCADE CONSTRAINTS clause Foreign Key Considerations

  10. Defining Constraints WhileCreating a Table CREATE TABLE hr.employee(id NUMBER(7) CONSTRAINT employee_id_pk PRIMARY KEY DEFERRABLE USING INDEX STORAGE(INITIAL 100K NEXT 100K) TABLESPACE indx, last_name VARCHAR2(25) CONSTRAINT employee_last_name_nn NOT NULL, dept_id NUMBER(7)) TABLESPACE users;

  11. Guidelines for Defining Constraints • Primary and unique constraints: • Place indexes in a separate tablespace. • Use nonunique indexes if bulk loads are frequent. • Self-referencing foreign keys: • Define or enable foreign keys after the initial load. • Defer constraint checking.

  12. Enabling Constraints • No locks on table • Primary and unique keys must use nonunique indexes ENABLE NOVALIDATE ALTER TABLE hr.departmentsENABLE NOVALIDATE CONSTRAINT dept_pk;

  13. Enabling Constraints • Locks the table • Can use unique or nonunique indexes • Needs valid table data ENABLE VALIDATE ALTER TABLE hr.employeesENABLE VALIDATE CONSTRAINT emp_dept_fk;

  14. Using the EXCEPTIONS Table • Create the EXCEPTIONS table by running the utlexcpt1.sql script. • Execute the ALTER TABLE statement with EXCEPTIONS option. • Use subquery on EXCEPTIONS to locate rows with invalid data. • Rectify the errors. • Reexecute ALTER TABLE to enable the constraint.

  15. Obtaining Constraint Information • Obtain information about constraints by querying the following views: • DBA_CONSTRAINTS • DBA_CONS_COLUMNS

  16. Summary • In this lesson, you should have learned how to: • Implement data integrity • Use an appropriate strategy to create and maintain constraints • Obtain information from the data dictionary

  17. Practice 13 Overview • This practice covers the following topics: • Creating constraints • Enabling unique constraints • Creating an EXCEPTIONS table • Identifying existing constraint violations in a table, correcting the errors, and reenabling the constraints

More Related