Integirty in Databases
Tharindu Weerasinghe
www.tharinduweerasinghe.com
Integrity
* Data Integrity implies the validity and consistency of
stored data.
* Loss of data integrity results in invalid or corrupted
data.
* Integrity is usually expressed in terms of Constraints.
Integrity
Examples of Duplicate data (Redundant data):
HR and Payroll domain:
➔
Employee transferred to another office location.
➔
Salary increment.
How do we achieve integrity?
Null Rule
➔
Defined on a single column
➔
Allows or Disallows to insert or update a rows with a
null.
Unique Column Values
➔
Defined on a column or set of columns.
➔
Allows the insert or update of a row only if it
contains a unique value in that column or set of
columns.
How do we achieve integrity?
Primary Key
➔
Defined on a column or set of columns (Key)
➔
Specifies that each row in the table can be uniquely
identified by the values in the key.
Referential Integrity Rules
➔
Defined on a column or set of columns in one tabl.e
➔
Guarantees that the value in that key matches with
the value in a key in a related table.
How do we achieve integrity?
Complex Integrity Checking
➔
User-defined rule for a column or a set of columns.
➔
Allow or disallow inserts, updates, or deletes of a
row based on the value it contains for the column or
set of columns.
How do we achieve integrity?
Complex Integrity Checking
➔
User-defined rule for a column or a set of columns.
➔
Allow or disallow inserts, updates, or deletes of a
row based on the value it contains for the column or
set of columns.
Referential Integrity – Associated Rules
Restrict
Disallows the update or deletion of referenced data
Set to Null
When referenced data is updated or deleted, all
associated dependent data is set to NULL
Set to Default
When referenced data is updated or deleted, all
associated dependent data is set to a default value
Referential Integrity – Associated Rules
Cascade
➔
When referenced data is updated, all associated
dependent data is correspondingly updated.
➔
When a referenced row is deleted, all associated
dependent rows are deleted.
No Action
➔
Disallows the update or deletion of referenced data
➔
This differs from RESTRICT.
●
It is checked at the end of the statement
●
Or at the end of the transaction
➔
Oracle uses No Action as its default action
Integrity Constraints Description
➔
NOT NULL constraints
Rules associated with nulls in a column
➔
UNIQUE key constraints
Rule associated with unique column values
➔
PRIMARY KEY constraints
Rule associated with primary identification
➔
FOREIGN KEY constraints
Rules associated with referential integrity
Oracle supports the use of FOREIGN KEY integrity constraints
to define the referential integrity actions.
Update and delete No Action
Delete CASCADE
Delete SET NULL
➔
CHECK constraints for complex integrity rules
Advantages of Integrity Constraints
➔
Easy to declare or to implement
➔
Rules are stored in a centralized location
➔
Easy to manage when business rules were
changed
➔
Immediate user feedback
➔
Superior Performance
➔
Flexibility for Data Loads and Identification of
➔
Integrity Violations
Referential Integrity Constraints
Single Table Referential Constraints
The CHECK Condition
➔
It must be a Boolean expression evaluated using the values in the
row being inserted or updated.
➔
It cannot contain subqueries; sequences; the SQL functions
SYSDATE, UID, USER, or USERENV; or the pseudocolumns LEVEL or
ROWNUM.
The CHECK Condition – Examples (w3schools)
The DEFAULT Condition – Examples (w3schools)
A must do
You must go to this Oracle Documentation page and read it.
https://docs.oracle.com/cd/B19306_01/server.102/b14220/data_int.htm
You must visit the w3schools regularly.
https://www.w3schools.com/sql/
Gratitude
References:
Previous Year ICTST40704 (UVT – Sri Lanka) lecture notes of Mr. Sanjeeva Perera
https://docs.oracle.com/cd/B19306_01/server.102/b14220/data_int.htm
W3Schools (https://www.w3schools.com/sql/sql_default.asp)

Database Intergrity

  • 1.
    Integirty in Databases TharinduWeerasinghe www.tharinduweerasinghe.com
  • 2.
    Integrity * Data Integrityimplies the validity and consistency of stored data. * Loss of data integrity results in invalid or corrupted data. * Integrity is usually expressed in terms of Constraints.
  • 3.
    Integrity Examples of Duplicatedata (Redundant data): HR and Payroll domain: ➔ Employee transferred to another office location. ➔ Salary increment.
  • 4.
    How do weachieve integrity? Null Rule ➔ Defined on a single column ➔ Allows or Disallows to insert or update a rows with a null. Unique Column Values ➔ Defined on a column or set of columns. ➔ Allows the insert or update of a row only if it contains a unique value in that column or set of columns.
  • 5.
    How do weachieve integrity? Primary Key ➔ Defined on a column or set of columns (Key) ➔ Specifies that each row in the table can be uniquely identified by the values in the key. Referential Integrity Rules ➔ Defined on a column or set of columns in one tabl.e ➔ Guarantees that the value in that key matches with the value in a key in a related table.
  • 6.
    How do weachieve integrity? Complex Integrity Checking ➔ User-defined rule for a column or a set of columns. ➔ Allow or disallow inserts, updates, or deletes of a row based on the value it contains for the column or set of columns.
  • 7.
    How do weachieve integrity? Complex Integrity Checking ➔ User-defined rule for a column or a set of columns. ➔ Allow or disallow inserts, updates, or deletes of a row based on the value it contains for the column or set of columns.
  • 8.
    Referential Integrity –Associated Rules Restrict Disallows the update or deletion of referenced data Set to Null When referenced data is updated or deleted, all associated dependent data is set to NULL Set to Default When referenced data is updated or deleted, all associated dependent data is set to a default value
  • 9.
    Referential Integrity –Associated Rules Cascade ➔ When referenced data is updated, all associated dependent data is correspondingly updated. ➔ When a referenced row is deleted, all associated dependent rows are deleted. No Action ➔ Disallows the update or deletion of referenced data ➔ This differs from RESTRICT. ● It is checked at the end of the statement ● Or at the end of the transaction ➔ Oracle uses No Action as its default action
  • 10.
    Integrity Constraints Description ➔ NOTNULL constraints Rules associated with nulls in a column ➔ UNIQUE key constraints Rule associated with unique column values ➔ PRIMARY KEY constraints Rule associated with primary identification ➔ FOREIGN KEY constraints Rules associated with referential integrity Oracle supports the use of FOREIGN KEY integrity constraints to define the referential integrity actions. Update and delete No Action Delete CASCADE Delete SET NULL ➔ CHECK constraints for complex integrity rules
  • 11.
    Advantages of IntegrityConstraints ➔ Easy to declare or to implement ➔ Rules are stored in a centralized location ➔ Easy to manage when business rules were changed ➔ Immediate user feedback ➔ Superior Performance ➔ Flexibility for Data Loads and Identification of ➔ Integrity Violations
  • 12.
  • 13.
  • 14.
    The CHECK Condition ➔ Itmust be a Boolean expression evaluated using the values in the row being inserted or updated. ➔ It cannot contain subqueries; sequences; the SQL functions SYSDATE, UID, USER, or USERENV; or the pseudocolumns LEVEL or ROWNUM.
  • 15.
    The CHECK Condition– Examples (w3schools)
  • 16.
    The DEFAULT Condition– Examples (w3schools)
  • 17.
    A must do Youmust go to this Oracle Documentation page and read it. https://docs.oracle.com/cd/B19306_01/server.102/b14220/data_int.htm You must visit the w3schools regularly. https://www.w3schools.com/sql/
  • 18.
    Gratitude References: Previous Year ICTST40704(UVT – Sri Lanka) lecture notes of Mr. Sanjeeva Perera https://docs.oracle.com/cd/B19306_01/server.102/b14220/data_int.htm W3Schools (https://www.w3schools.com/sql/sql_default.asp)