SlideShare a Scribd company logo
Programming Microsoft SQL
Server
03 - Constraints
Constraints Types
Domain Integrity
(Columns)
Entity Integrity
(Rows)
Referential Integrity
(Between Tables or Columns of the Same Table)
What Are Constraints?
Integrity
type
Constraint
type
Description
Domain
DEFAULT Specifies default value for column
CHECK Specifies allowed value for column
FOREIGN KEY Specifies column in which values must exist
NULL Specifies whether NULL is permitted
Entity
PRIMARY KEY Identifies each row uniquely
UNIQUE Prevents duplication of nonprimary keys
Referential
FOREIGN KEY
Defines columns whose value must match the primary key of
this table
CHECK
Specifies the allowed value for a column based on the
contents of another column
Constraint Scope
CREATE TABLE [dbo].[Customers] (
[CustomerID] [int] IDENTITY (1, 1) NOT NULL PRIMARY KEY ,
[CustomerEmail] [varchar] (30) NULL ,
[CustomerState] [varchar] (5) NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Products] ADD
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[ProductID],
[ProductName]
) ON [PRIMARY]
GO
• Column Level
• Table Level
PRIMARY KEY Constraints
 PRIMARY KEY constraints identify one or
more columns in a table that constitute a
primary key
 One PRIMARY KEY constraint is allowed per
table
 Value must be unique across constituent
columns
 Null values are not allowed in constituent
columns
PRIMARY KEY Constraints
CREATE TABLE [dbo].[DEPARTMENTS](
[DNO] [int] IDENTITY(1,1) PRIMARY KEY,
[DNAME] [nvarchar](20) NULL
) ON [PRIMARY]
CREATE TABLE [HumanResources].[Department](
[DepartmentID] [smallint] IDENTITY(1,1) NOT NULL,
[Name] [dbo].[Name],
…
CONSTRAINT [PK_Department_DepartmentID] PRIMARY KEY CLUSTERED
([DepartmentID] ASC) WITH (IGNORE_DUP_KEY = OFF) ON
[PRIMARY]
)
Default Constraints
 DEFAULT constraints define a default column
value when no value is provided
 Only one DEFAULT constraint per column
 Only applicable to INSERT statements
 Some system supplied functions allowed
CREATE TABLE [Production].[Location](
...
[Availability] [decimal](8, 2) NOT NULL CONSTRAINT
[DF_Location_Availability] DEFAULT (0.00),
[ModifiedDate] [datetime] NOT NULL CONSTRAINT
[DF_Location_ModifiedDate] DEFAULT (getdate())
)
Check Constraints
 CHECK constraints restrict the values that can
be entered into a column on INSERT or
UPDATE.
 Can define multiple CHECK constraints per
column.
 Can reference columns in the same table.
 Cannot contain subqueries.
ALTER TABLE [HumanResources].[EmployeeDepartmentHistory]
WITH CHECK
ADD CONSTRAINT [CK_EmployeeDepartmentHistory_EndDate] CHECK
(([EndDate]>=[StartDate] OR [EndDate] IS NULL))
Disabling Constraints
 Bad Data During Constraint Creation?
ALTER TABLE [dbo].[Products]
WITH NOCHECK
ADD CONSTRAINT [CK_Products_No_Nissan]
CHECK ([Productname] <> 'nissan sentra')
 Temporarily During Data Load
ALTER TABLE Products
NOCHECK
CONSTRAINT CK_Products_No_Nissan
UNIQUE Constraint
 UNIQUE constraints ensure that every value in a
column is unique
 Only one null value allowed in a unique column
 Can include one or more columns
CREATE TABLE [HumanResources].[Employee](
[EmployeeID] [int] IDENTITY(1,1) NOT NULL,
[NationalIDNumber] [nvarchar](15) NOT NULL UNIQUE
NONCLUSTERED,
…
)
FOREIGN KEY Constraints
 FOREIGN KEY constraints ensure referential
integrity between columns in same or
different tables
 Must reference a PRIMARY KEY or UNIQUE
constraint
 User must have REFERENCES permission on
