chapter 9 constraints. chapter objectives explain the purpose of constraints in a table ...

Post on 14-Jan-2016

236 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 9Constraints

Chapter 9Constraints

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

Constraint TypesConstraint Types

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

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

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

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

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

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

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

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

FOREIGN KEY Constraint - ExampleFOREIGN KEY Constraint - Example

UNIQUE ConstraintUNIQUE Constraint

No duplicates allowed in referenced column

NULL values are permitted

No duplicates allowed in referenced column

NULL values are permitted

CHECK ConstraintCHECK Constraint

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

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

NOT NULL Constraint ExampleNOT NULL Constraint Example

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

Include in column definitionInclude in column definition

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

Include at end of column listInclude at end of column list

Viewing Constraints – USER_CONSTRAINTSViewing Constraints – USER_CONSTRAINTS

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

Disabling/Enabling ConstraintsDisabling/Enabling Constraints

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

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

ALTER TABLE…DROP SyntaxALTER TABLE…DROP Syntax

top related