Ensuring Data Integrity Through Constraints
Ensuring Data Integrity Through Constraints
Ensuring Data Integrity Through Constraints
Module Overview
Enforcing Data Integrity
Implementing Domain Integrity
Implementing Entity and Referential Integrity
between 1 and 9.
Entity Integrity
(Rows)
Referential Integrity
(Between Tables or Columns in
different rows of the same
Table)
Description
Data types
Nullability
Constraints
Triggers
Data Types
Important decision when designing tables
Can be assigned using:
CREATE
CREATE TABLE
TABLE Sales.Opportunity
Sales.Opportunity
(
(
OpportunityID
OpportunityID int
int NOT
NOT NULL,
NULL,
Requirements
Requirements nvarchar(50)
nvarchar(50) NOT
NOT NULL,
ReceivedDate
ReceivedDate date
date NOT
NOT NULL,
NULL,
LikelyClosingDate
LikelyClosingDate date
date NOT
NOT NULL,
NULL,
SalespersonID
SalespersonID int
int NOT
NOT NULL,
NULL,
Rating
Rating int
int NOT
NOT NULL
NULL
);
);
Column Nullability
Determines whether a value must be provided for a column
Allows you to specify the value explicitly as NULL in an INSERT
Is often inappropriately defined
CREATE
CREATE TABLE
TABLE Sales.Opportunity
Sales.Opportunity
(
(
OpportunityID
OpportunityID int NOT NULL,
Requirements
Requirements nvarchar(50)
nvarchar(50) NOT
NOT NULL,
NULL,
ReceivedDate
ReceivedDate date
date NOT
NOT NULL,
NULL,
LikelyClosingDate
LikelyClosingDate date
date NULL,
NULL,
SalespersonID
SalespersonID int NULL,
Rating
Rating int
int NOT
NOT NULL
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
TABLE Sales.Opportunity
Sales.Opportunity
(
OpportunityID int
int NOT
NOT NULL,
NULL,
Requirements nvarchar(50)
nvarchar(50) NOT
NOT NULL,
NULL,
ReceivedDate date
date
CONSTRAINT
CONSTRAINT DF_Opportunity_ReceivedDate
DF_Opportunity_ReceivedDate
DEFAULT
DEFAULT (SYSDATETIME())
(SYSDATETIME()) NOT
NOT NULL,
NULL,
LikelyClosingDate date
date NOT
NOT NULL,
NULL,
SalespersonID int
int NOT
NOT NULL,
NULL,
Rating int
int NOT
NOT NULL
NULL
);
);
CHECK Constraints
Limits the values that are accepted into a column
Values that evaluate to FALSE are rejected
Care must be taken with nullable columns
Can be defined at table level to refer to multiple columns
CREATE
CREATE TABLE
TABLE Sales.Opportunity
Sales.Opportunity
((
OpportunityID
OpportunityID int
int NOT
NOT NULL,
NULL,
Requirements
Requirements nvarchar(50)
nvarchar(50) NOT
NOT NULL,
NULL,
ReceivedDate
ReceivedDate date
date NOT
NOT NULL,
NULL,
LikelyClosingDate
LikelyClosingDate date
date NOT
NOT NULL,
NULL,
SalespersonID
SalespersonID int
int NOT
NOT NULL,
NULL,
Rating
Rating int
int
CONSTRAINT
CONSTRAINT CHK_Opportunity_Rating_Range1To4
CHK_Opportunity_Rating_Range1To4
CHECK
CHECK (Rating
(Rating BETWEEN
BETWEEN 11 AND
AND 4)
4) NOT
NOT NULL
NULL
);
);
CREATE TABLE
TABLE Sales.Opportunity
Sales.Opportunity
(
OpportunityID int
int NOT
NOT NULL
NULL
CONSTRAINT
CONSTRAINT PK_Opportunity
PK_Opportunity
PRIMARY
PRIMARY KEY,
KEY,
Requirements nvarchar(50)
nvarchar(50) NOT
NOT NULL,
NULL,
ReceivedDate date
date NOT
NOT NULL,
NULL,
LikelyClosingDate date
date NULL,
NULL,
SalespersonID int
int NULL,
NULL,
Rating int
int NOT
NOT NULL
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
CREATE TABLE
TABLE Sales.Opportunity
Sales.Opportunity
((
OpportunityID
OpportunityID int
int NOT
NOT NULL
NULL
CONSTRAINT
CONSTRAINT PK_Opportunity
PK_Opportunity
PRIMARY
PRIMARY KEY,
KEY,
Requirements
Requirements nvarchar(50)
nvarchar(50) NOT
NOT NULL
NULL
CONSTRAINT
CONSTRAINT UQ_Opportunity_Requirements
UQ_Opportunity_Requirements
UNIQUE,
UNIQUE,
ReceivedDate
ReceivedDate date
date NOT
NOT NULL,
NULL,
LikelyClosingDate
LikelyClosingDate date
date NULL,
NULL,
SalespersonID
SalespersonID int
int NULL,
NULL,
Rating
Rating int
int NOT
NOT NULL
NULL
);
);
Option
NO ACTION
(default)
CASCADE
UPDATE Behavior
DELETE behavior
Delete rows in
referencing table
SET NULL
SET
DEFAULT
transactions
IDENTITY Property
Property of a column
Specify a seed and an increment
Default seed and increment are both 1
SCOPE_IDENTITY(), @@IDENTITY
CREATE
CREATE TABLE
TABLE Sales.Opportunity
Sales.Opportunity
((
OpportunityID
OpportunityID int
int NOT
NOT NULL
NULL
IDENTITY(1,1),
IDENTITY(1,1),
Requirements
Requirements nvarchar(50)
nvarchar(50) NOT
NOT NULL,
NULL,
ReceivedDate
ReceivedDate date
date NOT
NOT NULL,
NULL,
LikelyClosingDate
LikelyClosingDate date
date NULL,
NULL,
SalespersonID
SalespersonID int
int NULL,
NULL,
Rating
Rating int
int NOT
NOT NULL
NULL
);
);
Sequences
Are user-defined schema-bound objects
Are not tied to any particular table
Can be used to ease migration from other database engines
CREATE SEQUENCE
SEQUENCE Booking.BookingID
Booking.BookingID AS
AS INT
INT
START
START WITH
WITH 20001
20001
INCREMENT
INCREMENT BY
BY 10;
10;
CREATE TABLE
TABLE Booking.FlightBooking
Booking.FlightBooking
( FlightBookingID
FlightBookingID INT
INT NOT
NOT NULL
NULL PRIMARY
PRIMARY KEY
KEY
CLUSTERED
CLUSTERED
DEFAULT
DEFAULT (NEXT VALUE
VALUE FOR
FOR Booking.BookingID),
Booking.BookingID),
...
...
permits)
Logon information
Virtual machine
10776A-MIA-SQL1
User name
AdventureWorks\Administrator
Password
Pa$$w0rd
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 dont see the table in
Object Explorer. What do you need to do?