Sie sind auf Seite 1von 26

Module 4

Ensuring Data Integrity Through


Constraints
Module Overview

• Enforcing Data Integrity


• Implementing Data Domain Integrity
• Implementing Entity and Referential Integrity
Lesson 1: Enforcing Data Integrity

• Data Integrity Across Application Layers


• Types of Data Integrity
• Options for Enforcing Data Integrity
Data Integrity Across Application Layers

• Applications are often layered in a hierarchy

• Integrity can be enforced at each level:


• User interface tier
• Middle tier
• Data tier

• It is likely to be found at all tiers:


• Leading to the complexity of keeping all the constraint
management functional and nonfunctional code
synchronised
Types of Data Integrity

• Domain Integrity
• Defines the allowed values in columns

• Entity Integrity
• Primary key uniquely identifies each row within a table

• Referential integrity
• Defines the relationship between tables
Options for Enforcing Data Integrity

• Data type: defines the type of data that can be


stored in a column
• Nullability: determines whether a value must be
present in a column
• Constraints: defines rules that limit the values
that can be stored in a column, or how values in
different columns must be related; also default
values
• Triggers: define code that is executed
automatically when data in a table is modified
Lesson 2: Implementing Data Domain Integrity

• Data Types
• DEFAULT Constraints
• CHECK Constraints
• Demonstration: Data and Domain Integrity
Data Types

• Choosing data types is an important decision


when designing tables
• They can be assigned by using:
• System data types
• Alias data types
• User-defined data types
DEFAULT Constraints

• Default constraints
• Provide default values for columns
• Used if INSERT provides no column value
• Must produce data compatible with the data type for
the column
CHECK Constraints

• Check constraints
• Limit the values that are accepted in a column
• Only rejects FALSE outcomes
• NULL evaluates to UNKNOWN and not FALSE
• Can be defined at table level to refer to multiple
columns
Demonstration: 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
• IDENTITY Constraints
• Working with Sequences
• Demonstration: Sequences Demonstration
• FOREIGN KEY Constraints
• Cascading Referential Integrity
• Considerations for Constraint Checking
• Demonstration: Entity and Referential Integrity
PRIMARY KEY Constraints

• Primary keys
• Are used to uniquely identify a row in a table
• Must be unique and not NULL
• May involve multiple columns that form a composite
key
UNIQUE Constraints

• Unique constraints
• Ensure that values in a column are unique
• One row may have a NULL value
• You can have multiple unique columns
IDENTITY Constraints

• IDENTITY property
• Automatically generates column values
• You can specify a seed (starting number) and an
increment
• Default seed and increment are both 1
• SCOPE IDENTITY(), @@IDENTITY return current value
Working with Sequences

• Sequence objects:
• Are user-defined, schema-bound objects
• Are not tied to any particular table
• Can be used to ease migration from other database
engines
Demonstration: Sequences Demonstration

In this demonstration, you will see how to:


• Work with identity constraints
• Create a sequence, and use it to provide key
values for multiple tables
FOREIGN KEY Constraints

• Foreign key constraints:


• Are used to enforce relationships between tables
• Check the existence of parent when inserting child
• Check the existence of children when deleting parent
• Can be self-referencing
Cascading Referential Integrity

• Cascading referential integrity


• Is controlled by the CASCADE option of the FOREIGN
KEY constraint
• NO ACTION (default): return error and rollback operation
• CASCADE: update foreign keys in referencing tables. Delete
rows in referencing tables
• SET DEFAULT: set foreign keys in referencing tables to default
values
• SET NULL: set foreign keys in referencing tables to NULL
Considerations for Constraint Checking

• Give constraints meaningful names


• Constraints can be created, changed, and
dropped without having to drop and recreate the
table
• Perform error checking in your applications and
transactions
• Referential constraints can be also be suspended
• To improve performance during large batch jobs
• To avoid checking existing data when you add new
constraints to a table containing valid data
• You must have the name that you or the system
supplied to suspend the checks
Demonstration: Entity and Referential Integrity

In this demonstration, you will see how to:


• Define entity integrity for tables
• Define referential integrity for tables
• Define cascading actions to relax the default
referential integrity constraint
Lab: Ensuring Data Integrity Through Constraints

• Exercise 1: Add Constraints


• Exercise 2: Test the Constraints

Logon Information
Virtual machine: 20762C-MIA-SQL
User name: ADVENTUREWORKS\Student
Password: Pa55w.rd

Estimated Time: 30 Minutes


Lab Scenario

A table named Yield has recently been added to


the Marketing schema within the database, but it
has no constraints in place. In this lab, you will
implement the required constraints to ensure data
integrity and, if you have time, test that
constraints work as specified.
Lab Scenario (Continued)

Column Name Data Type Required Validation Rule

OpportunityID Int Yes Part of the Primary key

ProspectID Int Yes Part of the key—also,


prospect must exist
DateRaised datetime Yes Must be today’s date

Likelihood Bit Yes

Rating char(1) Yes

EstimatedClosingDate date Yes

EstimatedRevenue decimal(10,2) Yes


Lab Review

• Why implement CHECK constraints if an


application is already checking the input data?
• What are some scenarios in which you might want
to temporarily disable constraint checking?
• True or false? A PRIMARY KEY and a UNIQUE
constraint are doing the same thing using
different code words.
Module Review and Takeaways

• Review Question(s)
• Best Practice

Das könnte Ihnen auch gefallen