sql server constraints, explaining sql server constraints with different options for constraints , taking the step by step approach to deliver the knowledge to students. making examples clear and concise.
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
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