Data integrity

8,185 views

Published on

Published in: Education, Technology, Business
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,185
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
447
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Data integrity

  1. 1. Data Integrity Chapter 11
  2. 2. Definition Correctness and completeness of data in a database
  3. 3. When is data integrity lost? <ul><li>Invalid data added to the database </li></ul><ul><li>Existing data modified to incorrect value </li></ul><ul><li>Changes to database lost </li></ul><ul><li>Changes partially applied </li></ul>
  4. 4. How is data integrity preserved? <ul><li>Through Data Integrity Constraints </li></ul><ul><li>Constraints restrict data values that can be inserted or updated </li></ul>
  5. 5. Types of Data Integrity Constraints <ul><li>Required Data:Columns must contain valid data value in every row,NOT NULL </li></ul><ul><li>Validity Checking:Columns must contain data within the domain,set of values legal for a column-data type,CHECK,Domain </li></ul><ul><li>Entity Integrity:Primary key must contain unique value in each row </li></ul>
  6. 6. Types of Data Integrity Constraints(contd.) <ul><li>Referential Integrity:Foreign Key linking each row of child table to matching primary key value in parent table </li></ul><ul><li>Other data relationships:additional constraints governing legal data values </li></ul><ul><li>Business Rules </li></ul><ul><li>Consistency:Multiple updates involving many tables may make database inconsistent </li></ul>
  7. 7. Required data <ul><li>NOT NULL constraint in CREATE TABLE statement </li></ul><ul><li>Ensures that each INSERT statement specifies a non null value (Error for null) </li></ul><ul><li>Ensures that each UPDATE statement specifies a non null value (Error for null) </li></ul><ul><li>Must be specified at table creation time </li></ul><ul><li>Adding NOT NULL constraint after data values are added may result in error if any null values found for the column </li></ul>
  8. 8. Validity Checking <ul><li>Data type ensures specific values of a type to be present for the column </li></ul><ul><li>Two features to support validity checking-column check constraints and domains </li></ul>
  9. 9. Validity Checking (contd.) <ul><li>Column CHECK constraints – Search condition that produces true/false values </li></ul><ul><li>Specified with CREATE TABLE </li></ul><ul><li>With the CHECK constraint ,DBMS checks value of that column each time a new row is inserted or updated </li></ul><ul><li>Allows if condition true </li></ul><ul><li>Error if condition false </li></ul>
  10. 10. Validity Checking(contd.) CHECK CONSTRAINT EXAMPLE Create table test (rollno number(2) check (rollno between 1 and 80), name varchar2(15) ); Insert into test values(55,’ANANYA’); 1 row inserted Insert into test values(85,’AKSHAT’); ERROR-Check constraint violated
  11. 11. Validity Checking(contd.) Domains (SQL2) <ul><li>Generalizes CHECK Constraint </li></ul><ul><li>Same CHECK constraint can be applied to many different columns within a database through DOMAIN-a collection of legal data values </li></ul><ul><li>Domain defined with a name and a search condition for range of legal data values </li></ul><ul><li>CREATE DOMAIN statement </li></ul>
  12. 12. Validity Checking(contd.) Domain example Create DOMAIN dom_val_bet number(2) CHECK (value between 1 and 60); Create TABLE test1 (rollno dom_val_bet, name varchar2(15) ); Create table test2 (rollno dom_val_bet, marks number(3) );
  13. 13. Validity Checking(contd.) Why use DOMAIN??? <ul><li>Can be used repeatedly simplifying table definitions </li></ul><ul><li>Changing definition later, if required becomes easy </li></ul><ul><li>Specially helpful for large databases </li></ul>
  14. 14. Entity Integrity <ul><li>Each entity is unique </li></ul><ul><li>Table’s PRIMARY KEY must have unique values for each row </li></ul><ul><li>DBMS automatically checks uniqueness of primary key value for each insert or update </li></ul><ul><li>Insert or update to include duplicate value in primary key column results in error </li></ul><ul><li>If required for non-primary key column,UNIQUE constraint or CREATE INDEX </li></ul><ul><li>Uniqueness also enforces NOT NULL for primary key </li></ul>
  15. 15. Referential Integrity <ul><li>Foreign key-primary key relationship </li></ul><ul><li>Each foreign key value in child table must have a corresponding value in primary key column of parent table </li></ul><ul><li>Ensures integrity of parent-child relationship in tables </li></ul>
  16. 16. Referential Integrity Problems <ul><li>Inserting a new child row </li></ul><ul><li>Updating the foreign key in a child row </li></ul><ul><li>Deleting a parent row </li></ul><ul><li>Updating the primary key in a parent row </li></ul>
  17. 17. Delete and Update Rules Delete Rules <ul><li>Restrict delete rule(default) -prevents deletion of a row with children </li></ul><ul><li>Cascade delete rule -when parent row deleted all child rows automatically deleted </li></ul><ul><li>Set null delete rule – when parent row deleted foreign key values of all child rows set to null </li></ul><ul><li>Set default delete rule - when parent row deleted foreign key values of all child rows set to default value set for that column </li></ul>What to do when User tries to delete a row of parent table?
  18. 18. Delete and Update Rules Update Rules <ul><li>Restrict update rule(default) -prevents updation of a row with children </li></ul><ul><li>Cascade update rule -when parent row updated all child rows automatically updated accordingly </li></ul><ul><li>Set null update rule – when parent row updated foreign key values of all child rows set to null </li></ul><ul><li>Set default delete rule - when parent row updated foreign key values of all child rows set to default value set for that column </li></ul>What to do when User tries to update a row of parent table?
  19. 19. Cascaded deletes and Updates <ul><li>Restrict is a single level rule </li></ul><ul><li>Set Null and Set Default are two level rules </li></ul><ul><li>Cascade can be a multi level rule </li></ul><ul><li>Cascade delete rules must be used with special care as it may cause widespread deletion if used incorrectly </li></ul><ul><li>Cascade update affects more only if foreign key of child table is also its primary key </li></ul>
  20. 20. Referential Cycles <ul><li>When a child table’s primary key is referred by a column of the parent’s table a referential cycle is formed. </li></ul>Dept Emp Deptno Dname loc mgr Empno Ename dob sal deptno comm
  21. 21. Referential cycles (contd.) <ul><li>Inserting a row in EMP table not allowed as there must be corresponding deptno value in DEPT table . </li></ul><ul><li>Inserting a row in DEPT table not allowed as there must be corresponding value for mgr in EMP(empno) </li></ul><ul><li>Only possible when mgr is allowed null value </li></ul><ul><li>Sometimes it is convenient if constraints are not checked immediately-deferred checking </li></ul><ul><li>In referential cycles cascade delete rule should be given very cautiously </li></ul>
  22. 22. Foreign Keys and Null Values <ul><li>Allowed to have Null values </li></ul><ul><li>For handling Null values in foreign key columns options provided in CREATE TABLE statement: </li></ul><ul><li>Match Full :foreign keys in child table fully match primary key in parent table </li></ul><ul><li>Match Partial :Allows null values in parts of the foreign key,non-null values match the corresponding parts of primary key in parent table </li></ul>
  23. 23. Advanced Constraint Capabilities FOUR types of Constraints <ul><li>Column Constraints: appear in individual column definitions in CREATE TABLE statement </li></ul><ul><li>eg. </li></ul><ul><li>CREATE TABLE TEST2 </li></ul><ul><li>( ROLLNO NUMBER PRIMARY KEY, </li></ul><ul><li>NAME VARCHAR2(15) ); </li></ul>
  24. 24. Advanced Constraint Capabilities (contd.) <ul><li>Domains: </li></ul><ul><li>specialized type of column constraint </li></ul><ul><li>Provide capability to define new data types </li></ul><ul><li>Predefined data type + additional constraints </li></ul><ul><li>once created,can be used in place of data type </li></ul><ul><li>Defined outside the table with CREATE DOMAIN statement </li></ul>
  25. 25. Advanced Constraint Capabilities (contd.) <ul><li>Table Constraints:usually appear as a group after column definitions </li></ul><ul><li>Eg. Create table offices </li></ul><ul><li>( office number(5) not null , </li></ul><ul><li>city varchar2(15), </li></ul><ul><li>mgr number(4), </li></ul><ul><li>primary key(office), </li></ul><ul><li>foreign key(mgr) references salesreps(empno) </li></ul><ul><li>on delete set null, </li></ul><ul><li>unique(city) ); </li></ul>
  26. 26. Advanced Constraint Capabilities (contd.) <ul><li>Assertions </li></ul><ul><li>Specified outside table definition </li></ul><ul><li>Specifies relationship between data values crossing multiple tables </li></ul><ul><li>Create large database processing overhead so defined with care </li></ul><ul><li>Eg. Create assertion cr_ord </li></ul><ul><li>check(customer.credit_limit<= </li></ul><ul><li>select sum(orders.amount) </li></ul><ul><li>from orders </li></ul><ul><li>where orders.cust=customer.cust_num); </li></ul>
  27. 27. SQL2 Constraint Types <ul><li>1. NOT NULL </li></ul><ul><li>Prevents null values </li></ul><ul><li>Column constraint </li></ul>
  28. 28. SQL2 Constraint Types (contd.) <ul><li>PRIMARY KEY unique and not null </li></ul><ul><li>Can be a column or a table constraint </li></ul><ul><li>When a single column,column constraint is convenient </li></ul><ul><li>When on multiple columns, table constraint must </li></ul>
  29. 29. SQL2 Constraint Types(contd.) <ul><li>UNIQUE </li></ul><ul><li>Column or table constraint </li></ul><ul><li>When for a single column,column constraint is convenient </li></ul><ul><li>When on combination of multiple columns, table constraint must </li></ul>
  30. 30. SQL2 Constraint Types (contd.) <ul><li>4. Referential Integrity (Foreign Key) Constraint </li></ul><ul><li>Column or table constraint </li></ul><ul><li>When for a single column,column constraint is convenient </li></ul><ul><li>When on combination of multiple columns, table constraint must </li></ul><ul><li>When many foreign key relationships to many tables, convenient to gather all together as table constraint </li></ul>
  31. 31. SQL2 Constraint Types (contd.) <ul><li>5. Check Constraint </li></ul><ul><li>Column or table constraint </li></ul><ul><li>Only constraint that forms part of domain and assertion definition </li></ul><ul><li>Specified as search condition </li></ul><ul><li>Constraint satisfied if condition returns true </li></ul>
  32. 32. SQL2 Constraint Types (contd.) <ul><li>For small databases constraint names not necessary </li></ul><ul><li>For larger databases ,constraint names must be given </li></ul><ul><li>CHECK constraint in an assertion must have a name </li></ul>
  33. 33. Deferred Constraint Checking <ul><li>Constraints are checked with every insert , update and delete </li></ul><ul><li>Deferred constraint checking allows checking constraints at the completion of a transaction(COMMIT) </li></ul><ul><li>Useful when several updates required at once to keep database consistent </li></ul>
  34. 34. Deferred Constraint Checking(contd.) <ul><li>At the time of constraint creation two options: </li></ul><ul><li>DEFERRABLE: checking can be deferred to the end of the transaction </li></ul><ul><li>NOT DEFERRABLE (default): checking cannot be deferred. </li></ul>
  35. 35. Deferred Constraint Checking(contd.) <ul><li>For DEFERRABLE: </li></ul><ul><li>Initially Immediate (default): starts out as an immediate constraint </li></ul><ul><li>Initially Deferred :starts out as a deferred constraint </li></ul><ul><li>Create assertion quota_totals </li></ul><ul><li>Check ((offices.quota=sum(salesreps.quota)) and </li></ul><ul><li>(salesreps.rep_office=offices.office)) </li></ul><ul><li>deferrable initially immediate; </li></ul>
  36. 36. Deferred Constraint Checking(contd.) SET CONSTRAINTS <ul><li>SET CONSTRAINTS :to control the immediate or deferred processing of constraints </li></ul><ul><li>Set constraints quota_totals deferred </li></ul><ul><li>Insert……….. </li></ul><ul><li>Delete……….. </li></ul><ul><li>Commit </li></ul><ul><li>Insert………….. </li></ul><ul><li>Initially the constraint will not be checked for first insert and delete statements,changes will be checked at commit and quota_totals again is set to deferrable initially immediate. </li></ul><ul><li>Second insert statement would be checked immediately </li></ul>
  37. 37. Business Rules <ul><li>Manager to be notified whenever a customer is assigned a credit limit of more than Rs.1 lakh </li></ul><ul><li>Manager to be notified whenever a Sales Representative finishes quota assigned </li></ul><ul><li>When a new order is taken Qty_in_hand is to be decreased by the quantity ordered </li></ul>DBMS responsible for storing,organizing data,ensuring data integrity.Enforcing business rules is the responsibility of application programs accessing the database.
  38. 38. Business Rules(contd.) <ul><li>Duplication of effort </li></ul><ul><li>Lack of consistency </li></ul><ul><li>Maintenance problems </li></ul><ul><li>Complexity </li></ul>Disadvantages of application programs enforcing business rules Triggers help DBMS in enforcing business rules
  39. 39. Business Rules(contd.) TRIGGERS <ul><li>For any event that causes a change in the database , </li></ul><ul><li>user can specify an action to be carried out by DBMS </li></ul><ul><li>using a trigger </li></ul>Events that can trigger an action are insert, update and delete Action triggered by the event is a sequence of SQL statements
  40. 40. Business Rules(contd.) TRIGGERS <ul><li>Create trigger top_stud_trig </li></ul><ul><li>after insert on student </li></ul><ul><li>referencing new as newstud </li></ul><ul><li>when (marks>95) </li></ul><ul><li>insert into highrankers values(newstud.rollno,marks) </li></ul><ul><li>for each row </li></ul>Inserting records in separate highrankers table when marks >95
  41. 41. Business Rules(contd.) TRIGGERS FORMAT Create [or replace] trigger <trigger_name> [enable|disable] <before|after> <insert|update|delete> [of <column_name_list>] On <table_name> [referencing new as <synonym>] [for each row] [when (trigger_condition)] <trigger_code>
  42. 42. Business Rules(contd.) TRIGGERS - Advantages <ul><li>Help in enforcing business rules </li></ul><ul><li>Useful for enforcing referential integrity </li></ul><ul><li>If new data is inconsistent an error can be raised to rollback the entire transaction </li></ul>
  43. 43. Business Rules(contd.) TRIGGERS - Disadvantages <ul><li>Database complexity increases </li></ul><ul><li>Hidden rules </li></ul><ul><li>Hidden performance implications </li></ul>
  44. 44. THANKS

×