sql server - constraints

10
Constraints Presentation by Aaron Buma Dev Lead and DBA

Upload: aaron-buma

Post on 20-Jan-2017

888 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: SQL Server - Constraints

Constraints

Presentation by Aaron BumaDev Lead and DBA

Page 2: SQL Server - Constraints

Session Overview

•Constraints

•Data Integrity

•Demos

Page 3: SQL Server - Constraints

Primary Key Constraint

•Most common constraint

•Doesn’t allow NULL or duplicates

•There can only be one per table

Page 4: SQL Server - Constraints

Unique Constraint

•Can be used when a table already has a Primary Key

•Does not allow duplicates, but DOES allow a NULL

Page 5: SQL Server - Constraints

Check Constraint

•Enforces domain integrity, data must conform

•Can refer to a single or multiple columns

•Example: column value must be > 3.

Page 6: SQL Server - Constraints

Default Constraint

•Useful for inserting data into “NOT NULL” columns

•Can be dynamic, example: Using GETDATE()

•Can be overridden with actual values

Page 7: SQL Server - Constraints

Foreign Key Constraint

•A value in the FK’d table exists as a Primary Key of a table, usually a different one

•Verifies constraint on EXISTING data and NEW data– “WITH NOCHECK” only verifies NEW data

Page 8: SQL Server - Constraints

Data Integrity

•Domain Integrity (Constraint on individual value)– Check Constraints

•Entity Integrity (Looks at entire table)– Primary Keys, Unique Constraints

•Referential Integrity (must exist in other table)– Foreign Keys

Page 9: SQL Server - Constraints

DEMOS

Page 10: SQL Server - Constraints

Review

• Constraints

• Data Integrity

Presentation Recording, Slides and Scripts– Available at: http://www.AaronBuma.com