the relational model our mathematical foundation

Post on 12-Jan-2016

229 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

The Relational Model

Our Mathematical Foundation

Origins

• First proposed by E.F. Codd, 1969-70• subsequently modified and extended

• An abstract theory of data• based on aspects of maths• set theory

• Basis of most modern DBMS• none implement it entirely• we can compare them with the idea

3 Aspects of the Model

• It concerns• 1) data objects

• storing it

• 2) data integrity• making sure it corresponds to reality

• 3) data manipulation• working with it

Relational Data Objects

Storing information

Tables – or is it?

• We say that databases have tables• data are stored in them

• This is a simplification• helps user understanding• may be misleading

Two sets of nomenclatureModel RDBMS (Inituitive)

Relation Table

Tuple Row (record)

Attribute Column

Primary key Column(s) uniquelyidentifying a row

Domain A pool of possible valuesfor an attribute

Domain

• Pool of possible values for an attribute• Each tuple has one of these values for

the attribute• Allows meaningful comparisons• They are data types

• traditionally supported poorly in most systems

• area of recent development

Relation

• Based on a collection of domains• Heading

• a set of attribute:domain pairs, such that each attribute Ai has its own domain Di.

• {<A1:D1>, <A2:D2>, …, <Ai:Di>, … <An:Dn>}

• Body• a set of tuples• each tuple is a set of name:value pairs

• {<A1:V1>, <A2:V2>, …, <Ai:Vi>, … <An:Vn>}

Illustration of notation

UID Tel

nwh 2434

hoh 2436

xxh 7659

As a table

As a relation

Heading {<UID:String(3)>, <Tel:Digit(4)>}

Body {

{<UID:xxh>, <Tel:7659>},

{<Tel:2436>, <UID:hoh>},

{<UID:nwh>, <Tel:2434>}

}

Venn diagram notation

<Ext:Digit(4)>

<UID:String(3)>

{<UID:xxh>, <Tel:7659>}

{<Tel:2436>, <UID:hoh>}

{<UID:nwh>, <Tel:2434>}

Are Relations Tables?

• A table is a practical way to write down a relation

• Relations are defined on sets• a set of attributes in the heading • a set of tuples in the body• sets have no ordering• attributes come in no particular sequence

• but columns do have sequence• tuples come in no particular sequence

• but rows do have sequence

Properties of relations

• Tuples and attributes are unordered• There are no duplicate tuples• All attributes are atomic

Relations/attributes are unordered

• Tables seems to be• Don’t believe it!

• We do not work in terms of:• “next row”• “previous record”• “first tuple”

• We do not rely on:• “next attribute”• “first column”

• Do not try to write for loops

Output can be made to be ordered

No duplicate tuples (= rows)

• i.e..• No two tuples in a relation have all the

same values for corresponding attributes

• Crucial point• Can seem like a weakness

• It is a strength• Learn to exploit it

Example of duplicationName Age Employee ID

Jones 42 3895

Smith 27 6830

Jones 42 3895

Williams 62 4692

• We do not want two records for Jones• The DBMS will prevent this silly

duplication• A simple example of exploiting “no

duplication”

All attribute values are atomic

  

Jackie Chan Acting, Filming, Computing

Bob Dylan Singing, Dancing

Jackie Chan Acting

Jackie Chan Filming

Jackie Chan Computing

Bob Dylan Singing

Bob Dylan Dancing

Atomic Values

• Strings have characters, incl. spaces• not the problem

• Problem: Jim Monday Squash

Wednesday Cinema

Saturday Chess

Mary Wednesday Cinema

Friday Tennis

• Bad solution

Person Day1 Act1 Day2 Act2 Day3 Act3

Jim Mon Squash Wed Cinema Sat Chess

Mary Wed Cinema Fri Tennis

• Which is “day 1”How do we search this?Could get the day and activity separatedThink about what we are modelling

Name Day Activity

Jim Monday Squash

Jim Wednesday Cinema

Jim Saturday Chess

Mary Wednesday Cinema

Mary Friday Tennis

• Solution:

• Looks like adding more lines• Not complicated therefore• Can seem “cosmetic”• Will return to this

Relational Data Objects

End

Relational Data Integrity

making sure the data corresponds to reality

A database as a model

• A DB “models” a real-world enterprise• i.e. the DB must abstract from reality

• the attribute values and their combination must reflect the true state of the world

• We try to enforce plausibility• We do this by implementing integrity

rules (constraints)

Data-specific integrity rules

• Domain specific• Employee age is between 20 and 70• We only sell CDs in multiples of 10• Car registrations must be of the form: A DDD AAA

• where A = alphabetic, D = digit• A temperature cannot be lower than 273.15 deg C

• Inter-attribute (inter-relational)• Only senior managers and sales reps can have

cars over 2000cc

• “Cardinality” constraints• A team has 11 players

General Integrity Rules

• There are two general integrity features Part of the relational model

1. Entity integrity - tuple identification through candidate/primary keys

2. Referential integrity – foreign keys

• There may be application-specific rules• must be identified and implemented• may be able to use DBMS• we will return to this

Candidate Keys (CKs)• A candidate key for relation R is a subset K of

the attributes of R such that• no two tuples of R have the same value of K

the “uniqueness property”• no proper subset of K has the uniqueness property

the “irreducibility property”• All relations have at least 1 (everything)

• may have several

• Uniqueness applies to all possible tuples• not just the current ones

What are candidate keys for?

• Tuple-level addressing• allows unambiguous identification of 1

tuple• content addressing

• The tuple where X has value Y• not unique unless X is a candidate key

• Access mechanisms are more general• e.g. indexes• although CKs may be implemented using

