23
Module 14 Ensuring Data Integrity through Constraints

Module 14 Ensuring Data Integrity through Constraints

  • Upload
    sumana

  • View
    76

  • Download
    2

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Module 14 Ensuring Data Integrity through Constraints

Module 14

Ensuring Data Integrity through Constraints

Page 2: Module 14 Ensuring Data Integrity through Constraints

Module Overview

• Enforcing Data Integrity

• Implementing Domain Integrity

• Implementing Entity and Referential Integrity

Page 3: Module 14 Ensuring Data Integrity through Constraints

Lesson 1: Enforcing Data Integrity

• Discussion: Data Integrity Across Application Layers

• Types of Data Integrity

• Options for Enforcing Data Integrity

Page 4: Module 14 Ensuring Data Integrity through Constraints

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?

Page 5: Module 14 Ensuring Data Integrity through Constraints

Types of Data Integrity

Entity Integrity(Rows)

Domain Integrity(Columns)

Referential Integrity(Between Tables or Columns in different rows of the same Table)

Page 6: Module 14 Ensuring Data Integrity through Constraints

Options for Enforcing Data Integrity

Mechanism Description

Data typesDefines the type of data that can be stored in a column

NullabilityDetermines whether or not a value must be present in a column

Default valuesDefines the value of a column if a value is not specified

ConstraintsDefines rules that limit the values that might be stored in a column or how values in different columns in a table must be related

TriggersDefines 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.

Page 7: Module 14 Ensuring Data Integrity through Constraints

Lesson 2: Implementing Domain Integrity

• Data Types

• Column Nullability

• DEFAULT Constraints

• CHECK Constraints

• Demonstration 2A: Data and Domain Integrity

Page 8: Module 14 Ensuring Data Integrity through Constraints

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 intRating int

);

CREATE TABLE Sales.Opportunity(

OpportunityID int,Requirements nvarchar(50),ReceivedDate date,LikelyClosingDate date,SalespersonID intRating int

);

Page 9: Module 14 Ensuring Data Integrity through Constraints

Column Nullability

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

);

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

);

• 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

Page 10: Module 14 Ensuring Data Integrity through Constraints

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 intRating int

);

CREATE TABLE Sales.Opportunity(

OpportunityID int,Requirements nvarchar(50),ReceivedDate date CONSTRAINT DF_Opportunity_ReceivedDate DEFAULT (SYSDATETIME()),LikelyClosingDate date,SalespersonID intRating int

);

Page 11: Module 14 Ensuring Data Integrity through Constraints

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)

);

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)

);

Page 12: Module 14 Ensuring Data Integrity through Constraints

Demonstration 2A: Data and Domain Integrity

In this demonstration, you will see how to enforce data and domain integrity

Page 13: Module 14 Ensuring Data Integrity through Constraints

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

Page 14: Module 14 Ensuring Data Integrity through Constraints

PRIMARY KEY Constraints

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

);

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

);

• Is used to uniquely identify a row in a table

• Must be unique and not NULL

• May involve multiple columns

Page 15: Module 14 Ensuring Data Integrity through Constraints

UNIQUE Constraints

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

);

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

);

• Requires that values in each row are different if supplied

• Must be unique but one row can be NULL

• May involve multiple columns

Page 16: Module 14 Ensuring Data Integrity through Constraints

FOREIGN KEY Constraints

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

);

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

);

• Is used to enforce relationships between tables

• Must reference PRIMARY KEY or UNIQUE column(s)

• May be NULL

• Can be applied WITH NOCHECK

Page 17: Module 14 Ensuring Data Integrity through Constraints

Cascading Referential Integrity

Option UPDATE Behavior DELETE behavior

NO ACTION (default) Return error and roll back operation

CASCADE 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

Controlled by CASCADE clause of FOREIGN KEYControlled by CASCADE clause of FOREIGN KEY

Page 18: Module 14 Ensuring Data Integrity through Constraints

Considerations for Constraint Checking

Create, change, and drop constraints without having to drop and recreate the table

Assign meaningful names to constraints

• To improve performance during large batch jobs

• To avoid checking existing data when you add new constraints to a table

Disable CHECK and FOREIGN KEY constraints:

Perform error checking in your applications and transactions

üü

üü

üü

Page 19: Module 14 Ensuring Data Integrity through Constraints

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

Page 20: Module 14 Ensuring Data Integrity through Constraints

Lab 14: Ensuring Data Integrity through Constraints

• Exercise 1: Constraint Design

• Challenge Exercise 2: Test the constraints (Only if time permits)

Logon information

Estimated time: 45 minutes

Virtual machine 623XB-MIA-SQL

User name AdventureWorks\Administrator

Password Pa$$w0rd

Page 21: Module 14 Ensuring Data Integrity through Constraints

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.

Page 22: Module 14 Ensuring Data Integrity through Constraints

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?

Page 23: Module 14 Ensuring Data Integrity through Constraints

Module Review and Takeaways

• Review Questions

• Best Practices