Constraints

20
Constraints

description

Constraints. Primary Key Constraint. It is derived from the entity UID or created as surrogate key. Uniqueness is enforced by an index: Index is not defined in Oracle Designer All columns in key are mandatory. It should not be updatable. Every table should have one!. Unique Key Constraint. - PowerPoint PPT Presentation

Transcript of Constraints

Page 1: Constraints

Constraints

Page 2: Constraints

Primary Key Constraint

• It is derived from the entity UID or created as surrogate key.

• Uniqueness is enforced by an index:– Index is not defined in Oracle

Designer

• All columns in key are mandatory.

• It should not be updatable.• Every table should have one!

Page 3: Constraints

Unique Key Constraint

• Derived from secondary entity UIDs– Can include NULL columns– Are optional– Recommended where a surrogate

key is used for PK

Page 4: Constraints

Foreign Key Constraint

• Derived from relationships• Can reference either a primary or

a unique key:– But avoid referencing unique keys!

• Must not be partly NULL• Can reference its own table• Cannot reference tables on other

databases

Page 5: Constraints

RENTAL

RENTAL_ITEM BOOKINGBOOKING

COPYCOPY

COPY

TITLE

Foreign Key RulesCASCADECASCADERESTRICTRESTRICT NULLIFYNULLIFY DEFAULTDEFAULT

DML X

(default)

DML

DML

DML

NULL

DML

DEFAULT

• Server handles RESTRICT and CASCADE Delete only

Page 6: Constraints

Defining Foreign Key Rules

Cascade Rules

Delete Rule

Foreign Key Properties

Update Rule

• Cascades

• Defaults

• Nullifies

• Restricted

• Avoid using any update rule:

– PKs should not be updatable

Page 7: Constraints

Check ConstraintCHECK ((position = 'TECHNICAL' AND

salary BETWEEN 3000 AND 4000) OR

(position != 'TECHNICAL'))

EMPLOYEES

IDENTIFIER

LAST_NAME

FIRST_NAME

POSITION

SALARY

Page 8: Constraints

Foreign Keys in Arcs

C

ID

FK_AFK_B

A

ID

DSD1

CHECK ((FK_A IS NOT NULL AND FK_B IS NULL)

OR (FK_B IS NOT NULL AND FK_A IS NULL))

B

ID

Page 9: Constraints

Implementing SubtypesGAME and MOVIE subtypes in TITLES

TITLES

PRODUCT_CODE

TI_TYPE

TITLEMOVIE_CATEGORY

AGE_RATING

GAME_CATEGORY

MEDIUM

CHECK ((TI_TYPE = 'GA'AND GAME_CATEGORY IS NOT NULLAND MEDIUM IS NOT NULL AND MOVIE_CATEGORY IS NULL AND AGE_RATING IS NULL)OR ...

Page 10: Constraints

More Uses for Check Constraints

• START_DATE < END_DATE• Positive numeric column values

> 0• IS NOT NULL

• Can only check columns within the same row; use triggers for more complex checks

Page 11: Constraints

Defining a Check Constraint

EMP_DEPT_FKEMP_DEPT_FK

Create Check Constraint for EMPLOYEES : Name

Check constraint name

Check constraint condition

Column

• Specify the Where/Validation condition

Page 12: Constraints

Where Are Constraints Validated?

• At the client, the server or both• Considerations:

– Database integrity– Interactive response– Network traffic– Overall system performance

Page 13: Constraints

Specifying Location of Validation

Relational Table Definitions

Foreign Keys

BOO_COP_FK

Where to enforce

Foreign Key Properties

Validation

Validate in

ClientServerBothNone

Page 14: Constraints

Server Validation

Action Edit Block Field

Customers:

ServerServerClientClient

DML Posted

Guaranteed integrity Minimal network traffic

No immediate feedback

Page 15: Constraints

Server and Client Validation

Action Edit Block Field

Customers:

ServerServerClientClient

DML Posted

Guaranteed integrity Immediate feedback

Network traffic for every check

Page 16: Constraints

Client Validation

Action Edit Block Field

Customers:

ServerClient

DML Posted

Immediate feedback

No guaranteed integrity Network traffic for every check and again for DML or commit

Page 17: Constraints

Guidelines for Choosing Where to Validate

• Server validation:– Guaranteed data integrity• Client and server validation:

– Check constraints

– Lookup foreign keys

+

• Case-by-case basis:

– Other foreign keys

– Unique keys

?+

Page 18: Constraints

Controlling When to Enforce

Action Edit Block Field

Customers:

ServerServer

ClientClient

TransactionCommitted

• Typical transactions:Typical transactions:

– Oracle validates each DML statement.Oracle validates each DML statement.

• Complex transactions:Complex transactions:

– Oracle8 server can defer validation Oracle8 server can defer validation until COMMIT.until COMMIT.

Page 19: Constraints

Specifying When Validation Occurs

Relational Table Definitions

Foreign Keys

BOO_COP_FK

When to enforce

Foreign Key Properties

Validation

Defer Status

INITIALLY DEFERREDINITIALLY IMMEDIATENOT DEFERRED

Page 20: Constraints

Summary• Constraints enforce data

integrity:– Entity integrity– Referential integrity– Arcs, subtypes, and simple business

rules

• Validation can be carried out: – Server and client– Deferred