Data Integrity
Data Integrity
[email protected]
D7402IX6ER
Integrity of data ensures an appropriate lineage of business entities in the data flow
process.
Data Integrity
[email protected]
D7402IX6ER
- It ensures the consistency of the data through entire project life cycle. It means the
data should be relevant across the business.
● Receive accurate data as if the records are not ignored without any flaws.
Hence the loss of data costs to organization.
[email protected]
D7402IX6ER ● Consistency of data flow across business processes.
So, None of the entities(tables) are not struck in the middle of the business
process due invalid or irrelevant data.
● Data generated should be reliable within business units. So that the data is
consistent when one business unit shares the data with other business unit.
● Inappropriate decision making when multiple strategic reports are generated without
any quality of data
[email protected]
D7402IX6ER
3. Referential integrity
● Each row in the table is referenced by unique values, and avoids conflicts with other
rows.
● Unique identifiers are normally known as primary keys in tables.
[email protected]
D7402IX6ER
Example : CUSTOMER table uses single column Cust_Id as the unique identifier
Select * from CUSTOMER where Cust_Id=123001
[email protected]
D7402IX6ER
DESC COURSE
In this table,
Course_id column will not accept Alphabets because it is defined as an Integer.
[email protected]
D7402IX6ER
Referential Integrity -
[email protected]
E.g:
D7402IX6ER
Step1:
INSERT INTO COURSE Values (20,’SQL');
Step2:
INSERT INTO STUDENT VALUES
(2,’John’,20) ; This file is meant for personal use by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Records
COURSE
[email protected]
D7402IX6ER
STUDENT
● There are rules that limit what type of data can be put into a column/table using SQL
constraints.
● A set of constraints ensures that the data in the column or table is accurate and
[email protected]
D7402IX6ER
reliable.
● Following are the most used constraints on Single relationship that are applied to
Columns in a single table
●
NOT NULL, DEFAULT
[email protected]
D7402IX6ER
Other constraints like below will be discussed later.
● UNIQUE
● PRIMARY KEY
● CHECK
● Using the NOT NULL constraint, you prevent a column from having a NULL value.
● After applying NOT NULL constraint to column, you cannot pass a null value into that
column
[email protected]
D7402IX6ER
Course_name is NULL
It means Course_name value must
not be blank.
This file is meant for personal use by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
NOT NULL:
[email protected]
D7402IX6ER
● CHECK constraint conditionally validates the column values before insertion of the record, and
prevents from any unwanted entry of record.
● The table ensures that salary amount is not less than 2001 for any new entry.
Try:
insert into employees values(108,’Ashly’,’Mishra', ‘AMISHRA',
‘773.453.1489', ‘1999-12-19','MK_REP’, 1300,NULL, 66,19);
Eg:
Try :
[email protected]
D7402IX6ER
● Primary key constraint is defined on a column and it uniquely identifies each record in
a table.
● A Primary Key by default creates an Index on a column. An index key is used for search
records based on conditions.
[email protected]
D7402IX6ER
[email protected]
D7402IX6ER
Desc COURSE
Try :
insert into course values (30, 'Hive');
● Enforcing referential integrity will cause limitations to its referenced tables on DML
operations such as Insert, Delete and Update
● Referential integrities are overhead when exporting the tables from one database to
other database
● Problems like foreign key should be disabled and enabled after exporting
[email protected]
D7402IX6ER
● re-enabling foreign keys lost data integrity as it will not scan appropriately
● A foreign key value when assigned with NULL doesn’t references any primary key
[email protected]
D7402IX6ER
value in another table
● But if foreign key has any valid value, then it must have an associated value in primary
key
● During any DML operation on Parent tables , there are different rules that effect on
associated values in child tables.
This file is meant for personal use by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Rules on Updating Foreign Key
● When updating foreign key column with a value that is not present in primary key
column, it violates the referential integrity
Desc COURSE
[email protected]
D7402IX6ER
Foreign Key ( MUL ) in
STUDENT table is referring
Primary Key in COURSE .
Desc STUDENT
[email protected]
D7402IX6ER
[email protected]
D7402IX6ER
● Delete of a record in a parent table that is referenced by child’s foreign key columns
[email protected]
●
D7402IX6ER Trying to delete parent table record that is being referenced by foreign key
Try:
delete from course where course_id = 20;
● To overwrite the DML rules on referential integrity fields, CASCADE is issued on the
table having foreign key column
● So that changes to parent table records are automatically reflected in child table
Before CASCADE
[email protected]
D7402IX6ER
create table student
(sid integer primary key ,
sname varchar(20),
course_id integer,
foreign key (course_id) references course(course_id)
on delete cascade
on update cascade);
Before Update
[email protected]
D7402IX6ER
/* Update */
Update course set course_id = 100 where course_id = 10;
Before CASCADE
[email protected]
D7402IX6ER
/* Delete */
delete from course where course_id = 20;
This deck explains the data integrity and how it works, its constraints on relations queries and the referential
integrity. Here we generally, explained that how data integrity maintains the accuracy and consistency
amongst the table in SQL.
Each condition is explained using a query and its output.
[email protected]
D7402IX6ER