The Relational Model Our Mathematical Foundation.
-
Upload
donald-edgar-fletcher -
Category
Documents
-
view
229 -
download
1
Transcript of The Relational Model Our Mathematical Foundation.
![Page 1: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/1.jpg)
The Relational Model
Our Mathematical Foundation
![Page 2: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/2.jpg)
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
![Page 3: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/3.jpg)
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
![Page 4: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/4.jpg)
Relational Data Objects
Storing information
![Page 5: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/5.jpg)
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
![Page 6: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/6.jpg)
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
![Page 7: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/7.jpg)
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
![Page 8: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/8.jpg)
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>}
![Page 9: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/9.jpg)
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>}
}
![Page 10: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/10.jpg)
Venn diagram notation
<Ext:Digit(4)>
<UID:String(3)>
{<UID:xxh>, <Tel:7659>}
{<Tel:2436>, <UID:hoh>}
{<UID:nwh>, <Tel:2434>}
![Page 11: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/11.jpg)
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
![Page 12: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/12.jpg)
Properties of relations
• Tuples and attributes are unordered• There are no duplicate tuples• All attributes are atomic
![Page 13: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/13.jpg)
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
![Page 14: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/14.jpg)
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
![Page 15: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/15.jpg)
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”
![Page 16: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/16.jpg)
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
![Page 17: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/17.jpg)
Atomic Values
• Strings have characters, incl. spaces• not the problem
• Problem: Jim Monday Squash
Wednesday Cinema
Saturday Chess
Mary Wednesday Cinema
Friday Tennis
![Page 18: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/18.jpg)
• 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
![Page 19: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/19.jpg)
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
![Page 20: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/20.jpg)
Relational Data Objects
End
![Page 21: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/21.jpg)
Relational Data Integrity
making sure the data corresponds to reality
![Page 22: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/22.jpg)
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)
![Page 23: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/23.jpg)
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
![Page 24: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/24.jpg)
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
![Page 25: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/25.jpg)
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
![Page 26: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/26.jpg)
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
![Page 27: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/27.jpg)
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
![Page 28: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/28.jpg)
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
!
![Page 29: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/29.jpg)
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.
![Page 30: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/30.jpg)
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
![Page 31: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/31.jpg)
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
![Page 32: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/32.jpg)
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.
![Page 33: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/33.jpg)
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
![Page 34: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/34.jpg)
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
![Page 35: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/35.jpg)
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
![Page 36: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/36.jpg)
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
![Page 37: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/37.jpg)
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!
!
![Page 38: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/38.jpg)
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).
![Page 39: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/39.jpg)
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.
![Page 40: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/40.jpg)
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.
![Page 41: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/41.jpg)
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
![Page 42: The Relational Model Our Mathematical Foundation.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e9f5503460f94ba23f9/html5/thumbnails/42.jpg)
Data Integrity
End