referenced table
ALTER TABLE [Sales].[SalesOrderHeader] WITH CHECK
ADD CONSTRAINT
[FK_SalesOrderHeader_Customer_CustomerID]
FOREIGN KEY([CustomerID])
REFERENCES [Sales].[Customer] ([CustomerID])
Considerations for Constraint Checking
 Assign meaningful names to constraints
 Create, change, and drop constraints without
having to drop and re-create the table
 Perform error checking in your applications
and transactions
 Disable CHECK and FOREIGN KEY
constraints:
 To improve performance when you run large
batch jobs
 To avoid checking existing data when you add
new constraints to a table
Other Data Integrity Methods
 Triggers
 Good for non-normalized data
 Procedural
 No inserts allowed, only use of Stored Procs that
perform the insert/update/delete after checking data
 Can perform modifications in more than one table
 Application Role
 Client application gets hard coded password
 Updates only allowed through that application

More Related Content

Similar to 03Constraints - last.pdf

Sql commands
Sql commandsSql commands
Sql commands
Pooja Dixit
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
Data integrity
Data integrityData integrity
Data integrity
Rahul Gupta
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
Stewart Rogers
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
SakkaravarthiS1
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
Achmad Solichin
 
SQL Query
SQL QuerySQL Query
SQL Query
Imam340267
 
Les09
Les09Les09
Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
Sherif Gad
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
Information Technology
 
Create table
Create tableCreate table
Create table
Nitesh Singh
 
Sql overview-1232931296681161-1
Sql overview-1232931296681161-1Sql overview-1232931296681161-1
Sql overview-1232931296681161-1
sagaroceanic11
 
Les10
Les10Les10
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
Md. Mahedee Hasan
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
TheVerse1
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 
Using ddl statements to create and manage tables
Using ddl statements to create and manage tablesUsing ddl statements to create and manage tables
Using ddl statements to create and manage tables
Syed Zaid Irshad
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 "Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
Andriy Krayniy
 
Sql
SqlSql
Physical Design and Development
Physical Design and DevelopmentPhysical Design and Development
Physical Design and Development
Er. Nawaraj Bhandari
 

Similar to 03Constraints - last.pdf (20)

Sql commands
Sql commandsSql commands
Sql commands
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Data integrity
Data integrityData integrity
Data integrity
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
SQL Query
SQL QuerySQL Query
SQL Query
 
Les09
Les09Les09
Les09
 
Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
Create table
Create tableCreate table
Create table
 
Sql overview-1232931296681161-1
Sql overview-1232931296681161-1Sql overview-1232931296681161-1
Sql overview-1232931296681161-1
 
Les10
Les10Les10
Les10
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Using ddl statements to create and manage tables
Using ddl statements to create and manage tablesUsing ddl statements to create and manage tables
Using ddl statements to create and manage tables
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 "Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
 
Sql
SqlSql
Sql
 
Physical Design and Development
Physical Design and DevelopmentPhysical Design and Development
Physical Design and Development
 

Recently uploaded

Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
Nanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdfNanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdf
eddie19851
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
roli9797
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
dwreak4tg
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
apvysm8
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
mzpolocfi
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
GetInData
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
nyfuhyz
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
manishkhaire30
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
Social Samosa
 
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdfUnleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Enterprise Wired
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
Walaa Eldin Moustafa
 

Recently uploaded (20)

Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
Nanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdfNanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdf
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
 
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdfUnleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
 

