SlideShare a Scribd company logo
Data Integrity
 Data integrity refers to the accuracy,
consistency, and reliability of data that is
stored in the database.
 Enforcing data integrity ensures the quality of
the data in the data
Why Data Integrity??
 if an employee is entered with an employee_id
value of 123, the database should not allow
another employee to have an ID with the same
value. If you have an employee_rating column
intended to have values ranging from 1 to 5, the
database should not accept a value of 6. If the
table has a dept_id column that stores the
department number for the employee, the
database should allow only values that are valid
for the department numbers in the company.
 Data integrity falls into these categories:
 Entity integrity
 Domain integrity
 Referential integrity
1.Entity integrity
 Entity integrity refers to the requirement that
all rows in a table must have a unique
identifier that can be used to tell apart each
record.
 This unique identifier is normally known as
Primary Key of the table.
 A Primary Key can be formed by a single
column or a combination of multiple
columns.
2.Domain Integrity
 Domain integrity is the validity of entries for a
given column.
 It refers to the requirement that data stored
in a column must adhere to the same format
and definition.
 This includes data type, data length, default
value of data, range of possible values,
whether duplicate values are allowed, or
whether null values are allowed.
3. Referential Integrity
 Referential integrity is based on relationships
between foreign keys and primary keys.
 Referential integrity ensures that key values
are consistent across tables. Such consistency
requires that there be no references to
nonexistent values and that if a key value
changes, all references to it change
consistently throughout the database.
After enforcing referential
integrity
 Users cannot Add records to a related table if
there is no associated record in the primary table.
 User cannot Change values in a primary table
that result in orphaned records in a related table.
 Users cannot Delete records from a primary table
if there are matching related records.
Importance of integrity
 Maintaining integrity is of itmost importance
for a database, so much so that we cannot
trust users and applications to enforce these
rules by themselves. Once integrity is lost,
you may find customers are double billed,
payments to the supplier are missing, and
everyone loses faith in your application.
Database Constraints
 The primary job of a constraint is to enforce a
rule in the database.
 The constraints in a database maintain the
integrity of the database.
 Entity integrity is enforced by
 Primary key constraint
 Unique constraint
 Domain integrity is enforced by
 Foreign key constraint
 Check constraint
 Default constraint
 Data type constraint
 Nullability constraint
 Referential integrity is enforced by
 Foreign key constraint
 User-defined integrity
 Check constraint
 Constraints can be defined in two ways
 The constraints can be specified immediately after
the column definition.This is called column-level
definition.
 The constraints can be specified after all the
columns are defined.This is called table-level
definition.
1.Primary key constraints:
 This constraint defines a column or
combination of columns which uniquely
identifies each row in the table.
CREATETABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
or
CREATETABLE employee
( id number(5) CONSTRAINT emp_id_pk PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
Syntax to define a Primary key at column level:
column name datatype [CONSTRAINT constraint_name] PRIMARY KEY
Syntax to define a Primary key at table level:
[CONSTRAINT constraint_name] PRIMARY KEY(column_name)
CREATE TABLE employee
( id number(5),
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
CONSTRAINT emp_id_pk PRIMARY KEY (id)
);
ALTERTABLE employee
ADD CONSTRAINT emp_id_pk PRIMARY KEY (id);
2.UNIQUE constraint
 This constraint ensures that a column or a group
of columns in each row have a distinct value. A
column(s) can have a null value but the values
cannot be duplicated.
Syntax to define a Unique key at column level:
[CONSTRAINT constraint_name] UNIQUE
CREATETABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10) UNIQUE
);
or
CREATETABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10) CONSTRAINT loc_un UNIQUE
);
Syntax to define a Unique key at table level:
[CONSTRAINT constraint_name] UNIQUE(column_name)
CREATETABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
CONSTRAINT loc_un UNIQUE(location)
);
ALTERTABLE employee
ADD CONSTRAINT loc_un UNIQUE (location);
3. Foreign key constraint:
 This constraint identifies any column referencing the
PRIMARY KEY in another table.
 It establishes a relationship between two columns in the
same table or between different tables.
 For a column to be defined as a Foreign Key, it should be