them

The primary key (PK)

• A specified candidate key• The choice of PK is arbitrary

• There may only be one candidate

• Practical factors may help decide• It is common to think only of “the” key

(i.e. the primary key)• but remember there may be other

candidates

Why bother with CKs ?Suppose that we have attributes: uid, ucas, national heath,

surname, first name, d.o.b., city. Here are some data:

Let us declare UCAS to be the primary key. This constrains

UCAS numbers to be distinct for different people, but permits :

UID UCAS NH Surname FirstNameDOB Cityxxh8 987632 234179 Kerr Kevin 26.07.82 Borthjjo1 003872 139873 Jones Jane 13.12,81 Lampeterjja1 013873 243179 Jones Jane 10.06.81 Llanonllo2 003874 264902 Lamb Louis 14.07.82 Borth

UID UCAS NH Surname FirstNameDOB Cityxxh8 987632 243179 Kerr Kevin 26.07.82 Borthjja1 003872 139873 Jones Jane 13.12,81 Lampeterjja1 013873 243179 Jones Jane 10.06.81 Llanonllo2 003874 264902 Lamb Louis 14.07.82 Borth

!

Penalty of not declaring CKs

• A real-world model would not want repetitions of UID and NH for distinct persons.

• We want UID to be unique for each person. Similarly for NH.

• Remedy: declare UID to be a candidate key. Similarly for NH.

• Then, either of UID, NH gives unique identification. This captures two constraints.

• Continue to use UCAS as the PK.• UCAS, UID, NH are 3 candidate keys.

Foreign Keys

• A reference mechanism between relations• The target of a reference must exist in the

referenced relation

no “dangling references”

Example,Consider a table of employees and a table of

car allocations:

Referential Integrity

Name(PK) Post …

Jones M.D.Smith AccountantDavies Porter

Car(PK) Allocated …

R 345 XYZ JonesP 987 CBA DaviesM 567 GHJ Brown

No car- OK

Violates referential integrity

Foreign Keys

Interactive Fiction

Foreign Keys (FKs)- definition

• Linking relation R2 to relation R1:• A foreign key in a relation R2 is a

subset of its attributes such that:• there is a relation R1 with a candidate key

CK• For each value of FK in R2 there must exist

an identical value in the CK in some tuple of R1.

Foreign keys - notes

• All keys are sets of attributes• A candidate key can contain a value not

currently found in the foreign key• Chains of references can build up• Relations can reference themselves

• personnel relation can have a “manager” attribute - managers are personnel

Foreign key - examples

• Earlier example:PERSON{Name(PK), Post}CAR_ALLOC{Car(PK), Allocated*}

FK {Allocated} references PERSON

• Self referential example:a surgeon is supervised by a senior surgeon, called a “consultant”)SURGEON {Surgeon(PK), Consultant*}

FK {Consultant} references SURGEON

Notation

Referential Integrity

• The database must not hold any unmatched foreign keys

• The DBMS should prevent the situation arising - most do today

• The DBMS can:• reject operation which would compromise

integrityor

• make other changes to retain integrity

Maintaining R. Integrity

• Attempt to delete the target of a foreign key

• only allow if there is no matching FK value

or• cascade-delete

tuples with FK matches

• Attempt to update the candidate key

• only allow if there is no matching FK value

or• cascade-update the

FK in the matching tuples

Interactive Fiction

Beware Autogenerated Keys!

Some systems readily offer to generate key values for you – e.g. every time another tuple is entered, a numeric key value is automatically allocated.

This permits:UID UCAS NH Surname FirstNameDOB City AutoKeyxxh8 987632 234179 Kerr Kevin 26.07.82 Borth 1000jjo1 003872 139873 Jones Jane 13.12,81 Lampeter 1001jja1 013873 243179 Jones Jane 10.06.81 Llanon 1002xxh8 987632 234179 Kerr Kevin 26.07.82 Borth 1003

- the same information inserted twice with distinct autokey values!

!

Beware Autogenerated Keys!

• Remedy: make Autokey, UID, UCAS, NH candidate keys and select one as PK.

• Remedy: avoid introducing unnecessary new keys. Do we need an AutoKey in this example?

• Caution: if you do introduce a new key, you still need to identify other candidate keys (or risk bad modelling).

Multiple Multi-attribute keys

• Consider a timetable, with entries for Day, Time, Module, Room, Building, LecturerID

• Assume that a Room label such as A6 can appear in different buildings.

• Some data:

Day Time Module Room Building LecturerMonday 09.00 CI25910 C22 Hugh Owen rrpMonday 09.00 CS23710 A6 Hugh Owen dapFriday 09.00 CS27310 A6 Llandinam hohMonday 17.00 CS25610 C22 Hugh Owen dap

Candidate Keys? There might be several.

Multiple Multi-attribute Keys

• With business rules: e.g.• a module has only one lecturer,

a lecturer lectures in 1 room at a time, only 1 module in a room at a time ...

• 3 Candidate keys in this case: 1. {Day,Time, Room, Building}, – 4 attributes

2. {Day, Time, Lecturer} – 3 attributes

3. {Day, Time, Module}, – 3 attributes

• Choose one to be the PK.

Multi-attribute Foreign Keys

• A multi-attribute PK may be referenced from another relation

• the referencing foreign key then needs to be declared with same structure as that PK (or generally, the CK)

• {Room, Building} above could be declared to be a FK that references a relation with PK {Room, Building} describing facilities:

Room Building BoardType Projection Facilities LightingA6 Hugh Owen None Yes FixedC22 Hugh Owen Fixed Yes DimmingA6 Llandinam Roller Yes Dimming

Data Integrity

End

top related