Chapter 9 Constraints. Chapter Objectives Explain the purpose of constraints in a table ...
-
Upload
coleen-moore -
Category
Documents
-
view
241 -
download
0
Transcript of Chapter 9 Constraints. Chapter Objectives Explain the purpose of constraints in a table ...
![Page 1: Chapter 9 Constraints. Chapter Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/1.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/2.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/3.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/4.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/5.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/6.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/7.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/8.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/9.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/10.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/11.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/12.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/13.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/14.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/15.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/16.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/17.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/18.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/19.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/20.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/21.jpg)
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,](https://reader036.fdocuments.net/reader036/viewer/2022062301/56649f3d5503460f94c5d170/html5/thumbnails/22.jpg)
ALTER TABLE…DROP SyntaxALTER TABLE…DROP Syntax