a defined as a Primary Key in the table which it is
referring. One or more columns can be defined as
Foreign key.
Syntax to define a Foreign key at column level:
[CONSTRAINT constraint_name] REFERENCES
Referenced_Table_name(column_name)
Lets use the "product" table and "order_items".
CREATETABLE product
( product_id number(5) CONSTRAINT pd_id_pk PRIMARY KEY,
product_name char(20),
supplier_name char(20),
unit_price number(10)
);
CREATETABLE order_items
( order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY,
pid number(5) CONSTRAINT pd_id_fk
REFERENCES,product(product_id),
product_name char(20),
supplier_name char(20),
unit_price number(10)
);
Syntax to define a Foreign key at table level:
[CONSTRAINT constraint_name] FOREIGN KEY(column_name)
REFERENCES referenced_table_name(column_name);
CREATETABLE order_items
( order_id number(5) ,
p_id number(5),
product_name char(20),
supplier_name char(20),
unit_price number(10)
CONSTRAINT od_id_pk PRIMARY KEY(order_id),
CONSTRAINT pd_id_fk FOREIGN KEY(p_id)
REFERENCES product(product_id)
);
If the employee table has a 'mgr_id' i.e, manager id as a
foreign key which references primary key 'id' within the
same table, the query would be like,
CREATETABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
mgr_id number(5) REFERENCES employee(id),
salary number(10),
location char(10)
);
4.Null constraint:
 This defines that if a column is NOT NULL or
allow NULL values to be stored in it.
 The decision to allow NULL values in a
column or not is a type of rule enforcement
for domain integrity.
CREATETABLE Employees_2
(
EmployeeID int PRIMARY KEY,
FirstName varchar(50) NULL,
LastName varchar(50) NOT NULL,
)
CREATETABLE employee
(
id number(5),
name char(20) CONSTRAINT nm_nn NOT NULL,
dept char(10),
age number(2),
salary number(10),
location char(10)
);
5.Default constraint:
 This defines what value the column should use when no
value has been supplied explicitly when inserting a record
in the table.
 A default can assign a constant value, the value of a
system function, or NULL to a column.
 You can use a default on any column except IDENTITY
columns.
CREATETABLE Orders_2
(
OrderID int ,
EmployeeID int ,
OrderDate datetime NULL DEFAULT(GETDATE()),
Freight money NULL DEFAULT (0),
ShipAddress nvarchar (60) NULL DEFAULT('NO SHIPPING ')
);
INSERT INTO Orders_2 (OrderId ,EmployeeID, Freight)VALUES(1,1, NULL)
OrderID: 1
EmployeeID : 1
OrderDate : 2003-01-02
Freight : NULL
ShipAddress : NO SHIPPING ADDRESS
alter table table_name
add constraint name _of _constraint
default value for column_name;
6. Check constraint
 Check constraints contain an expression the
database will evaluate when you modify or
insert a row. If the expression evaluates to
false, the database will not save the row.
 You can use many of the same operators (>, <,
<=, >=, <>, =) in additional to BETWEEN, IN,
LIKE, and NULL.
Syntax to define a Check constraint:
[CONSTRAINT constraint_name]CHECK (condition)
CREATETABLE Products_2
(
ProductID int PRIMARY KEY,
UnitPrice money CHECK(UnitPrice > 0 AND UnitPrice < 100)
)
OR
CREATETABLE Products_2
(
ProductID int PRIMARY KEY,
UnitPrice money,
CONSTRAINT CK_UnitPrice2 CHECK(UnitPrice > 0 AND UnitPrice < 100)
)
You can also add check constraints to a table after a table exists using the
ALTERTABLE syntax.
CREATETABLE Employees_2
(
EmployeeID int,
HireDate datetime
)
ALTERTABLE Employees_2
ADD CONSTRAINT CK_HireDate CHECK(hiredate < GETDATE())
Dropping constraints:
ALTERTABLE Table_name
DROP CONSTRAINT name_of_constraint
Ad

More Related Content

What's hot (20)

Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
Megha yadav
 
Sql DML
Sql DMLSql DML
Sql DML
Vikas Gupta
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
Anurag
 
Referential integrity
Referential integrityReferential integrity
Referential integrity
Jubin Raju
 
