You are on page 1of 20

Implementing Data Integrity

Vu Tuyet Trinh
trinhvt-fit@mail.hut.edu.vn

Hanoi University of Technology


1

Data Integrity

Defining the quality of the data in the database. Types:


Domain Integrity (columns)

User-defined integrity

Entity Integrity (rows)

Referential Integrity (between tables)


Microsoft

Enforcing Data Integrity

Declarative data integrity


Defined in object definitions Enforced automatically SQL Server by using constraints, defaults, and rules

Procedural data integrity


Defined in script Enforced by executing scripts, triggers and stored procedures

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

PRIMARY KEY constraints UNIQUE constraints FOREIGN KEY constraints CHECK constraints DEFAULT constraints Cascading referential integrity

Microsoft

PRIMARY KEY Constraints

Only one PRIMARY KEY constraint per table Unique values Not allowed NULL values Enforced with unique index

USE Northwind ALTER TABLE dbo.Customers ADD CONSTRAINT PK_Customers PRIMARY KEY NONCLUSTERED (CustomerID)

Microsoft

UNIQUE Constraints

Defined with one or more columns Allowing one null value Allowing multiple UNIQUE constraints on a table Enforced with a unique Index
USE Northwind ALTER TABLE dbo.Suppliers ADD CONSTRAINT U_CompanyName UNIQUE NONCLUSTERED (CompanyName)

Microsoft

FOREIGN KEY Constraints

Single or multi-column referential integrity Must reference a PRIMARY KEY or UNIQUE constraint Must have SELECT or REFERENCES permissions on referenced tables Use only REFERENCES clause within same table
USE Northwind ALTER TABLE dbo.Orders ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (CustomerID) REFERENCES dbo.Customers(CustomerID)

Microsoft

CHECK Constraints

Used with INSERT or UPDATE Statements Defined on one or more column(s) in the same table Cannot Be used with the rowversion data type

Contain subqueries

USE Northwind ALTER TABLE dbo.Employees ADD CONSTRAINT CK_birthdate CHECK (BirthDate > '01-01-1900' AND BirthDate <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 Northwind ALTER TABLE dbo.Customers ADD CONSTRAINT DF_contactname DEFAULT 'UNKNOWN' FOR ContactName
Microsoft

Cascading referential integrity

Defined for DETELE or UPDATE a key to which existing foreign keys point

[ON DELETE {NO ACTION|CASCADE|SET NULL|SET DEFAULT}] [ON UPDATE {NO ACTION|CASCADE|SET NULL|SET DEFAULT}]

Microsoft

Considerations for using constraints

Can be changed without recreating a table Require error-checking in applications and transactions Verify existing data?

If not case, disabling constraints

Microsoft

Disabling Constraints

Disabling constraint checking on existing data


Using WITH NOCHECK option when adding a new constraint Applied to CHECK and FOREIGN KEY constraints

USE Northwind ALTER TABLE dbo.Employees WITH NOCHECK ADD CONSTRAINT FK_Employees_Employees FOREIGN KEY (ReportsTo) REFERENCES dbo.Employees(EmployeeID)

Microsoft

Disabling Constraints

Disabling constraint checking when loading new data


Using NOCHECK option before loading new data Applied to CHECK and FOREIGN KEY Constraints

USE Northwind ALTER TABLE dbo.Employees NOCHECK CONSTRAINT FK_Employees_Employees

Microsoft

Default Constraints

Defined as independent objects Bound to one or more columns or user-defined data types

CREATE DEFAULT [schema_name.] default_name AS constant_expression [ ; ] EXEC sp_bindefault <default_name>, <Object_name> CREATE DEFAULT phone_no_default AS '(000)000-0000' GO EXEC sp_bindefault phone_no_default, 'Customers.Phone'
Microsoft

Rule Constraints
CREATE RULE [schema_name.] rule_name AS <condition_expression> GO EXEC sp_bindrule regioncode_rule,'Customers.Region'

CREATE RULE regioncode_rule AS @regioncode IN ('IA', 'IL', 'KS', 'MO') GO EXEC sp_bindrule regioncode_rule,'Customers.Region'

Microsoft

Note: CREATE RULE may be not supported anymore in next version of SQL Server

Enforcement Method to Use


Data integrity components Constraints Functionality Performance Before or after costs modification Medium Low Before

Defaults and rules


Triggers Data types, Null/Not Null

Low
High Low

Low

Before

Medium-High After Low Before

Microsoft

Summary

Data integrity as means for defining the correctness and the completeness of data
Enforcing data integrity as means for maintaining complete and correct database Using data integrity, using enforcement method for data integrity are important but only when necessary

Microsoft

Microsoft

Using Stored Procedure & Trigger

Microsoft

You might also like