Beruflich Dokumente
Kultur Dokumente
Implementing
Data Integrity
Vidya Vrat Agarwal. | MCT, MCSD
Overview
Referential Integrity
(between tables)
Enforcing Data Integrity
Type
Typeof
ofintegrity
integrity Constraint
Constrainttype
type
DEFAULT
DEFAULT
Domain
Domain CHECK
CHECK
REFERENTIAL
REFERENTIAL
PRIMARY
PRIMARYKEY
KEY
Entity
Entity UNIQUE
UNIQUE
FOREIGN
FOREIGNKEY
KEY
Referential
Referential CHECK
CHECK
Creating Constraints
You should specify names for constraints when you create them,
because SQL Server provides complicated, system-generated
names. Names must be unique to the database object owner and
follow the rules for SQL Server identifiers.
Types of Constraints
DEFAULT Constraints
CHECK Constraints
PRIMARY KEY Constraints
UNIQUE Constraints
FOREIGN KEY Constraints
Cascading Referential Integrity
DEFAULT Constraints
USE
USE Northwind
Northwind
ALTER
ALTER TABLE
TABLE Customers
Customers
ADD
ADD CONSTRAINT
CONSTRAINT DF_contactname
DF_contactname DEFAULT
DEFAULT 'UNKNOWN'
'UNKNOWN'
FOR
FOR ContactName
ContactName
CHECK Constraints
Are Used with INSERT and UPDATE Statements
A CHECK constraint restricts the data that users can enter into a
particular column to specific values. CHECK constraints are similar to
WHERE clauses in that you can specify the conditions under which data
will be accepted.
USE
USE Northwind
Northwind
ALTER
ALTER TABLE
TABLE Customers
Customers
ADD
ADD CONSTRAINT
CONSTRAINT PK_Customers
PK_Customers
PRIMARY
PRIMARY KEY
KEY NONCLUSTERED
NONCLUSTERED (CustomerID)
(CustomerID)
UNIQUE Constraints
A UNIQUE constraint specifies that two rows in a column cannot have the
same value. This constraint enforces entity integrity with a unique index.
A UNIQUE constraint is helpful when you already have a primary key, such
as an employee number, but you want to guarantee that other identifiers,
such as an employee’s driver’s license number, are also unique.
Allow One Null Value
Allow Multiple UNIQUE Constraints on a Table
Defined with One or More Columns
Enforced with a Unique Index
USE
USE Northwind
Northwind
ALTER
ALTER TABLE
TABLE Suppliers
Suppliers
ADD
ADD CONSTRAINT
CONSTRAINT U_CompanyName
U_CompanyName
UNIQUE
UNIQUE NONCLUSTERED
NONCLUSTERED (CompanyName)
(CompanyName)
FOREIGN KEY Constraints
A FOREIGN KEY constraint enforces referential integrity. The FOREIGN
KEY constraint defines a reference to a column with a PRIMARY KEY or
UNIQUE constraint in the same, or another table.
USE
USE Northwind
Northwind
ALTER
ALTER TABLE
TABLE Orders
Orders
ADD
ADD CONSTRAINT
CONSTRAINT FK_Orders_Customers
FK_Orders_Customers
FOREIGN
FOREIGN KEY
KEY (CustomerID)
(CustomerID)
REFERENCES
REFERENCES Customers(CustomerID)
Customers(CustomerID)
Cascading Referential Integrity
1 1
INSERT
INSERT new
new UPDATE
UPDATE CustomerID
CustomerID
CustomerID
CustomerID
CASCADE
Orders
Orders
CustomerID (FK) Orders
Orders
2 CustomerID (FK)
UPDATE
UPDATE old
old
CustomerID
CustomerID to
to new
new
CustomerID
CustomerID
Customers
Customers
CustomerID (PK)
3
DELETE
DELETE old
old
CustomerID
CustomerID
Using Defaults and Rules
CREATE
CREATE DEFAULT
DEFAULT phone_no_default
phone_no_default
AS
AS '(000)000-0000'
'(000)000-0000' Table Name
GO
GO Column Name
EXEC sp_bindefault phone_no_default,
EXEC sp_bindefault phone_no_default,
'Customers.Phone'
'Customers.Phone'
CREATE
CREATE RULE
RULE regioncode_rule
regioncode_rule
AS
AS @regioncode
@regioncode IN
IN ('IA',
('IA', 'IL',
'IL', 'KS',
'KS', 'MO')
'MO')
GO
GO
EXEC
EXEC sp_bindrule
sp_bindrule regioncode_rule,
regioncode_rule,
'Customers.Region'
'Customers.Region'
Unbinding Defaults and Rules
Data
Dataintegrity
integrity Functionality
Functionality Performance
Performance Before
Beforeor
orafter
after
components
components costs
costs modification
modification
Constraints
Constraints Medium
Medium Low
Low Before
Before
Defaults
Defaultsand
andrules
rules Low
Low Low
Low Before
Before
Triggers
Triggers High
High Medium-High
Medium-High After
After
Data
Datatypes,
types, Low
Low Low
Low Before
Before
Null/Not
Null/NotNull
Null
Check Your Understanding.
Q.1. What is Data Integrity.? What are the Types of data
integrity.?
Q.2. What are the ways to Enforce Data Integrity.?
Q.3. What is Default constraint.?
Q.4. What is Check Constraint.?
Q.5. What is Primary Key.?
Q.6. What is Foreign Key.?
Q.7. What is Unique Constraint.?
Q.8. Primary Key allows Null Values. ( T / F)
Q.9. How many Null values are allowed by Unique
Constraint .?
Q.10. What is the difference between Default and Rule.?
Q.11. Which two commands work in Combination to
Create and bind Rule and Default..?
Q.12. What is Cascading .? What is the action of
No Action.?
Q.13. What is the difference between Unbinding and
Drop.?
Review
Thank You.