Data Manipulation Language
Data Manipulation LanguageData Manipulation Language
Data Manipulation Language
Jas Singh Bhasin
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
Dr. C.V. Suresh Babu
 
Integrity constraints in dbms
Integrity constraints in dbmsIntegrity constraints in dbms
Integrity constraints in dbms
Vignesh Saravanan
 
Sql commands
Sql commandsSql commands
Sql commands
Balakumaran Arunachalam
 
SQL Data Manipulation
SQL Data ManipulationSQL Data Manipulation
SQL Data Manipulation
khalid alkhafagi
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
Al-Mamun Sarkar
 
SQL
SQLSQL
SQL
zekeLabs Technologies
 
DML Commands
DML CommandsDML Commands
DML Commands
Randy Riness @ South Puget Sound Community College
 
SQL
SQLSQL
SQL
Vineeta Garg
 
Commands of DML in SQL
Commands of DML in SQLCommands of DML in SQL
Commands of DML in SQL
Ashish Gaurkhede
 
Intro to tsql unit 9
Intro to tsql   unit 9Intro to tsql   unit 9
Intro to tsql unit 9
Syed Asrarali
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
Merging data (1)
Merging data (1)Merging data (1)
Merging data (1)
Ris Fernandez
 
DBMS Integrity rule
DBMS Integrity ruleDBMS Integrity rule
DBMS Integrity rule
GirdharRatne
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
Abdelhay Shafi
 

Similar to Sql server ___________session_15(data integrity) (20)

Integrity and security
Integrity and securityIntegrity and security
Integrity and security
Surendra Karki Chettri
 
Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraint
suman kumar
 
Intro to tsql unit 7
Intro to tsql   unit 7Intro to tsql   unit 7
Intro to tsql unit 7
Syed Asrarali
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
TheVerse1
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
Prithwis Mukerjee
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
Prithwis Mukerjee
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
Mukesh Tekwani
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
Dimara Hakim
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
VARSHAKUMARI49
 
Assignment#07
Assignment#07Assignment#07
Assignment#07
Sunita Milind Dol
 
Relational data model
Relational data modelRelational data model
Relational data model
Vaishali Chinchkhede
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 
Unit1
Unit1Unit1
Unit1
Jay Patel
 
MQSL JOINING OF TABLES.pptx
MQSL JOINING OF TABLES.pptxMQSL JOINING OF TABLES.pptx
MQSL JOINING OF TABLES.pptx
lemonchoos
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
SakkaravarthiS1
 
Ankit
AnkitAnkit
Ankit
Ankit Dubey
 
Sql
SqlSql
Sql
jyothislides
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).ppt
arjun431527
 
Interview Questions.pdf
Interview Questions.pdfInterview Questions.pdf
Interview Questions.pdf
TarunKumar893717
 
Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraint
suman kumar
 
Intro to tsql unit 7
Intro to tsql   unit 7Intro to tsql   unit 7
Intro to tsql unit 7
Syed Asrarali
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
TheVerse1
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
Prithwis Mukerjee
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
Prithwis Mukerjee
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
Mukesh Tekwani
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
Dimara Hakim
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 
MQSL JOINING OF TABLES.pptx
MQSL JOINING OF TABLES.pptxMQSL JOINING OF TABLES.pptx
MQSL JOINING OF TABLES.pptx
lemonchoos
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
SakkaravarthiS1
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).ppt
arjun431527
 
Ad

More from Ehtisham Ali (17)

Android tutorial
Android tutorialAndroid tutorial
Android tutorial
Ehtisham Ali
 
Sql server ___________session_20(ddl triggers)
Sql server  ___________session_20(ddl triggers)Sql server  ___________session_20(ddl triggers)
Sql server ___________session_20(ddl triggers)
Ehtisham Ali
 
Sql server ___________session3-normailzation
Sql server  ___________session3-normailzationSql server  ___________session3-normailzation
Sql server ___________session3-normailzation
Ehtisham Ali
 
Sql server ___________session2-data_modeling
Sql server  ___________session2-data_modelingSql server  ___________session2-data_modeling
Sql server ___________session2-data_modeling
Ehtisham Ali
 
Sql server ___________session_19(triggers)
Sql server  ___________session_19(triggers)Sql server  ___________session_19(triggers)
Sql server ___________session_19(triggers)
Ehtisham Ali
 
