PL/SQL Transactions   «Prev  Next»

Lesson 2Database integrity
ObjectiveDescribe how data integrity is compromised.

Database Integrity

How can data integrity be compromised in an Oracle RDBMS, where the first part of data integrity involves valid relationships between tables and is enforced by foreign key constraints.
Even with robust foreign key constraints in place, data integrity in an Oracle RDBMS can be compromised in several ways. Here's a breakdown of scenarios that can lead to inconsistencies, along with preventive measures: **1. Human Error and Direct Manipulation** * **Accidental Deletion:** Users might accidentally delete rows in a parent table, leaving orphan records in the child table that violate the foreign key relationship. * **Incorrect Updates:** Updating primary key values in the parent table without cascading those changes to related records in the child table can break the referential integrity. * **Bypassing Constraints:** Users with sufficient privileges can disable or temporarily drop foreign key constraints, potentially introducing inconsistent data. **Prevention:** * **Clear Permissions:** Establish strict user roles and permissions to limit who can modify data, especially in key tables. * **Robust Input Validation:** Implement thorough validation in your application layer to prevent incorrect data entry. * **Triggers:** Consider database triggers to automatically enforce cascading updates or deletions if a primary key is modified. **2. Application Logic Failures** * **Bugs in Code:** Errors in your application code might try to insert or update data that violates foreign key relationships, even if the database constraints are theoretically in place. * **Race Conditions:** In multi-user environments, if two transactions happen simultaneously, one might violate the integrity constraints set by the other. **Prevention** * **Rigorous Testing:** Implement thorough unit and integration tests to catch potential issues in application logic that could affect data integrity. * **Transactions:** Use database transactions to ensure related operations happen as a single unit, preventing intermediate states that would violate referential integrity. **3. Data Import Errors** * **Inconsistent Formats:** Data imported from external sources might not conform to the expected format or may contain values that don't match existing entries in the parent table. * **Incomplete Data:** Bulk uploads with missing data in the foreign key column can introduce potential referential integrity errors. **Prevention** * **Data Cleaning:** Validate and clean data before importing it. Establish processes to catch inconsistencies early on. * **Staging Tables:** Utilize staging tables to quarantine and review imported data before inserting it into the main database. **4. Hardware or System Failures** * **Unexpected Shutdowns:** Power outages or hardware crashes during data modification can leave transactions incomplete and the database in an inconsistent state. * **Data Corruption:** Disk or memory errors can corrupt data directly, leading to broken relationships. Prevention
  • Reliable Infrastructure:** Invest in a robust server setup with redundancy and power backups.
  • Regular Backups:** Maintain consistent backups that allow you to restore the database to a known good state if corruption occurs.
**Remember: Foreign key constraints are a crucial safeguard, but they cannot completely eliminate every data integrity risk. A multi-layered approach involving permissions, application logic, data validation, and disaster recovery is essential for preserving data integrity in your Oracle RDBMS.**
Data integrity has two parts in a relational database:
  1. Valid relationships between tables
  2. Complete sets of interrelated data in tables

Valid Relationships between Tables

The first part of data integrity (valid relationships between tables) is enforced by foreign key constraints and requires no special handling in PL/SQL. The second part of data integrity (complete sets of interrelated data in tables) requires careful consideration when designing and coding PL/SQL. This is the issue described in this lesson. The concept of complete sets of data is best understood by looking at an example. To keep data integrity intact, you must be sure that complete sets of related data are added to the database and that the entire set of related data is removed if errors occur. Follow along with the following series of images to see how data integrity might be compromised in a telephone billing system.

Process Thousands of Records

 In this imaginary telephone billing system, thousands of records are processed at the end of each billing cycle.
1) In this imaginary telephone billing system, thousands of records are processed at the end of each billing cycle.
The records are collected from many diverse sources and brought to the central billing system. Let us say that there is an invalid record in the middle of Jody Smith's records.

The central billing system sorts all the information by customer and feeds it all into a PL/SQL procedure
2) The central billing system sorts all the information by customer and feeds it all into a PL/SQL procedure

The procedure processes each record, adding the record to a history table and then updating the summary record for the summer's monthly billing statement
3) The procedure processes each record, adding the record to a history table and then updating the summary record for the summer's monthly billing statement

The invalid record causes the procedure to stop, leaving all the previous customer's records plus some portion of Jody Smith's records in the database.
4) The invalid record causes the procedure to stop, leaving all the previous customer's records plus some portion of Jody Smith's records in the database. At this point data integrity has been compromised.

Your best defense against this sort of data integrity problem is careful use of transaction controls in your procedure. Transaction controls are commands you can use inside PL/SQL to tell Oracle when one transaction starts and ends. Later lessons describe these commands. A transaction is a set of database activity that is handled as a unit by the database. If an error causes any part of the transaction to fail, Oracle automatically undoes the entire set of actions.

Enforcing Data Integrity.

Take advantage of Oracle’s transaction treatment to enforce data integrity. Use transaction controls in your procedure so that logical sets of actions are seen by Oracle as a single transaction. This way, any error found in the transaction causes the entire transaction to be undone and preserves data integrity. The next lesson describes how to implement transaction controls within PL/SQL blocks.

asdfasdfasdf
SEMrush Software 2SEMrush Software Banner 2