Top Banner
Chapter 9 Constraints
22

Chapter 9 Constraints. Chapter Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

Jan 14, 2016

Download

Documents

Coleen Moore
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

Chapter 9Constraints

Chapter 9Constraints

Page 2: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

ConstraintsConstraints

Rules used to enforce business rules, practices, and policies

Rules used to ensure accuracy and integrity of data

Rules used to enforce business rules, practices, and policies

Rules used to ensure accuracy and integrity of data

Page 3: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

Constraint TypesConstraint Types

Page 4: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

Creating ConstraintsCreating Constraints

When:– During table creation– Modify existing table

How:– Column level approach– Table level approach

When:– During table creation– Modify existing table

How:– Column level approach– Table level approach

Page 5: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

General Syntax – Column LevelGeneral Syntax – Column Level

If a constraint is being created at the column level, the constraint applies to the column specified

If a constraint is being created at the column level, the constraint applies to the column specified

Page 6: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

General Syntax – Table LevelGeneral Syntax – Table Level

Approach can be used to create any constraint type except NOT NULL

Required if constraint is based on multiple columns

Approach can be used to create any constraint type except NOT NULL

Required if constraint is based on multiple columns

Page 7: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

EnforcementEnforcement

All constraints are enforced at the table level

If a data value violates a constraint, the entire row is rejected

All constraints are enforced at the table level

If a data value violates a constraint, the entire row is rejected

Page 8: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

Adding Constraints to Existing TablesAdding Constraints to Existing Tables

Added to existing table with ALTER TABLE command

Add NOT NULL constraint using MODIFY clause

All other constraints added using ADD clause

Added to existing table with ALTER TABLE command

Add NOT NULL constraint using MODIFY clause

All other constraints added using ADD clause

Page 9: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

PRIMARY KEY ConstraintPRIMARY KEY Constraint

Ensures that columns do not contain duplicate or NULL values

Only one per table allowed

Ensures that columns do not contain duplicate or NULL values

Only one per table allowed

Page 10: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

PRIMARY KEY Constraint for Composite KeyPRIMARY KEY Constraint for Composite Key

List column names within parentheses separated by commasList column names within parentheses separated by commas

Page 11: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

FOREIGN KEY ConstraintFOREIGN KEY Constraint

Requires a value to exist in referenced column of other table

NULL values are allowed Enforces referential integrity Maps to the PRIMARY KEY in parent

table

Requires a value to exist in referenced column of other table

NULL values are allowed Enforces referential integrity Maps to the PRIMARY KEY in parent

table

Page 12: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

FOREIGN KEY Constraint - ExampleFOREIGN KEY Constraint - Example

Page 13: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

UNIQUE ConstraintUNIQUE Constraint

No duplicates allowed in referenced column

NULL values are permitted

No duplicates allowed in referenced column

NULL values are permitted

Page 14: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

CHECK ConstraintCHECK Constraint

Updates and additions must meet specified condition Updates and additions must meet specified condition

Page 15: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

NOT NULL ConstraintNOT NULL Constraint

Special CHECK constraint with IS NOT NULL condition

Can only be created at column level Included in output of DESCRIBE

command Can only be added to existing table

using ALTER TABLE…MODIFY command

Special CHECK constraint with IS NOT NULL condition

Can only be created at column level Included in output of DESCRIBE

command Can only be added to existing table

using ALTER TABLE…MODIFY command

Page 16: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

NOT NULL Constraint ExampleNOT NULL Constraint Example

Page 17: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

Adding Constraints During Table Creation – Column LevelAdding Constraints During Table Creation – Column Level

Include in column definitionInclude in column definition

Page 18: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

Adding Constraints During Table Creation – Table LevelAdding Constraints During Table Creation – Table Level

Include at end of column listInclude at end of column list

Page 19: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

Viewing Constraints – USER_CONSTRAINTSViewing Constraints – USER_CONSTRAINTS

Can display name, type, and condition of CHECK constraintsCan display name, type, and condition of CHECK constraints

Page 20: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

Disabling/Enabling ConstraintsDisabling/Enabling Constraints

Use DISABLE or ENABLE clause of ALTER TABLE commandUse DISABLE or ENABLE clause of ALTER TABLE command

Page 21: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

Dropping a ConstraintDropping a Constraint

Constraints cannot be modified, must be dropped and recreated

Actual syntax depends on type of constraint– PRIMARY KEY - just list type of constraint– UNIQUE - include column name– All others - reference constraint name

Constraints cannot be modified, must be dropped and recreated

Actual syntax depends on type of constraint– PRIMARY KEY - just list type of constraint– UNIQUE - include column name– All others - reference constraint name

Page 22: Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,

ALTER TABLE…DROP SyntaxALTER TABLE…DROP Syntax