Sql server ___________session_18(stored procedures)
Sql server  ___________session_18(stored procedures)Sql server  ___________session_18(stored procedures)
Sql server ___________session_18(stored procedures)
Ehtisham Ali
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)
Ehtisham Ali
 
Sql server ___________session_16(views)
Sql server  ___________session_16(views)Sql server  ___________session_16(views)
Sql server ___________session_16(views)
Ehtisham Ali
 
Sql server ___________session_11-12(joins)
Sql server  ___________session_11-12(joins)Sql server  ___________session_11-12(joins)
Sql server ___________session_11-12(joins)
Ehtisham Ali
 
Sql server ___________session_10(group by clause)
Sql server  ___________session_10(group by clause)Sql server  ___________session_10(group by clause)
Sql server ___________session_10(group by clause)
Ehtisham Ali
 
Sql server ___________session_1-intro
Sql server  ___________session_1-introSql server  ___________session_1-intro
Sql server ___________session_1-intro
Ehtisham Ali
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)
Ehtisham Ali
 
Sql server ___________session 2(sql 2008)
Sql server  ___________session 2(sql 2008)Sql server  ___________session 2(sql 2008)
Sql server ___________session 2(sql 2008)
Ehtisham Ali
 
Sql server ___________session 1(sql 2008)
Sql server  ___________session 1(sql 2008)Sql server  ___________session 1(sql 2008)
Sql server ___________session 1(sql 2008)
Ehtisham Ali
 
Sql server ___________data type of sql server
Sql server  ___________data type of sql serverSql server  ___________data type of sql server
Sql server ___________data type of sql server
Ehtisham Ali
 
Sql server ___________data control language
Sql server  ___________data control languageSql server  ___________data control language
Sql server ___________data control language
Ehtisham Ali
 
Sql server ___________ (advance sql)
Sql server  ___________  (advance sql)Sql server  ___________  (advance sql)
Sql server ___________ (advance sql)
Ehtisham Ali
 
Sql server ___________session_20(ddl triggers)
Sql server  ___________session_20(ddl triggers)Sql server  ___________session_20(ddl triggers)
Sql server ___________session_20(ddl triggers)
Ehtisham Ali
 
Sql server ___________session3-normailzation
Sql server  ___________session3-normailzationSql server  ___________session3-normailzation
Sql server ___________session3-normailzation
Ehtisham Ali
 
Sql server ___________session2-data_modeling
Sql server  ___________session2-data_modelingSql server  ___________session2-data_modeling
Sql server ___________session2-data_modeling
Ehtisham Ali
 
Sql server ___________session_19(triggers)
Sql server  ___________session_19(triggers)Sql server  ___________session_19(triggers)
Sql server ___________session_19(triggers)
Ehtisham Ali
 
Sql server ___________session_18(stored procedures)
Sql server  ___________session_18(stored procedures)Sql server  ___________session_18(stored procedures)
Sql server ___________session_18(stored procedures)
Ehtisham Ali
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)
Ehtisham Ali
 
Sql server ___________session_16(views)
Sql server  ___________session_16(views)Sql server  ___________session_16(views)
Sql server ___________session_16(views)
Ehtisham Ali
 
Sql server ___________session_11-12(joins)
Sql server  ___________session_11-12(joins)Sql server  ___________session_11-12(joins)
Sql server ___________session_11-12(joins)
Ehtisham Ali
 
Sql server ___________session_10(group by clause)
Sql server  ___________session_10(group by clause)Sql server  ___________session_10(group by clause)
Sql server ___________session_10(group by clause)
Ehtisham Ali
 
Sql server ___________session_1-intro
Sql server  ___________session_1-introSql server  ___________session_1-intro
Sql server ___________session_1-intro
Ehtisham Ali
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)
Ehtisham Ali
 
Sql server ___________session 2(sql 2008)
Sql server  ___________session 2(sql 2008)Sql server  ___________session 2(sql 2008)
Sql server ___________session 2(sql 2008)
Ehtisham Ali
 
Sql server ___________session 1(sql 2008)
Sql server  ___________session 1(sql 2008)Sql server  ___________session 1(sql 2008)
Sql server ___________session 1(sql 2008)
Ehtisham Ali
 