03Constraints - last.pdf

  • 2. Constraints Types Domain Integrity (Columns) Entity Integrity (Rows) Referential Integrity (Between Tables or Columns of the Same Table)
  • 3. What Are Constraints? Integrity type Constraint type Description Domain DEFAULT Specifies default value for column CHECK Specifies allowed value for column FOREIGN KEY Specifies column in which values must exist NULL Specifies whether NULL is permitted Entity PRIMARY KEY Identifies each row uniquely UNIQUE Prevents duplication of nonprimary keys Referential FOREIGN KEY Defines columns whose value must match the primary key of this table CHECK Specifies the allowed value for a column based on the contents of another column
  • 4. Constraint Scope CREATE TABLE [dbo].[Customers] ( [CustomerID] [int] IDENTITY (1, 1) NOT NULL PRIMARY KEY , [CustomerEmail] [varchar] (30) NULL , [CustomerState] [varchar] (5) NULL ) ON [PRIMARY] GO ALTER TABLE [dbo].[Products] ADD CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED ( [ProductID], [ProductName] ) ON [PRIMARY] GO • Column Level • Table Level
  • 5. PRIMARY KEY Constraints  PRIMARY KEY constraints identify one or more columns in a table that constitute a primary key  One PRIMARY KEY constraint is allowed per table  Value must be unique across constituent columns  Null values are not allowed in constituent columns
  • 6. PRIMARY KEY Constraints CREATE TABLE [dbo].[DEPARTMENTS]( [DNO] [int] IDENTITY(1,1) PRIMARY KEY, [DNAME] [nvarchar](20) NULL ) ON [PRIMARY] CREATE TABLE [HumanResources].[Department]( [DepartmentID] [smallint] IDENTITY(1,1) NOT NULL, [Name] [dbo].[Name], … CONSTRAINT [PK_Department_DepartmentID] PRIMARY KEY CLUSTERED ([DepartmentID] ASC) WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] )
  • 7. Default Constraints  DEFAULT constraints define a default column value when no value is provided  Only one DEFAULT constraint per column  Only applicable to INSERT statements  Some system supplied functions allowed CREATE TABLE [Production].[Location]( ... [Availability] [decimal](8, 2) NOT NULL CONSTRAINT [DF_Location_Availability] DEFAULT (0.00), [ModifiedDate] [datetime] NOT NULL CONSTRAINT [DF_Location_ModifiedDate] DEFAULT (getdate()) )
  • 8. Check Constraints  CHECK constraints restrict the values that can be entered into a column on INSERT or UPDATE.  Can define multiple CHECK constraints per column.  Can reference columns in the same table.  Cannot contain subqueries. ALTER TABLE [HumanResources].[EmployeeDepartmentHistory] WITH CHECK ADD CONSTRAINT [CK_EmployeeDepartmentHistory_EndDate] CHECK (([EndDate]>=[StartDate] OR [EndDate] IS NULL))
  • 9. Disabling Constraints  Bad Data During Constraint Creation? ALTER TABLE [dbo].[Products] WITH NOCHECK ADD CONSTRAINT [CK_Products_No_Nissan] CHECK ([Productname] <> 'nissan sentra')  Temporarily During Data Load ALTER TABLE Products NOCHECK CONSTRAINT CK_Products_No_Nissan
  • 10. UNIQUE Constraint  UNIQUE constraints ensure that every value in a column is unique  Only one null value allowed in a unique column  Can include one or more columns CREATE TABLE [HumanResources].[Employee]( [EmployeeID] [int] IDENTITY(1,1) NOT NULL, [NationalIDNumber] [nvarchar](15) NOT NULL UNIQUE NONCLUSTERED, … )
  • 11. FOREIGN KEY Constraints  FOREIGN KEY constraints ensure referential integrity between columns in same or different tables  Must reference a PRIMARY KEY or UNIQUE constraint  User must have REFERENCES permission on referenced table ALTER TABLE [Sales].[SalesOrderHeader] WITH CHECK ADD CONSTRAINT [FK_SalesOrderHeader_Customer_CustomerID] FOREIGN KEY([CustomerID]) REFERENCES [Sales].[Customer] ([CustomerID])
  • 12. Considerations for Constraint Checking  Assign meaningful names to constraints  Create, change, and drop constraints without having to drop and re-create the table  Perform error checking in your applications and transactions  Disable CHECK and FOREIGN KEY constraints:  To improve performance when you run large batch jobs  To avoid checking existing data when you add new constraints to a table
  • 13. Other Data Integrity Methods  Triggers  Good for non-normalized data  Procedural  No inserts allowed, only use of Stored Procs that perform the insert/update/delete after checking data  Can perform modifications in more than one table  Application Role  Client application gets hard coded password  Updates only allowed through that application