6232B_14

advertisement
Module 14
Ensuring Data Integrity
through Constraints
Module Overview
• Enforcing Data Integrity
• Implementing Domain Integrity
• Implementing Entity and Referential Integrity
Lesson 1: Enforcing Data Integrity
• Discussion: Data Integrity Across Application Layers
• Types of Data Integrity
• Options for Enforcing Data Integrity
Discussion: Data Integrity Across Application Layers
• Consider these issues:

We need to make sure that orders are only placed for
customers that exist.

We need to ensure that a particular column contains an
integer with a value between 1 and 9.

We need to ensure that a particular column always contains a
value.

We need to ensure that product specials are only ordered on
Tuesdays in summer.
• Should the application enforce these?
• Should the database enforce these?
Types of Data Integrity
Domain Integrity
(Columns)
Entity Integrity
(Rows)
Referential Integrity
(Between Tables or Columns in
different rows of the same
Table)
Options for Enforcing Data Integrity
Mechanism
Description
Data types
Defines the type of data that can be stored
in a column
Nullability
Determines whether or not a value must be
present in a column
Default values
Defines the value of a column if a value is
not specified
Constraints
Defines rules that limit the values that might
be stored in a column or how values in
different columns in a table must be related
Triggers
Defines code that is executed automatically
when a table is modified
Earlier versions of SQL Server used Rules and Defaults as
separate objects. These should no longer be used.
Lesson 2: Implementing Domain Integrity
• Data Types
• Column Nullability
• DEFAULT Constraints
• CHECK Constraints
• Demonstration 2A: Data and Domain Integrity
Data Types
• Important decision when designing tables
• Can be assigned using:

System Data Types

Alias Data Types

User-defined Data Types
CREATE TABLE Sales.Opportunity
(
OpportunityID int,
Requirements nvarchar(50),
ReceivedDate date,
LikelyClosingDate date,
SalespersonID int
Rating int
);
Column Nullability
• Determines whether a value must be provided for a column
• Allows to specify the value explicitly as NULL in an INSERT
• Is often inappropriately defined
CREATE TABLE Sales.Opportunity
(
OpportunityID int NOT NULL,
Requirements nvarchar(50) NOT NULL,
ReceivedDate date NOT NULL,
LikelyClosingDate date NULL,
SalespersonID int NULL,
Rating int NOT NULL
);
DEFAULT Constraints
• Provide default values for columns
• Are used when no value is provided in an INSERT statement
• Must be compatible with the data type for the column
CREATE TABLE Sales.Opportunity
(
OpportunityID int,
Requirements nvarchar(50),
ReceivedDate date
CONSTRAINT DF_Opportunity_ReceivedDate
DEFAULT (SYSDATETIME()),
LikelyClosingDate date,
SalespersonID int
Rating int
);
CHECK Constraints
• Limits the values that are accepted into a column
• Values that evaluate to FALSE are rejected
• Care must be taken with values returning NULL
• Can be defined at table level to refer to multiple columns
CREATE TABLE Sales.Opportunity
(
OpportunityID int,
Requirements nvarchar(50),
ReceivedDate date,
LikelyClosingDate date,
SalespersonID int,
Rating int
CONSTRAINT CHK_Opportunity_Rating_Range1To4
CHECK (Rating BETWEEN 1 AND 4)
);
Demonstration 2A: Data and Domain Integrity
In this demonstration, you will see how to enforce data and
domain integrity
Lesson 3: Implementing Entity and Referential
Integrity
• PRIMARY KEY Constraints
• UNIQUE Constraints
• FOREIGN KEY Constraints
• Cascading Referential Integrity
• Considerations for Constraint Checking
• Demonstration 3A: Entity and Referential Integrity
PRIMARY KEY Constraints
• Is used to uniquely identify a row in a table
• Must be unique and not NULL
• May involve multiple columns
CREATE TABLE Sales.Opportunity
(
OpportunityID int NOT NULL
CONSTRAINT PK_Opportunity
PRIMARY KEY,
Requirements nvarchar(50) NOT NULL,
ReceivedDate date NOT NULL,
LikelyClosingDate date NULL,
SalespersonID int NULL,
Rating int NOT NULL
);
UNIQUE Constraints
• Requires that values in each row are different if supplied
• Must be unique but one row can be NULL
• May involve multiple columns
CREATE TABLE Sales.Opportunity
(
OpportunityID int NOT NULL
CONSTRAINT PK_Opportunity
PRIMARY KEY,
Requirements nvarchar(50) NOT NULL
CONSTRAINT UQ_Opportunity_Requirements
UNIQUE,
ReceivedDate date NOT NULL,
LikelyClosingDate date NULL,
SalespersonID int NULL,
Rating int NOT NULL
);
FOREIGN KEY Constraints
• Is used to enforce relationships between tables
• Must reference PRIMARY KEY or UNIQUE column(s)
• May be NULL
• Can be applied WITH NOCHECK
CREATE TABLE Sales.Opportunity
(
OpportunityID int NOT NULL
CONSTRAINT PK_Opportunity
PRIMARY KEY,
Requirements nvarchar(50) NOT NULL,
ReceivedDate date NOT NULL,
LikelyClosingDate date NULL,
SalespersonID int NULL
CONSTRAINT FK_Opportunity_Salesperson
FOREIGN KEY
REFERENCES Sales.Salesperson (BusinessEntityID),
Rating int NOT NULL
);
Cascading Referential Integrity
Controlled by CASCADE clause of FOREIGN KEY
Option
NO ACTION
(default)
CASCADE
UPDATE Behavior
DELETE behavior
Return error and roll back operation
Update foreign keys in
referencing tables
Delete rows in
referencing table
SET NULL
Set foreign keys in referencing tables to NULL
SET
DEFAULT
Set foreign keys in referencing tables to DEFAULT
values
Considerations for Constraint Checking

Assign meaningful names to constraints

Create, change, and drop constraints without having
to drop and recreate the table

Perform error checking in your applications and
transactions
Disable CHECK and FOREIGN KEY constraints:
• To improve performance during large batch jobs
• To avoid checking existing data when you add new
constraints to a table
Demonstration 3A: Entity and Referential Integrity
In this demonstration, you will see how to define:
• Entity integrity for tables
• Referential integrity for tables
• Cascading referential integrity constraints
Lab 14: Ensuring Data Integrity through
Constraints
• Exercise 1: Constraint Design
• Challenge Exercise 2: Test the constraints (Only if time
permits)
Logon information
Virtual machine
623XB-MIA-SQL
User name
AdventureWorks\Administrator
Password
Pa$$w0rd
Estimated time: 45 minutes
Lab Scenario
A table has recently been added to the Marketing system
but has no constraints in place. In this lab, you will
implement the required constraints to ensure data integrity.
You have been supplied with the required specifications of a
new table called Marketing.Yield. You need to implement
the required constraints and, if you have time, test their
behavior.
Lab Review
• In SQL Server Management Studio, you successfully ran a
script that created a table but you don’t see the table in
Object Explorer. What do you need to do?
• What does the option Default do when creating a column?
• What requirement does a primary key constraint have that
a unique constraint doesn’t?
Module Review and Takeaways
• Review Questions
• Best Practices
Download