3-oracle-developer-tables-indexes-essentials-m3-slides
3-oracle-developer-tables-indexes-essentials-m3-slides
David Berry
http://buildingbettersoftware.blogspot.com/
Table Constraints Overview
Table constraints
Define rules your data must follow
Examples
Every student must have a unique student id number
Every course must be associated with a department
Module Outline
Primary Keys
Foreign Keys
Check Constraints
Primary Keys
Combination of naturally
A unique value is generated
occurring columns that form
for each row
a unique key
The real reason I favor surrogate keys….
I am a terrible typist!
Primary Key Definition
departments
department_code name
MA Math
PH Physics
CS Computer Science
courses
department_code course_number Title
MA 101 Calculus 1
PH 102 Physics 2
CS 101 Intro to
Programming
On Delete Cascade
courses
department_code course_number Title
MA 101 Calculus 1
MA 102 Calculus 2
PH 101 Physics 1
PH 102 Physics 2
CS 101 Intro to
Programming
Foreign Key Options Summary
Disabling a constraint
ALTER TABLE courses
DISABLE CONSTRAINT fk_courses_department_code;
Enabling a constraint
ALTER TABLE courses
ENABLE CONSTRAINT fk_courses_department_code;
Constraints and Data Integrity
Consistent
• All applications must comply with
Enforcement of
database constraints
Constraints
Enforcing Constraints in Your Application
Database Constraints
• One set of tools in an overall strategy
• Make use of all tools available
Performance Impacts
• Constraints can be checked in milliseconds
• Bad data take s along time to clean up
Summary
Check
Primary Keys Foreign Keys
Constraints