Sql server ___________data type of sql server
Sql server  ___________data type of sql serverSql server  ___________data type of sql server
Sql server ___________data type of sql server
Ehtisham Ali
 
Sql server ___________data control language
Sql server  ___________data control languageSql server  ___________data control language
Sql server ___________data control language
Ehtisham Ali
 
Sql server ___________ (advance sql)
Sql server  ___________  (advance sql)Sql server  ___________  (advance sql)
Sql server ___________ (advance sql)
Ehtisham Ali
 
Ad

Recently uploaded (20)

UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 

Sql server ___________session_15(data integrity)

  • 2.  Data integrity refers to the accuracy, consistency, and reliability of data that is stored in the database.  Enforcing data integrity ensures the quality of the data in the data
  • 3. Why Data Integrity??  if an employee is entered with an employee_id value of 123, the database should not allow another employee to have an ID with the same value. If you have an employee_rating column intended to have values ranging from 1 to 5, the database should not accept a value of 6. If the table has a dept_id column that stores the department number for the employee, the database should allow only values that are valid for the department numbers in the company.
  • 4.  Data integrity falls into these categories:  Entity integrity  Domain integrity  Referential integrity
  • 5. 1.Entity integrity  Entity integrity refers to the requirement that all rows in a table must have a unique identifier that can be used to tell apart each record.  This unique identifier is normally known as Primary Key of the table.  A Primary Key can be formed by a single column or a combination of multiple columns.
  • 6. 2.Domain Integrity  Domain integrity is the validity of entries for a given column.  It refers to the requirement that data stored in a column must adhere to the same format and definition.  This includes data type, data length, default value of data, range of possible values, whether duplicate values are allowed, or whether null values are allowed.
  • 7. 3. Referential Integrity  Referential integrity is based on relationships between foreign keys and primary keys.  Referential integrity ensures that key values are consistent across tables. Such consistency requires that there be no references to nonexistent values and that if a key value changes, all references to it change consistently throughout the database.
  • 8. After enforcing referential integrity  Users cannot Add records to a related table if there is no associated record in the primary table.  User cannot Change values in a primary table that result in orphaned records in a related table.  Users cannot Delete records from a primary table if there are matching related records.
  • 9. Importance of integrity  Maintaining integrity is of itmost importance for a database, so much so that we cannot trust users and applications to enforce these rules by themselves. Once integrity is lost, you may find customers are double billed, payments to the supplier are missing, and everyone loses faith in your application.
  • 11.  The primary job of a constraint is to enforce a rule in the database.  The constraints in a database maintain the integrity of the database.
  • 12.  Entity integrity is enforced by  Primary key constraint  Unique constraint  Domain integrity is enforced by  Foreign key constraint  Check constraint  Default constraint  Data type constraint  Nullability constraint  Referential integrity is enforced by  Foreign key constraint  User-defined integrity  Check constraint
  • 13.  Constraints can be defined in two ways  The constraints can be specified immediately after the column definition.This is called column-level definition.  The constraints can be specified after all the columns are defined.This is called table-level definition.
  • 14. 1.Primary key constraints:  This constraint defines a column or combination of columns which uniquely identifies each row in the table.
  • 15. CREATETABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) ); or CREATETABLE employee ( id number(5) CONSTRAINT emp_id_pk PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) ); Syntax to define a Primary key at column level: column name datatype [CONSTRAINT constraint_name] PRIMARY KEY
  • 16. Syntax to define a Primary key at table level: [CONSTRAINT constraint_name] PRIMARY KEY(column_name) CREATE TABLE employee ( id number(5), name char(20), dept char(10), age number(2), salary number(10), location char(10), CONSTRAINT emp_id_pk PRIMARY KEY (id) ); ALTERTABLE employee ADD CONSTRAINT emp_id_pk PRIMARY KEY (id);
  • 17. 2.UNIQUE constraint  This constraint ensures that a column or a group of columns in each row have a distinct value. A column(s) can have a null value but the values cannot be duplicated.
  • 18. Syntax to define a Unique key at column level: [CONSTRAINT constraint_name] UNIQUE CREATETABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) UNIQUE ); or CREATETABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) CONSTRAINT loc_un UNIQUE );
  • 19. Syntax to define a Unique key at table level: [CONSTRAINT constraint_name] UNIQUE(column_name) CREATETABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10), CONSTRAINT loc_un UNIQUE(location) ); ALTERTABLE employee ADD CONSTRAINT loc_un UNIQUE (location);
  • 20. 3. Foreign key constraint:  This constraint identifies any column referencing the PRIMARY KEY in another table.  It establishes a relationship between two columns in the same table or between different tables.  For a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring. One or more columns can be defined as Foreign key.
  • 21. Syntax to define a Foreign key at column level: [CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(column_name) Lets use the "product" table and "order_items". CREATETABLE product ( product_id number(5) CONSTRAINT pd_id_pk PRIMARY KEY, product_name char(20), supplier_name char(20), unit_price number(10) ); CREATETABLE order_items ( order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY, pid number(5) CONSTRAINT pd_id_fk REFERENCES,product(product_id), product_name char(20), supplier_name char(20), unit_price number(10) );
  • 22. Syntax to define a Foreign key at table level: [CONSTRAINT constraint_name] FOREIGN KEY(column_name) REFERENCES referenced_table_name(column_name); CREATETABLE order_items ( order_id number(5) , p_id number(5), product_name char(20), supplier_name char(20), unit_price number(10) CONSTRAINT od_id_pk PRIMARY KEY(order_id), CONSTRAINT pd_id_fk FOREIGN KEY(p_id) REFERENCES product(product_id) );
  • 23. If the employee table has a 'mgr_id' i.e, manager id as a foreign key which references primary key 'id' within the same table, the query would be like, CREATETABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), mgr_id number(5) REFERENCES employee(id), salary number(10), location char(10) );
  • 24. 4.Null constraint:  This defines that if a column is NOT NULL or allow NULL values to be stored in it.  The decision to allow NULL values in a column or not is a type of rule enforcement for domain integrity. CREATETABLE Employees_2 ( EmployeeID int PRIMARY KEY, FirstName varchar(50) NULL, LastName varchar(50) NOT NULL, )
  • 25. CREATETABLE employee ( id number(5), name char(20) CONSTRAINT nm_nn NOT NULL, dept char(10), age number(2), salary number(10), location char(10) );
  • 26. 5.Default constraint:  This defines what value the column should use when no value has been supplied explicitly when inserting a record in the table.  A default can assign a constant value, the value of a system function, or NULL to a column.  You can use a default on any column except IDENTITY columns.
  • 27. CREATETABLE Orders_2 ( OrderID int , EmployeeID int , OrderDate datetime NULL DEFAULT(GETDATE()), Freight money NULL DEFAULT (0), ShipAddress nvarchar (60) NULL DEFAULT('NO SHIPPING ') ); INSERT INTO Orders_2 (OrderId ,EmployeeID, Freight)VALUES(1,1, NULL) OrderID: 1 EmployeeID : 1 OrderDate : 2003-01-02 Freight : NULL ShipAddress : NO SHIPPING ADDRESS
  • 28. alter table table_name add constraint name _of _constraint default value for column_name;
  • 29. 6. Check constraint  Check constraints contain an expression the database will evaluate when you modify or insert a row. If the expression evaluates to false, the database will not save the row.  You can use many of the same operators (>, <, <=, >=, <>, =) in additional to BETWEEN, IN, LIKE, and NULL.
  • 30. Syntax to define a Check constraint: [CONSTRAINT constraint_name]CHECK (condition) CREATETABLE Products_2 ( ProductID int PRIMARY KEY, UnitPrice money CHECK(UnitPrice > 0 AND UnitPrice < 100) ) OR CREATETABLE Products_2 ( ProductID int PRIMARY KEY, UnitPrice money, CONSTRAINT CK_UnitPrice2 CHECK(UnitPrice > 0 AND UnitPrice < 100) )
  • 31. You can also add check constraints to a table after a table exists using the ALTERTABLE syntax. CREATETABLE Employees_2 ( EmployeeID int, HireDate datetime ) ALTERTABLE Employees_2 ADD CONSTRAINT CK_HireDate CHECK(hiredate < GETDATE())
  • 32. Dropping constraints: ALTERTABLE Table_name DROP CONSTRAINT name_of_constraint