Implementing Data Integrity: Hanoi University of Technology
Implementing Data Integrity: Hanoi University of Technology
Data Integrity
Vu Tuyet Trinh
[email protected]
Hanoi University of Technology
1
Data Integrity
Referential
Integrity
(between tables)
Microsoft
Enforcing Data Integrity
Microsoft
Outline
Data Integrity
Enforcing Data Integrity
Types of Constraints
Defining Constraints
Disabling Constraints
Using Defaults and Rules
Deciding enforcement method to use
Microsoft
Types of Constraints
Microsoft
PRIMARY KEY Constraints
USE
USE Northwind
Northwind
ALTER
ALTER TABLE
TABLE dbo.Customers
dbo.Customers
ADD
ADD CONSTRAINT
CONSTRAINT PK_Customers
PK_Customers
PRIMARY
PRIMARY KEY
KEY NONCLUSTERED
NONCLUSTERED (CustomerID)
(CustomerID)
Microsoft
UNIQUE Constraints
USE
USE Northwind
Northwind
ALTER
ALTER TABLE
TABLE dbo.Suppliers
dbo.Suppliers
ADD
ADD CONSTRAINT
CONSTRAINT U_CompanyName
U_CompanyName
UNIQUE
UNIQUE NONCLUSTERED
NONCLUSTERED (CompanyName)
(CompanyName)
Microsoft
FOREIGN KEY Constraints
USE
USE Northwind
Northwind
ALTER
ALTER TABLE
TABLE dbo.Orders
dbo.Orders
ADD
ADD CONSTRAINT
CONSTRAINT FK_Orders_Customers
FK_Orders_Customers
FOREIGN
FOREIGN KEY
KEY (CustomerID)
(CustomerID)
REFERENCES
REFERENCES dbo.Customers(CustomerID)
dbo.Customers(CustomerID)
Microsoft
CHECK Constraints
USE
USE Northwind
Northwind
ALTER
ALTER TABLE
TABLE dbo.Employees
dbo.Employees
ADD
ADD CONSTRAINT
CONSTRAINT CK_birthdate
CK_birthdate
CHECK
CHECK (BirthDate
(BirthDate >> '01-01-1900'
'01-01-1900' AND
AND BirthDate
BirthDate <getdate())
<getdate())
Microsoft
DEFAULT Constraints
Applied for INSERT statements
Only one DEFAULT constraint per column
Cannot be used with IDENTITY property
or rowversion data type
Allowing some system-supplied values
USE
USE Northwind
Northwind
ALTER
ALTER TABLE
TABLE dbo.Customers
dbo.Customers
ADD
ADD CONSTRAINT
CONSTRAINT DF_contactname
DF_contactname
DEFAULT
DEFAULT 'UNKNOWN'
'UNKNOWN'
FOR
FOR ContactName
ContactName
Microsoft
Cascading referential integrity
Microsoft
Considerations for using constraints
Microsoft
Disabling Constraints
USE
USE Northwind
Northwind
ALTER
ALTER TABLE
TABLE dbo.Employees
dbo.Employees
WITH
WITH NOCHECK
NOCHECK
ADD
ADD CONSTRAINT
CONSTRAINT FK_Employees_Employees
FK_Employees_Employees
FOREIGN
FOREIGN KEY
KEY (ReportsTo)
(ReportsTo)
REFERENCES
REFERENCES dbo.Employees(EmployeeID)
dbo.Employees(EmployeeID)
Microsoft
Disabling Constraints
USE
USE Northwind
Northwind
ALTER
ALTER TABLE
TABLE dbo.Employees
dbo.Employees
NOCHECK
NOCHECK
CONSTRAINT
CONSTRAINT FK_Employees_Employees
FK_Employees_Employees
Microsoft
Default Constraints
EXEC
EXEC sp_bindefault
sp_bindefault <default_name>,
<default_name>, <Object_name>
<Object_name>
CREATE
CREATE DEFAULT
DEFAULT phone_no_default
phone_no_default
AS
AS '(000)000-0000'
'(000)000-0000'
GO
GO
EXEC
EXEC sp_bindefault
sp_bindefault phone_no_default,
phone_no_default, 'Customers.Phone'
'Customers.Phone'
Microsoft
Rule Constraints
CREATE
CREATE RULE
RULE [schema_name.]
[schema_name.] rule_name
rule_name
AS
AS <condition_expression>
<condition_expression>
GO
GO
EXEC
EXEC sp_bindrule
sp_bindrule regioncode_rule,'Customers.Region'
regioncode_rule,'Customers.Region'
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,'Customers.Region'
regioncode_rule,'Customers.Region'
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
Microsoft
Summary
Microsoft
Microsoft
Using Stored Procedure & Trigger
Microsoft