SlideShare a Scribd company logo
1 of 13
Download to read offline
r
Lecture 1 �-
·-·
Implementing
Data Integrity
""''"�•••J2c-,..,..,. ,,_.,.,..,,.-,..,�,.___,..,,,_ ......,..,,,,,.�..:,,,.,,,__,.3,,,,,.,,..,.,.;.iiii.:�--:,,i�....lf±'-"
·"".<•;..;:•,. ··.. ;-�,.,,..,.,.___,.__.;,;;;,;..,c-· "-"' >c-::-<!.,_._�..x•>-� .,......_..,,__,...,_,.L, ;, ,,.,; f
Dr. Mohammed Zayed-
-----,.
..-.-
.. .-.�
..
Implementing Data Integrity
- -- -
Ifchecks are not applied while defining and creating tables, the data stored in the tables
can become redundant. For example, if you do not store the data about all the employees
with complete address details, then the data would not be useful.
Similarly, ifa database used by the Human Resource department stores employee contact
details in two separate tables, the details of the employees might not match. This would
result in inconsistency and confusion.
Therefore, it is important to ensure that the data stored in tables is complete and
consistent. The concept ofmaintaining consistency and completeness ofdata is called
data integrity. Data integrity is enforced to ensure that the data in a database is accurate,
consistent, and reliable. It is broadly classified into_the following categories:
■ Entity integrity: Ensures that each row can be uniquely identified by an att1ibute
called the primary key. The primary key column contains unique value in all the
rows. In addition, this column cannot be NULL. Consider a situation where there
might be two candidates for an interview with the same name 'Jack'. By enforcing
4.12 Managing Databases and Tables ©NIIT
-·---�-·-··-·_._._-- -
entity integrity, the two candidates can be identified by using the unique code
assigned to them. For example, one candidate can have the code 001 and the other
candidate can be 002.
■ Domain integrity: Ensures that only a valid range of values is stored in a column. It
can be enforced by restricting the type of data, the range of values, and the format of
the data. For example, you have a table called BranchOffice with a column called
city that stores the name of the cities, where the branch offices are located. The
offices are located in 'Berkeley', 'Boston', 'Chicago', 'Dallas', 'Munchen', 'New
Jersey', 'New York', 'Paris', and 'Washington'. By enforcing domain integrity, you
can ensure that only valid values (as per the list specified) are entered in the City
column ofthe BranchOffice table.
■ Referential integrity: Ensures that the values of the foreign key match the value of
the corresponding primary key. For example, if a bicycle has been ordered and an
entry is to be made in the OrderDetaj} table, then that bicycle code should exist in the
product table. This ensures that an order is placed only for the bicycle that is
available.
■ User-defined integrity: Refers to a set of rules specified by a user, which do not
belong to the·entity, domain, and referential integrity categories.
When creating tables, the SQL Server allows you to maintain integrity by:
■ Applying constraints
■ Applying rules
■ Using user-defined types
Applying Constraints
Consider an example where a user entered a duplicate value in the EmployeeID column of
the Employee table. This would myan that two employees have same empl_oyee ID. This
would further result in erroneous results when anybody queries the table. As a database
developer, you can prevent this by enforcing data integrity on the table by using
constraints.
· ·
Co:q_strarnts define rules that must be followed to maintain CQil§isteQCY and correcmesJ, of
data-:-A constraint can either be created while creating a table or can be added lat�r. When
a constraint is added after the table is created, it checks the existing data. If there is any
viol�tio�, th�i;i. the cpn�traint is rejected.
A constraint can be created by using either ofthe following statements:
a- -eREATFTABI:;E statement
■ ALTER TABLE statement
©NUT Managing Databases and Tables 4.13
--�---.-.-.�---.-.--:- --- ..
A constraint can be defined on a column while creating a table. It can be created with the
CREATE TABLE statement. The syntax ofadding a constraint at the time of table
creation is:
CREATE TABLE table name
column name CONSTRAINT constraint name constraint_type f,CONSTRAINT
constraint name constraint_type]
where,
column name is the name ofthe column on which the constraint is to be defined.
constraint name is the name ofthe constraint to be created and must follow the rules for
the identifier.
constraint_type is the type of constraint to be added.
Constraints can be divided into the following types:
II Primary key constraint
• Unique constraint
• Foreign key constraint
• Check constraint
• Defa
ult constraint
Primary Key Constraint
A prima1y key constraint is defined on a column or a set of columns whose values
uniquely identify all the rows in a table. These columns are referred to as the primary key
columns. A primary key column cannot contain NULL values since it is used to uniquely
identify rows in a table. The primary key constraint ensures entity integrity.
You can define a primary key constraint while creating the table or you can add it later by
altering the table. However, if you define the p1imary key constraint after inserting rows,
the SQL Server will give an error if the rows contain duplicate values in the column.
While defining a p1imary key constraint, you need to specify a name for the constraint. If
a name is not specified, tbe SQL Server automatically assigns a name to the constraint.
4.14 Managing Databases and Tables ©NilT
lf a primary key constraint is defined on a column that already contains. data, then the
existing data in the column is screened. If any duplicate values are found, thenthe
primary key constraint is rejected. The syntax of applying the primary key constraint
when creating tabTe is:
CREATE TJI.BLE table name
col name [CONSTRAINT constraint name PRIMARY KEY
[CLUSTEREDINONCLUSTERED)
col name [, col name [, col name [, ...] ) )
where,
constraint_name specifies the name of the constraint to be created.
CLUSTERED I NONCLUSTERED are keywords that specify if a clustered or a nonclustered
index is to be created for the primary key constraint.
col_name specifies the name ofthe column(s) on which the primary key constraint is to
.be defmed.
Note
You will learn more about indexes in Chapter 6.
In the preceding example of the EmployeeLeave table, you can add a primaiy .key
constraint, while creating the table. You can set the EmployeeID and the LeaveStartDate
columns ofthe EmployeeLeave table as a composite primary key. You can use the
following·statement to apply the·primary key constraint:
CREATE TABLE HumanResources.EmployeeLeave
ErnployeeID int,
LeaveStartDate datetime CONSTRAINT cpkLeaveStartDate PRIMARY
KEY(EmployeeID, LeaveStartDate),
---.-.---
r - -···· --
The preceding statement creates the EmployeeLeave table with a composite primary key
constraint on EmployeeID and LeaveStartDate. The name ofthe constraint is
cpk:LeaveStartDate.
Managing Databases and Tab_les 4.15
--�
Unique Constraint
The unique constraint is used to enforce uniqueness on non-primary key columns. A
ptimary key constraint column automatically includes a restriction for uniqueness. The
unique constraint is similar to the primary key constraint except that it allows one NULL
row. Multiple unique constraints can be created on a table. The syntax ofapplying the
unique constraint when creating table is:
CREATE TABLE table name
col_name [CONSTRAINT constraint_name UNIQUE [CLUSTERED
NONCLUSTERED]
(col_name [, col_name [, col_name [, ... )]])
col name [, col name [, col name [, ...]]]
where,
constraint_name specifies the name of the constraint to be created.
CLUSTERED I NONCLUSTERED are keywords that specify if a clustered or a nonclustered
index is to be created for the unique constraint.
col_name specifies the name ofthe column(s) on which the unique constraint is to be
defined.
Foreign Key Constraint
You can use the foreign key constraint to remove the inconsistency in two tables when the
data in one table depends on the data in another table.
A foreign key constraint associates one or more columns (the foreign key) of a table with
an identical set of columns (a primary key column) in another table on which a primary
key constraint has been defined. The syntax of applying the foreign key constraint when
creating table is:
CREATE TABLE table name
(
col name [CONSTRAINT constraint name FOREIGN KEY (col_name [,
col name [, ...]])
REFERENCES table name (column_name [, column name [, ...]]) ]
(col_name [, col_name (, col name [, ...]]])
col name (, col name (, col name [, ...]]]
4.16 Managing Databases and Tables ©NTIT
-......�- -··- -
where,
constraint name is the name of the constraint on which the foreign key constraint is to
be defined.
col name is the name of the column on which the foreign key constraint is to be enforced.
table name is the name of the related table jn which the primary key constraint has been
specified.
column_name is the name of the primary key column ofthe related table on which the
primary key constraint has been defined.
In the example of the EmployeeLeave table under the HumanResources schema, you need
to add the foreign key constraint to enforce referential integrity. In the HumanResources
schema, the EmployeeID column is set as a primary key in the Employee table. Therefore,
you need to set EmployeeID in the EmployeeLeave table as a foreign key.
In the preceding example, you can use the following statement to apply the foreign key
constraint in the EmployeeLeave table:
CREATE TABLE HumanResources.EmployeeLeave
EmployeeID int CONSTRAINT fkEmployeeID FOREIGN KEY REFERENCES
HumanResources.Ernployee(EmployeeID),
LeaveStartDate datetime CONSTRAINT cpkLeaveStartDate PRIMARY
KEY(EmployeeID, LeaveStartDate),
The preceding statement creates the EmployeeLeave table with a foreign key constraint
on the EmployeeID column. The name of the constraint is fkEmployeeID.
Check Constraint
"'
A check constraint enforces domain integrity by restricting the values to be inserted in a
column. It is possible to define multiple check constraints on a single column. These are
evaluated-in the order in which they are defined. The syntax of applying the check
constraint is:
CREATE TABLE table name
col name [CONSTRAINT constraint_name) CHECK (expression)
©NIIT Managing Databases and Tables 4.17
(coL name [, col name [, ... J])
A single check constraint can be applied to multiple columns when it is defined at the
table level.
where,
constraint_name specifies the name of the constraint to be created.
expression specifies the conditions that define the check to be made on the column.
Expression can include elements, such as arithmetic operators, relational operators, or
keywords, such as IN, LIKE, and BETWEEN.
A check constraint can be specified by using the following keywords:
rs IN: To ensure that the values entered are from a list ofconstant expressions.
o LIKE: To ensure that the values entered in specific columns are of a certain pattern.
This can be achieved by using wildcards.
� BETWEEN: To specify a range of constant expressions by using the BETWEEN
keyword. The upper and lower boundary values are included in the range.
The rules regarding the creation of the CHECK constraint are as follows:
t1 It can be created at the column level.
WA It can contain user-specified search conditions.
r1!i It cannot contain subqueries.
ii It does not check the existing data in the table if created with the WITH NOCHECK
option.
m It can reference other columns of the same table.
In the example ofthe EmployeeLeave table, you need to ensure that the leave type can
take any one ofthe three values: CL, PL, or SL. For this, while creating the table, you can
add the check constraint using the IN keyw·ord for the LeaveType column
You can use the following statement to apply the check constraint:
CREATE TABLE HurnanResources.EmployeeLeave
EmployeeID int CONSTRAINT fkEmployeeID FOREIGN KEY REFERENCES
HumanResources.Employee(EmployeeID),
LeaveStartDate datetirne CONSTRAINT cpkLeaveStartDate PRIMARY
KEY(EmployeeID, LeaveStartDate),
LeaveEndDate datetime NOT NULL,
LeaveReason varchar(l00),
4.18 Managing Databases and Tables ©NTIT
LeaveType char(2) CONSTRAINT chkLeave CHECK(LeaveType
IN ( I CL I ' 'SL I ' I PL I ) )
The preceding command creates the EmployeeLeave table with a check constraint on the
LeaveType colu
mn. The name ofthe constraint is chkLeave.
Default Constraint
A default constraint can be used to assign a·constant value to a column, and the user need
not insert values for such a column. Only one default constraint can be created for a
column but the column cannot be an identity column. The system-supplied values, such as
USER, CURRENT_USER, and user-defined values can be assigned as defaults.
The syntax ofapplying the default consh·aint while creating a table is:
CREATE TABLE table name
col name [CONSTRAINT constraint_name] DEFAULT (constant expression
NULL)
(col_name [, col name[, ...]])
where,
constraint_name specifies the name ofthe constraint to be created.
constant expression specifies an expression that contains only constant values. This
can contain a NULL value.
In the example ofcreating the EmployeeLeave table, you can insert a default constraint to
add a default value for the LeaveType column. You can set �e default leave type as PL.
You can use the following statement to create the default constraint:
©NUT
CREATE TABLE HumanResources.EmployeeLeave
( ..
EmployeeID int CONSTRAINT fkEmployeeID FOREIGN KEY REFERENCES
HumanResources.Employee(EmployeeID),
LeaveStartDate datet�me CONSTRAINT cpkLeaveStartDate PRIMARY·­
KEY(EmployeeID, LeaveStartDate),
LeaveEndDate datetime NO T NULL,
LeaveReason varchar(l00),
LeaveType char(2) CONSTRAINT chkLeave CHECK(LeaveType
IN('CL', 'SL', 'PL')) CONSTRAINT chkDefLeave DEFAULT 'PL'
Managing Databases and Tables 4.19
The preceding command creates the Employee Leave table with a default constraint on the
LeaveType column, where the default value is spccificd as PL. The name ofthe constraint
is chkDefLeave.
Note
You can also use the DEFAULTdatabase objects to create a de.fault constraint that can
he applied to columns across tables within the same database. For this, you can create
database ohjects by using the CREATE DEPA ULTstatement.
J
-------c:======-l
Just a minute:
Which keyword is used to specify a check constraint?
Answer:
A check constraint can be specified by using the LIKE, IN, and BETWEEN keywords.
Applying Rules
A rule enforces domain integrity for columns or user-defined data types. The rule is
applied to the column or the user-defined data type before an INSERT or UPDATE
statement is issued. fn otber words, a rule specifies a restriction on the values ofa column
or a user-defined data type. Rules are used to implement business-related restrictions or
limitations. A rnle can be created by using the CREATE RULE statement. The syntax of
the CREATE RULE statement is:
CREATE RULE rule name AS conditional_expression
where,
rule_name specifies the name of the new rnle that must conform to rules for identifiers.
conditional_expression specifies the condition(s) that defines the rule. It can be any
expression that is valid in a WHERE clause and can include elements, such as arithmetic
operators, relational operators, TN, LIKE, and BETWEEN.
The variable specified in the conditional expression must be prefixed whh the@ symbol.
The expression refers to the value that is being specified with the INSERT or UPDATE
statement.
4.20 Managing Databases and Tables ©NIIT
., . .:-- ,.. --• •• -.;,r -- . .. ...
In the preceding example of the EmployeeLeave table, you applied a rule to accept only
three values: 'CL', 'SL', and 'PL'. You can perform the same task by creating a rule. You
can use the following statement to create the rule:
CREATE RULE rulType
AS @LeaveType IN ('CL', 'SL', 'PL')
After you create the rule, you need to activate the rule by using a stored procedure,
sp_bindrule.
The syntax ofsp_bindrule is:
sp_bindrule <'rule'>, <'object name'>, [<'futureonly_flag'>)
where,
rule specifies the name ofthe rule that you want to bind.
object_name specifies the object on which you want to bind the rule.
futureonly flag applies only when you want to bind the rule to a user-defined data
type.
Consider the following example, where the rulType rule created for the LeaveType
column of the EmployeeLeave table is bound by using the sp_bindrule stored procedure.
You can use the following statement to bind the rule.
sp_bindrule 'rulType', 'HumanResources.EmployeeLeave.LeaveType'
Similarly, when you want to remove the n1le, the sp_unbindrule stored procedure is
used. For example, to remove the rule from the EmployeeLeave table, you can use the
following statement to unbind the rule.
sp_unbindrule 'HumanResources.EmployeeLeave.LeaveType'
Using a User-Defined Data Type
User-defined data types are custom data types defined by the users with a custom name.
User-defined data types allow modifying the composite data type used in the database.
The user-defined data types are based on the system data types and can be used to
predefine several attributes of a column, such as its data typ_e, length, and whether it
supports Nbl'..::t'values.
©NI1T Managing Databases and Tables 4.21
You can create user-defined data types by using the CREATE TYPE statement. The
syntax ofthe CREATE TYPE statement is:
CREATE TYPE [ schema_name. ] type_name { FROM base_type [ ( precision
[ , scale J ) ] [ NULL I NOT NULL l l [ ; ]
where,
schema_name specifies the name of the schema to which the alias data type or the user­
defined data type belongs.
type_name specifies the name of the alias data type or the user-defined data type.
base_type specifies the SQL Server supplied data type on which the alias data lype is
based.
precision specifies the decimal or nwneric point. Decimal and numeric are nonnegative
integers that indicate the maximum total number ofdecimal digits that can be stored, both
to the left and to the right of the decimal point.
scale specifies the decimal or numeric scale.
NULL I NOT NULL specifies whether the data type can hold a null value. [f not specified,
NULL is the default value.
The following SQL query creates a user-defined data type for descriptive columns:
CREATE TYPE DSCRP
FROM varchar(l00) NOT NULL ;
In the preceding example, a user-defined data type DSCRP is created to store the varchar
data type and the size limit is specified as 100. Further, it also specifies NOT NULL.
Therefore, you can use this data for the columns that hold description, address, and
reason.
For example, you can use the DSCRP data type to store the data ofthe LeaveReason
column of the EmployeeLeave table, as shown in the following statement:
CREATE TABLE HumanResources.EmployeeLeave
(
EmployeeID int CONSTRAINT fkEmployeeID FOREIGN KEY REFERENCES
HumanResources.Employee(EmployeeID),
LeaveStartDate datetime CONSTRAINT cpkLeave-StartDate PRIMARY
KEY(EmployeeID, LeaveStartDate),
LeaveEndDate datetime NOT NULL,
LeaveReason DSCRP,
4.22 Managing Databases and Tables ©NUT
LeaveType char(2) CONSTRAINT chkLeave CHECK(LeaveType
IN('CL', 'SL', 'PL')) CONSTRAINT chkDefLeave DEFAULT 'PL'
Just a minute:
You want to create a rule, rule1, which allows the user to enter any ofthefour values:
Tea, Coffee, Soup, or Miranda in a column. Which command shouldyou execuie?
Answer:
CREATE RULE rulel
AS@TypeRule IN ('Tea', 'Coffee', 'Soup', 'Miranda')
, }
Managing Databases and Tables 4.23

More Related Content

Similar to Implement Data Integrity

Similar to Implement Data Integrity (20)

Les11 Including Constraints
Les11 Including ConstraintsLes11 Including Constraints
Les11 Including Constraints
 
Module 3
Module 3Module 3
Module 3
 
SQL
SQLSQL
SQL
 
Dms 22319 micro project
Dms 22319 micro projectDms 22319 micro project
Dms 22319 micro project
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
 
Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraint
 
Assignment#07
Assignment#07Assignment#07
Assignment#07
 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developer
 
Constraints constraints of oracle data base management systems
Constraints  constraints of oracle data base management systemsConstraints  constraints of oracle data base management systems
Constraints constraints of oracle data base management systems
 
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
 
SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data   SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data
 
zekeLabs sql-slides
zekeLabs sql-slideszekeLabs sql-slides
zekeLabs sql-slides
 
DDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleDDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using Oracle
 
Sql wksht-2
Sql wksht-2Sql wksht-2
Sql wksht-2
 
Bn1037 demo oracle sql
Bn1037 demo  oracle sqlBn1037 demo  oracle sql
Bn1037 demo oracle sql
 
Database
Database Database
Database
 
Python SQLite3...
Python                                                                SQLite3...Python                                                                SQLite3...
Python SQLite3...
 
Les10
Les10Les10
Les10
 
MySQL Presentation
MySQL PresentationMySQL Presentation
MySQL Presentation
 
Sql commands
Sql commandsSql commands
Sql commands
 

Recently uploaded

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 

Recently uploaded (20)

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 

Implement Data Integrity

  • 1. r Lecture 1 �- ·-· Implementing Data Integrity ""''"�•••J2c-,..,..,. ,,_.,.,..,,.-,..,�,.___,..,,,_ ......,..,,,,,.�..:,,,.,,,__,.3,,,,,.,,..,.,.;.iiii.:�--:,,i�....lf±'-" ·"".<•;..;:•,. ··.. ;-�,.,,..,.,.___,.__.;,;;;,;..,c-· "-"' >c-::-<!.,_._�..x•>-� .,......_..,,__,...,_,.L, ;, ,,.,; f Dr. Mohammed Zayed-
  • 2. -----,. ..-.- .. .-.� .. Implementing Data Integrity - -- - Ifchecks are not applied while defining and creating tables, the data stored in the tables can become redundant. For example, if you do not store the data about all the employees with complete address details, then the data would not be useful. Similarly, ifa database used by the Human Resource department stores employee contact details in two separate tables, the details of the employees might not match. This would result in inconsistency and confusion. Therefore, it is important to ensure that the data stored in tables is complete and consistent. The concept ofmaintaining consistency and completeness ofdata is called data integrity. Data integrity is enforced to ensure that the data in a database is accurate, consistent, and reliable. It is broadly classified into_the following categories: ■ Entity integrity: Ensures that each row can be uniquely identified by an att1ibute called the primary key. The primary key column contains unique value in all the rows. In addition, this column cannot be NULL. Consider a situation where there might be two candidates for an interview with the same name 'Jack'. By enforcing 4.12 Managing Databases and Tables ©NIIT
  • 3. -·---�-·-··-·_._._-- - entity integrity, the two candidates can be identified by using the unique code assigned to them. For example, one candidate can have the code 001 and the other candidate can be 002. ■ Domain integrity: Ensures that only a valid range of values is stored in a column. It can be enforced by restricting the type of data, the range of values, and the format of the data. For example, you have a table called BranchOffice with a column called city that stores the name of the cities, where the branch offices are located. The offices are located in 'Berkeley', 'Boston', 'Chicago', 'Dallas', 'Munchen', 'New Jersey', 'New York', 'Paris', and 'Washington'. By enforcing domain integrity, you can ensure that only valid values (as per the list specified) are entered in the City column ofthe BranchOffice table. ■ Referential integrity: Ensures that the values of the foreign key match the value of the corresponding primary key. For example, if a bicycle has been ordered and an entry is to be made in the OrderDetaj} table, then that bicycle code should exist in the product table. This ensures that an order is placed only for the bicycle that is available. ■ User-defined integrity: Refers to a set of rules specified by a user, which do not belong to the·entity, domain, and referential integrity categories. When creating tables, the SQL Server allows you to maintain integrity by: ■ Applying constraints ■ Applying rules ■ Using user-defined types Applying Constraints Consider an example where a user entered a duplicate value in the EmployeeID column of the Employee table. This would myan that two employees have same empl_oyee ID. This would further result in erroneous results when anybody queries the table. As a database developer, you can prevent this by enforcing data integrity on the table by using constraints. · · Co:q_strarnts define rules that must be followed to maintain CQil§isteQCY and correcmesJ, of data-:-A constraint can either be created while creating a table or can be added lat�r. When a constraint is added after the table is created, it checks the existing data. If there is any viol�tio�, th�i;i. the cpn�traint is rejected. A constraint can be created by using either ofthe following statements: a- -eREATFTABI:;E statement ■ ALTER TABLE statement ©NUT Managing Databases and Tables 4.13
  • 4. --�---.-.-.�---.-.--:- --- .. A constraint can be defined on a column while creating a table. It can be created with the CREATE TABLE statement. The syntax ofadding a constraint at the time of table creation is: CREATE TABLE table name column name CONSTRAINT constraint name constraint_type f,CONSTRAINT constraint name constraint_type] where, column name is the name ofthe column on which the constraint is to be defined. constraint name is the name ofthe constraint to be created and must follow the rules for the identifier. constraint_type is the type of constraint to be added. Constraints can be divided into the following types: II Primary key constraint • Unique constraint • Foreign key constraint • Check constraint • Defa ult constraint Primary Key Constraint A prima1y key constraint is defined on a column or a set of columns whose values uniquely identify all the rows in a table. These columns are referred to as the primary key columns. A primary key column cannot contain NULL values since it is used to uniquely identify rows in a table. The primary key constraint ensures entity integrity. You can define a primary key constraint while creating the table or you can add it later by altering the table. However, if you define the p1imary key constraint after inserting rows, the SQL Server will give an error if the rows contain duplicate values in the column. While defining a p1imary key constraint, you need to specify a name for the constraint. If a name is not specified, tbe SQL Server automatically assigns a name to the constraint. 4.14 Managing Databases and Tables ©NilT
  • 5. lf a primary key constraint is defined on a column that already contains. data, then the existing data in the column is screened. If any duplicate values are found, thenthe primary key constraint is rejected. The syntax of applying the primary key constraint when creating tabTe is: CREATE TJI.BLE table name col name [CONSTRAINT constraint name PRIMARY KEY [CLUSTEREDINONCLUSTERED) col name [, col name [, col name [, ...] ) ) where, constraint_name specifies the name of the constraint to be created. CLUSTERED I NONCLUSTERED are keywords that specify if a clustered or a nonclustered index is to be created for the primary key constraint. col_name specifies the name ofthe column(s) on which the primary key constraint is to .be defmed. Note You will learn more about indexes in Chapter 6. In the preceding example of the EmployeeLeave table, you can add a primaiy .key constraint, while creating the table. You can set the EmployeeID and the LeaveStartDate columns ofthe EmployeeLeave table as a composite primary key. You can use the following·statement to apply the·primary key constraint: CREATE TABLE HumanResources.EmployeeLeave ErnployeeID int, LeaveStartDate datetime CONSTRAINT cpkLeaveStartDate PRIMARY KEY(EmployeeID, LeaveStartDate), ---.-.--- r - -···· -- The preceding statement creates the EmployeeLeave table with a composite primary key constraint on EmployeeID and LeaveStartDate. The name ofthe constraint is cpk:LeaveStartDate. Managing Databases and Tab_les 4.15
  • 6. --� Unique Constraint The unique constraint is used to enforce uniqueness on non-primary key columns. A ptimary key constraint column automatically includes a restriction for uniqueness. The unique constraint is similar to the primary key constraint except that it allows one NULL row. Multiple unique constraints can be created on a table. The syntax ofapplying the unique constraint when creating table is: CREATE TABLE table name col_name [CONSTRAINT constraint_name UNIQUE [CLUSTERED NONCLUSTERED] (col_name [, col_name [, col_name [, ... )]]) col name [, col name [, col name [, ...]]] where, constraint_name specifies the name of the constraint to be created. CLUSTERED I NONCLUSTERED are keywords that specify if a clustered or a nonclustered index is to be created for the unique constraint. col_name specifies the name ofthe column(s) on which the unique constraint is to be defined. Foreign Key Constraint You can use the foreign key constraint to remove the inconsistency in two tables when the data in one table depends on the data in another table. A foreign key constraint associates one or more columns (the foreign key) of a table with an identical set of columns (a primary key column) in another table on which a primary key constraint has been defined. The syntax of applying the foreign key constraint when creating table is: CREATE TABLE table name ( col name [CONSTRAINT constraint name FOREIGN KEY (col_name [, col name [, ...]]) REFERENCES table name (column_name [, column name [, ...]]) ] (col_name [, col_name (, col name [, ...]]]) col name (, col name (, col name [, ...]]] 4.16 Managing Databases and Tables ©NTIT
  • 7. -......�- -··- - where, constraint name is the name of the constraint on which the foreign key constraint is to be defined. col name is the name of the column on which the foreign key constraint is to be enforced. table name is the name of the related table jn which the primary key constraint has been specified. column_name is the name of the primary key column ofthe related table on which the primary key constraint has been defined. In the example of the EmployeeLeave table under the HumanResources schema, you need to add the foreign key constraint to enforce referential integrity. In the HumanResources schema, the EmployeeID column is set as a primary key in the Employee table. Therefore, you need to set EmployeeID in the EmployeeLeave table as a foreign key. In the preceding example, you can use the following statement to apply the foreign key constraint in the EmployeeLeave table: CREATE TABLE HumanResources.EmployeeLeave EmployeeID int CONSTRAINT fkEmployeeID FOREIGN KEY REFERENCES HumanResources.Ernployee(EmployeeID), LeaveStartDate datetime CONSTRAINT cpkLeaveStartDate PRIMARY KEY(EmployeeID, LeaveStartDate), The preceding statement creates the EmployeeLeave table with a foreign key constraint on the EmployeeID column. The name of the constraint is fkEmployeeID. Check Constraint "' A check constraint enforces domain integrity by restricting the values to be inserted in a column. It is possible to define multiple check constraints on a single column. These are evaluated-in the order in which they are defined. The syntax of applying the check constraint is: CREATE TABLE table name col name [CONSTRAINT constraint_name) CHECK (expression) ©NIIT Managing Databases and Tables 4.17
  • 8. (coL name [, col name [, ... J]) A single check constraint can be applied to multiple columns when it is defined at the table level. where, constraint_name specifies the name of the constraint to be created. expression specifies the conditions that define the check to be made on the column. Expression can include elements, such as arithmetic operators, relational operators, or keywords, such as IN, LIKE, and BETWEEN. A check constraint can be specified by using the following keywords: rs IN: To ensure that the values entered are from a list ofconstant expressions. o LIKE: To ensure that the values entered in specific columns are of a certain pattern. This can be achieved by using wildcards. � BETWEEN: To specify a range of constant expressions by using the BETWEEN keyword. The upper and lower boundary values are included in the range. The rules regarding the creation of the CHECK constraint are as follows: t1 It can be created at the column level. WA It can contain user-specified search conditions. r1!i It cannot contain subqueries. ii It does not check the existing data in the table if created with the WITH NOCHECK option. m It can reference other columns of the same table. In the example ofthe EmployeeLeave table, you need to ensure that the leave type can take any one ofthe three values: CL, PL, or SL. For this, while creating the table, you can add the check constraint using the IN keyw·ord for the LeaveType column You can use the following statement to apply the check constraint: CREATE TABLE HurnanResources.EmployeeLeave EmployeeID int CONSTRAINT fkEmployeeID FOREIGN KEY REFERENCES HumanResources.Employee(EmployeeID), LeaveStartDate datetirne CONSTRAINT cpkLeaveStartDate PRIMARY KEY(EmployeeID, LeaveStartDate), LeaveEndDate datetime NOT NULL, LeaveReason varchar(l00), 4.18 Managing Databases and Tables ©NTIT
  • 9. LeaveType char(2) CONSTRAINT chkLeave CHECK(LeaveType IN ( I CL I ' 'SL I ' I PL I ) ) The preceding command creates the EmployeeLeave table with a check constraint on the LeaveType colu mn. The name ofthe constraint is chkLeave. Default Constraint A default constraint can be used to assign a·constant value to a column, and the user need not insert values for such a column. Only one default constraint can be created for a column but the column cannot be an identity column. The system-supplied values, such as USER, CURRENT_USER, and user-defined values can be assigned as defaults. The syntax ofapplying the default consh·aint while creating a table is: CREATE TABLE table name col name [CONSTRAINT constraint_name] DEFAULT (constant expression NULL) (col_name [, col name[, ...]]) where, constraint_name specifies the name ofthe constraint to be created. constant expression specifies an expression that contains only constant values. This can contain a NULL value. In the example ofcreating the EmployeeLeave table, you can insert a default constraint to add a default value for the LeaveType column. You can set �e default leave type as PL. You can use the following statement to create the default constraint: ©NUT CREATE TABLE HumanResources.EmployeeLeave ( .. EmployeeID int CONSTRAINT fkEmployeeID FOREIGN KEY REFERENCES HumanResources.Employee(EmployeeID), LeaveStartDate datet�me CONSTRAINT cpkLeaveStartDate PRIMARY·­ KEY(EmployeeID, LeaveStartDate), LeaveEndDate datetime NO T NULL, LeaveReason varchar(l00), LeaveType char(2) CONSTRAINT chkLeave CHECK(LeaveType IN('CL', 'SL', 'PL')) CONSTRAINT chkDefLeave DEFAULT 'PL' Managing Databases and Tables 4.19
  • 10. The preceding command creates the Employee Leave table with a default constraint on the LeaveType column, where the default value is spccificd as PL. The name ofthe constraint is chkDefLeave. Note You can also use the DEFAULTdatabase objects to create a de.fault constraint that can he applied to columns across tables within the same database. For this, you can create database ohjects by using the CREATE DEPA ULTstatement. J -------c:======-l Just a minute: Which keyword is used to specify a check constraint? Answer: A check constraint can be specified by using the LIKE, IN, and BETWEEN keywords. Applying Rules A rule enforces domain integrity for columns or user-defined data types. The rule is applied to the column or the user-defined data type before an INSERT or UPDATE statement is issued. fn otber words, a rule specifies a restriction on the values ofa column or a user-defined data type. Rules are used to implement business-related restrictions or limitations. A rnle can be created by using the CREATE RULE statement. The syntax of the CREATE RULE statement is: CREATE RULE rule name AS conditional_expression where, rule_name specifies the name of the new rnle that must conform to rules for identifiers. conditional_expression specifies the condition(s) that defines the rule. It can be any expression that is valid in a WHERE clause and can include elements, such as arithmetic operators, relational operators, TN, LIKE, and BETWEEN. The variable specified in the conditional expression must be prefixed whh the@ symbol. The expression refers to the value that is being specified with the INSERT or UPDATE statement. 4.20 Managing Databases and Tables ©NIIT
  • 11. ., . .:-- ,.. --• •• -.;,r -- . .. ... In the preceding example of the EmployeeLeave table, you applied a rule to accept only three values: 'CL', 'SL', and 'PL'. You can perform the same task by creating a rule. You can use the following statement to create the rule: CREATE RULE rulType AS @LeaveType IN ('CL', 'SL', 'PL') After you create the rule, you need to activate the rule by using a stored procedure, sp_bindrule. The syntax ofsp_bindrule is: sp_bindrule <'rule'>, <'object name'>, [<'futureonly_flag'>) where, rule specifies the name ofthe rule that you want to bind. object_name specifies the object on which you want to bind the rule. futureonly flag applies only when you want to bind the rule to a user-defined data type. Consider the following example, where the rulType rule created for the LeaveType column of the EmployeeLeave table is bound by using the sp_bindrule stored procedure. You can use the following statement to bind the rule. sp_bindrule 'rulType', 'HumanResources.EmployeeLeave.LeaveType' Similarly, when you want to remove the n1le, the sp_unbindrule stored procedure is used. For example, to remove the rule from the EmployeeLeave table, you can use the following statement to unbind the rule. sp_unbindrule 'HumanResources.EmployeeLeave.LeaveType' Using a User-Defined Data Type User-defined data types are custom data types defined by the users with a custom name. User-defined data types allow modifying the composite data type used in the database. The user-defined data types are based on the system data types and can be used to predefine several attributes of a column, such as its data typ_e, length, and whether it supports Nbl'..::t'values. ©NI1T Managing Databases and Tables 4.21
  • 12. You can create user-defined data types by using the CREATE TYPE statement. The syntax ofthe CREATE TYPE statement is: CREATE TYPE [ schema_name. ] type_name { FROM base_type [ ( precision [ , scale J ) ] [ NULL I NOT NULL l l [ ; ] where, schema_name specifies the name of the schema to which the alias data type or the user­ defined data type belongs. type_name specifies the name of the alias data type or the user-defined data type. base_type specifies the SQL Server supplied data type on which the alias data lype is based. precision specifies the decimal or nwneric point. Decimal and numeric are nonnegative integers that indicate the maximum total number ofdecimal digits that can be stored, both to the left and to the right of the decimal point. scale specifies the decimal or numeric scale. NULL I NOT NULL specifies whether the data type can hold a null value. [f not specified, NULL is the default value. The following SQL query creates a user-defined data type for descriptive columns: CREATE TYPE DSCRP FROM varchar(l00) NOT NULL ; In the preceding example, a user-defined data type DSCRP is created to store the varchar data type and the size limit is specified as 100. Further, it also specifies NOT NULL. Therefore, you can use this data for the columns that hold description, address, and reason. For example, you can use the DSCRP data type to store the data ofthe LeaveReason column of the EmployeeLeave table, as shown in the following statement: CREATE TABLE HumanResources.EmployeeLeave ( EmployeeID int CONSTRAINT fkEmployeeID FOREIGN KEY REFERENCES HumanResources.Employee(EmployeeID), LeaveStartDate datetime CONSTRAINT cpkLeave-StartDate PRIMARY KEY(EmployeeID, LeaveStartDate), LeaveEndDate datetime NOT NULL, LeaveReason DSCRP, 4.22 Managing Databases and Tables ©NUT
  • 13. LeaveType char(2) CONSTRAINT chkLeave CHECK(LeaveType IN('CL', 'SL', 'PL')) CONSTRAINT chkDefLeave DEFAULT 'PL' Just a minute: You want to create a rule, rule1, which allows the user to enter any ofthefour values: Tea, Coffee, Soup, or Miranda in a column. Which command shouldyou execuie? Answer: CREATE RULE rulel AS@TypeRule IN ('Tea', 'Coffee', 'Soup', 'Miranda') , } Managing Databases and Tables 4.23