Lecture 3

The Relational Model
by
Davood Rafiei
University of Alberta

Database Management Systems, Ramakrishnan, Gehrke

1

Relational Databases

 

Basic idea:
• Organize data as a set of tables.
• View each table as a set of rows.

Advantages
• simple
• solid mathematical foundation

set theory

• powerful query languages
• efficient query optimizers

Database Management Systems, Ramakrishnan, Gehrke

2

• e. #Rows = cardinality.. age: integer. Gehrke Example: Students Relation sid 53666 53688 53650 nam e login Jones jones@cs Sm ith sm ith@eecs Sm ith sm ith@m ath age 18 18 19 gp a 3. Students(sid: string. Can think of a relation as a set of rows (or tuples) • What does this imply for each tuple? 3 Database Management Systems.. age : {1. #fields = degree / arity. with rows and columns. plus name and type of each column. Ramakrishnan. Gehrke 4 .g. gpa: real).G.8 ✟✡✠☞☛✍✌✏✎✒✑✔✓✕☛✍✖✔✑✔✗✙✘✛✚✢✜✍✣✤✎✦✥★✧✩✌✏✥✪✥✫✚✢✬✍✣✤☛✭✖✔✖✮✌✰✯✮✱✳✲✴✎✒✑✵✲✶✗✷✑✔✓✹✸✺✗ ✻ Domain: the set of values from which the values of an attribute are drawn.Definition ✄ ☎ Relational database: a set of relations (tables) Relation: made up of 2 parts: ✆ • Instance : a table. • Schema : specifies name of relation. login: string. name : char(20).4 3.2 3.100} Database Management Systems. name: string. Ramakrishnan.. ✞ ✝ E.

Database Management Systems. Ramakrishnan. we can write: ②④③⑥⑤ ⑦✤⑧❖⑨❶⑩ ❷❹❸✺❺★③❻⑦ ⑧❩❺★⑩ ❺★❼✭⑧ ✾❀✿❂❁❃✿❃❄✍❅❇❆ ❈✏❉✪❊✩❋ ●❂❍❏■✕❑▼▲❖◆✤❍◗€❘● ❽✶❽✶❾❖❾ ❿❩➈ ❿❩❾ ❿➇ ➀➌➍❸➁❷⑥⑦➂⑩①⑩✰➎ ② ➃❻⑧❩❸▼❷⑥⑦➂⑩①➎❃⑩①➄❡②➅➄❜➆❀②➆❀② ➏❩➇❩➐ ➈ ❾❩❾❩➉➉➋➐➊ ❙❯❚ ✿ ❉ ✿ ●✭❱❳❲✶❨❩▲★❬❘❭❀❪ ❽✶❾❖❿❩❿❩❿ ➑ ⑨❶③❻➒➔➓ ②➅⑨❶③❻➒➔➓✤➄❡⑩①⑩ ➇❩➈ ❾❩➉ ➏ ❫✰❴✹❵❜❛✵❝ ◆✩❑❡❞❢■✕€❣❍✭◆❤❲❥✐❦▲★€✕❲❀◆✩❑♠❧ ❵ ❨ ❝ ◆✩€❣♥✶♦◗▲❖♣q❧❣❲❀rs▲❜❍✵t✭▲ ❛✵❝ ♦✰€❣❍✭❧ ❝ ◆✤▲❜✉ ❝ ❍❏t❤✈ ✾❀✿❂❁❃✿❃❄✍❅ ●✭❱ ◆✩❲❀✐✇▲①♥❃●✍❱ ❧ ❵ ❨ ❝ ◆ Database Management Systems. Gehrke 5 Example: Querying Relations in SQL ✽ To find all 18 year old students. and the DBMS is responsible for efficient evaluation. • Allows the optimizer to extensively re-order operations. • Queries can be written intuitively. • Precise semantics for relational queries. Ramakrishnan. and still ensure that the answer does not change.Querying Relations ✼ Simple and powerful querying of data. Gehrke 6 .

Gehrke Creating Relations in SQL ➢ ➤ Observe that the type (domain) of each field is specified. Gehrke 8 . Ramakrishnan. and enforced by the DBMS whenever tuples are added or modified.Example: Querying Multiple Relations → ④② ③⑥⑤ ❽✶❾❖❿❩❿❩❿ ❽✶❾ ➈ ❾ ➇ ❽✶❾❖❿❩❿❩❿ ✾❀✿❂❁❃✿❃❄✍❅ ●✍❱ ◆❤❲❀✐✇▲①♥✶➣↔❱ r ❝ ❑ ❈✏❉✪❊✩❋ ●❂❍❏■✕❑▼▲❖◆✤❍◗€❘●✺♥✶➣❘◆✭♦ ❵ ❧❹❧❻▲★❑✴➣ ❙❯❚ ✿ ❉ ✿ ●✭❱❳€ ❝ ❑❘❬★➣✹❱❳€ ❝ ❑➙↕✩➛➝➜❶➣✹❱❳❨❩♦✰❲✶❑★▲▼❬❘➞❀➟✫➠ ⑦✤⑧❖⑨❶⑩ ❷❹❸✺❺★③❻⑦ ⑧❩❺★⑩ ❺★❼✭⑧ sid cid ➀ ❸➁⑦➂⑩✰② ➃❻❸▼⑦➂⑩①②➅➄❜➆❀② ➇❩➈ ❾❩➉➋➊ 53831 Carnatic101 Reggae203 ➌➍❷⑥⑩①➎ ⑧❩❷⑥⑩①➎❃➄❡➆❀② ➏❩➐ ❾❩➉ ➐ 53831 53650 ➑ ⑨❶③❻➒➔➓ ②➅⑨❶③❻➒➔➓✤➄❡⑩①⑩ ➇❩➈ ❾❩➉ ➏ 53666 Topology112 History105 What does the following query compute? Enrolled Students ✉❯▲♠❨❩▲❖❍➡✈ grade C B A B S. ❄ ❉ ✿ ↕ ❃❅ ✿✦➲ ❅ ↕✩➥ ❁❃✿✦✿❃➭❖➴❏➷❥➬➋➬➮➫◗➩ € ❝ ❑➳✈ ❄ ❚ ↕✩❉★➵❻➸➻➺④➼❳♥ r ❝ ❑❘✈ ❄ ❚ ↕✩❉★➵❻➸➻➺➻➼➽➲➨♥ ➱ ❨❩♦✰❲✶❑▼▲★✈ ❄ ❚ ↕✩❉ ➘✵➘ Database Management Systems. Ramakrishnan.name E.cid Smith Topology112 7 Database Management Systems. ❄ ❉ ✿ ↕ ❃❅ ✿✦➲ ❅ ↕✩➥ ❁❃✿✦✾s➦➨➧✶➩❂➫◗➭①➦⑥➯ € ❝ ❑➳✈ ❄ ❚ ↕✩❉★➵❻➸➻➺④➼❳♥ ◆❤❲❀✐✇▲▼✈ ❄ ❚ ↕✩❉★➵❻➸➻➺➻➼➽♥ ❧ ❵ ❨ ❝ ◆✩✈ ❄ ❚ ↕✩❉★➵⑥➾④➺➻➼❻➚ ❲✶❨❩▲▼✈✏➪❻➛ ❅❃✿❃➶➹✿ ❉✺♥ ❨❩♣✕❲✶✈✏❉ ✿ ↕ ❁★➘ As another example. the Enrolled table holds information about courses that students take.

☎ A legal instance of a relation is one that satisfies all specified ICs. Gehrke ✁✂ 9 Integrity Constraints (ICs) ✄ IC: condition that must be true for any instance of the database. Gehrke 10 . • DBMS should not allow illegal instances. too! Database Management Systems. • ICs are checked when relations are modified..Adding and Deleting Tuples ✃ Can insert a single tuple using: ➪⑥➛ ✾s✿ ❉ ❅ ➪❻➛ ➲➽❅❰✶Ï➂❊❐Ð ●❂❍❏■✕❑▼▲❩◆✤❍◗€ ➲ € ❝ ❑▼♥✶◆✩❲❀✐✇▲①♥✶❧ ❵ ❨ ❝ ◆✭♥➂Ï ❲✶❨❩➱ ▲①♥➂❨❃♣✕❲ ➘ ❒q↕ ❁❃❮✩✿❃✾ ❪✶❪✶♥➂ÑÒ●❂✐ ❝ ❍✵tÔÓÒ♥➂Ñ⑥€Õ✐ ❝ ❍❏t✤Ö×▲❖▲★ÓØ♥➂❭❀❪✶♥ ❱ ➘ Ù ✠☞☛✍✓Ú✎✒✥✪✖✔✥✪✗✙✥✫☛✍✖❏✖➹✗ÜÛ✦Ý➝✖❏✥★✲✴✲❩☛✍✗✙✑✔✲✶Þ➡✘✩✑✔✓✹✧ß✲✶✯❤àá✥ ✸✺✯➹✓↔✎✒✑✔✗✷✑✵✯❤✓ãâ✵✥✮äå✧✦äå✣✭✓✹☛✭àá✥✫✚✢æ➳àá✑✔✗✷ç✹è✏é ➜ ✿❂❁❃✿❃❅❃✿ ❈✏❉✪❊✩❋ê●❂❍❏■✕❑▼▲❖◆✤❍◗€ ❙❯❚ ✿ ❉ ✿ ◆❤❲❀✐✇▲✴❬❶ÑØ●✺✐ ❝ ❍❏t❤Ó ë ì❘í❃îÔï❥ð ñ❖ò✪ó➁ô❀õ✶ð➡öÒõ❃÷✪ø➽ù❤í❢ñ❜ø➽ú✶ï➡ù➡ï✒û✔í❃üýü✦õ❃÷❩þ✶ù❤õ✶ð✵ï❤õ❃ô❀õ❃ö⑥óÒõ❃ÿ✏óØï ➂ü✦í❃ð➔ï✒óÒõ❃ø➽ï✏ð ✁  Database Management Systems. Ramakrishnan. • Avoids data entry errors. domain constraints. e. ✆ If the DBMS checks ICs. Ramakrishnan. • ICs are specified when schema is defined.g. stored data is more faithful to real-world meaning.

one of which is chosen as the primary key. • Superkey: 1st condition holds but the 2nd may not. gpa} is a superkey. Gehrke Primary and Candidate Keys in SQL ✟ Possibly many candidate keys (specified using UNIQUE). Ramakrishnan.Primary Key Constraints ✝ A set of fields is a key for a relation if : 1. (What about name?) The set {sid. and 2. This is not true for any subset of the key. • Primary key: If there’s >1 key for a relation. No two distinct tuples can have same values in all key fields. ✞ E. sid is a key for Students. ❄ ❉ ✿ ↕ ❅❃✿✦❅ ↕✩➥ ❁❃✿ ➣❘◆✭♦ ❵ ❧❹❧❻▲★❑❘❭ ➲ € ❝ ❑ ❄ ❚ ↕✩❉ ➲➨➱ ➘ r ❝ ❑ ❄ ❚ ↕✩❉★➵❻➸➻➺➻➲➨➼➽➱ ♥ ❨❩♦✰❲✶❑▼▲ ❄ ❚ ↕✩❉ ➘ ➲ ♥ ✶❉★➪⑥❋✴↕✩❉ ✿ € ❝ ❑▼♥➽r ❝ ❑ ➘✭➘ ❄ ❉ ✿ ↕ ❅❃✿✦❅ ↕✩➥ ❁❃✿ ➣❘◆✭♦ ❵ ❧❹❧❻▲★❑ ➱ t✩❲❀❍❤❲❀♦◗▲❜➱ ❍❏t✤▲❜r ❵ ◆❤€Õ❍✵♦✰❲ ❝ ◆✭❍➡€ ❝ ◆ ➲ € ❝ ❑ ❄ ❚ ↕✩❉ ➲➨➱ ➘ ➣❘◆✭♦ ❵ ❧❹❧❹▲▼❑ €❣▲★❑✴r❃❲❀♦◗▲①❧❹▲★€➻€Õ❧ ➂♥➂❲❀◆ r❃❲❀◆☞♣q♦◗▲ ①▲ ◆✭❍ r ❨❩❝ ♦✰❑ ❲✶❑▼❄ ▲ ❚ ❄ ↕✩❚ ❉★↕✩➵❻➸➻❉ ➺➻➲➨➼➽➱ ♥ ➘ ♥ ❍❏t✤▲♠€❣❍ ❵ ♦✰❲✶❨❩▲ ❵❖❛ ❑➳❲❀❍➡❲ ➳❲✶€Õ▲ ❝ ◆❤€Õ❍➡❲❀◆✭rs▲▼€ ✶❉★➪⑥❋✴↕✩❉ ➲ ✿ ➲ € ❝ ❑ ➘ ♥ ❍❏t❤❲❀❍✩❲❀♦ ❝ €❣▲ ❝ ◆❯♣q♦✰❲❀rs❍ ❝ rs▲ ❮ ➛↔➪ ❮✩✿ r ❝ ❑★♥➂❨❩♦✰❲✶❑▼▲ ➘✤➘ ➞ ❵ ♦✦❲×❨ ❝ ▲①◆ €❣❍❏■✕❑▼▲❖◆✤❍✩❲❀◆❤❑♠r ❵ ■q♦✰€Õ▲ ❝ ◆☞➣❘◆✤♦ ❵ ❧❹❧❹▲★❑❘❭❀♥✶❍✵t✭▲❖♦◗▲ ❝ €✕❲׀ ❝ ◆❤❨❩❧❹▲ ❨❩♦✰❲✶❑★▲▼❱❳➠ ✒✑ ✏ ✔✓ ✡✠ ☛ ✕✗✖ ✙✗✚ ✌☞✎✍ ☞ ✡✠ ✡✘ ✜✛ ✣✢✥✤ ✒✦ ★✧ Database Management Systems.. 11 Database Management Systems. Ramakrishnan. one of the keys is chosen (by DBA) to be the primary key.g. Gehrke ✓ ☛ ✌☞✎✍ ☞ ✜✩ 12 .

Ramakrishnan. sid in Enrolled is a foreign key referring to Students: ✫ Enrolled(sid: string.2 3.8 14 . grade: string) • If all foreign key constraints are enforced. • Can you name a data model w/o referential integrity? 13 Database Management Systems.g.e. Referential Integrity ✪ Foreign key : Set of fields in one relation that is used to `refer’ to a tuple in another relation..4 3. no dangling references.) Like a `logical pointer’. Gehrke Foreign Keys in SQL ✬ Only students listed in the Students relation should be allowed to enroll in courses. (Must correspond to primary key of the second relation. Ramakrishnan. referential integrity is achieved. cid: string. i. • E. ❄ ❉ ✿ ↕ ❅❃✿✦❅ ↕✩➥ ❁❃✿ ➣❘◆✭♦ ❵ ❧❹❧❻▲★❑ ➲ € ❝ ❑ ❄ ❚ ↕✩❉ ➲➨➱ ➘ ♥×r ❝ ❑ ❄ ❚ ↕✩❉★➵❻➸➻➺④➼❳♥➙❨❖♦✰❲✶❑▼▲ ❄ ❚ ↕✩❉ ➲➽➱ ➘ ♥ ✶❉★➪⑥❋✴↕✩❉ ✿ ➲ ➲ € ❝ ❑▼♥➽r ❝ ❑ ➘ ♥ ❈✏❊Ô❉ ✿ ➪ ➶ ➛ ✿ € ❝ ❑ ➘ ❉ ✿ ❈ ✿ ❉ ✿ ➛ ❄✍✿❃✾ ●❂❍❏■✕❑▼▲❖◆✤❍◗€ ➘ ➣❘◆✤♦ ❵ ❧❹❧❹▲★❑ ●❂❍❏■✕❑▼▲①◆✭❍◗€ ✡✠ ☛ ✌☞✎✍ ✭✍ sid 53666 53666 53650 53666 ☞ ☞ cid grade Carnatic101 C Reggae203 B Topology112 A History105 B Database Management Systems.Foreign Keys. Gehrke sid 53666 53688 53650 nam e login Jones jones@cs Sm ith sm ith@eecs Sm ith sm ith@m ath age 18 18 19 gpa 3.

Ramakrishnan. (In SQL. Gehrke ❄ ❉ ➲ ✿ ↕ ❅❃✿✦❅ ↕✩➥ ➲➨❁❃➱ ✿ ➣❘◆✭♦ ❵ ❧❹❧❻▲★❑ € ❝ ❑ ❄ ❚ ↕✩❉ ➘ ♥ r ❝ ❑ ❄ ❚ ↕✩❉★➵❻➸➻➺➻➼➽➲➨♥ ➱ ❨❩♦✰❲✶❑▼▲ ❄ ❚ ↕✩❉ ➘ ➲ ♥ ✶❉★➪⑥❋✴↕✩❉ ✿ ➲ € ❝ ❑▼♥➽r ❝ ❑ ➘ ♥ ❈✏❊Ô❉ ✿ ➪ ➶ ➛ ✿ € ❝ ❑ ➘ ❉ ✿ ❈ ✿ ❉ ✿ ➛ ❄✍✿❃✾ ●✺❍❏■✕❑★▲①◆✍❍◗€ ❊✩➛ ➜ ✿❂❁❃✿❃❅❂✿❡❄ ↕ ✾❀❄ ↕✩➜ ✿ ❊✩➛ ❮ ✶➜➹↕ ❅❂✿✦✾s✿❃❅ ➛ ❮✩❁❃❁➍➘ ☛ ✡✠ ✌☞✎✍ ☞ ✭✍ ☞ ☛ 16 .also: Set sid in Enrolled tuples that refer to it to a special value null. Gehrke ✲ Referential Integrity in SQL/92 SQL/92 supports all 4 options on deletes and updates. What should be done if an Enrolled tuple with a non-existent student id is inserted? ✯ • Reject it! ✰ What should be done if a Students tuple is deleted? ✱ • Also delete all Enrolled tuples that refer to it. • Set sid in Enrolled tuples that refer to it to a default sid.Enforcing Referential Integrity ✮ Consider Students and Enrolled. denoting `unknown’ or `inapplicable’. sid in Enrolled is a foreign key that references Students. 15 Database Management Systems. • Default is NO ACTION (delete/update is rejected) • CASCADE (also delete all tuples that refer to deleted tuple) • SET NULL / SET DEFAULT (sets foreign key value of referencing tuple) Database Management Systems.) Similar if primary key of Students tuple is updated. • Disallow deletion of a Students tuple that is referred to. Ramakrishnan.

but the assertion that sid is a key is given to us. but we store a definition. 17 Database Management Systems. • From example. Ramakrishnan. ❄ ❉ ✿ ↕ ❅❃✿ q❒ ➪ ✿ ❙ ❵ ■q◆❤❨✶➟ýrs❍ ❝ ▲①●✺❍✵■✕❑★▲①◆✭❍➡€ ➲ ◆✩❲s✐✇▲ ➘ ↕ ✾ ✾s✿❃❁❃✿❃❄✍❅ ●✭❱ ◆✩❲❀✐❦▲ ❈✏❉✪❊✩❋ ●❂❍❏■✕❑▼▲❖◆✤❍◗€➳●❂♥✶➣❘◆✭♦ ❵ ❧❹❧❹▲▼❑✴➣ ➱ ❙❯❚ ✿ ❉ ✿ ●✭❱❳€ ❝ ❑❶❬✴➣✹❱❳€ ❝ ❑ ❲❥◆❤❑✴●✍❱❳❲✶❨❩▲ ❭ ✑✔✥✪✱✳✲ ✸❂☛✭✓ ✦✥✫✎✒✌✏✯❤Ý Ý➝✥✪✎✢Û✒✲✶✑✔✓✹✧✛✗✷ç✹✥ ➜❤❉★❊ ❡❒q➪ ✿ ❙ r ❵ ✐ ✐✛❲❀◆✩❑❘❱ ✷✶ ✜✓ ★✸ ✹✻✺ ✭✼ Database Management Systems. more general ICs supported too. we know name is not a key. ✴ Key and foreign key ICs are the most common. Gehrke ✽☛ 18 . rather than a set of tuples. • An IC is a statement about all possible instances! • We cannot infer that an IC is true by looking at an instance. Ramakrishnan.Where do ICs Come From? ✳ ICs are based upon the semantics of the realworld enterprise that is being described in the database relations. Gehrke Views ✵ A view is just a relation.

while hiding details in underlying relation(s). we always have domain constraints. ❂ Powerful and natural query languages exist. based on application semantics. ❀ ❁ • Two important ICs: primary and foreign keys • In addition. but not the cid’s of or grade’s in the courses they are enrolled in. Database Management Systems. we can find young students who are enrolled. but not Students or Enrolled. Integrity constraints can be specified by the DBA. currently the most widely used. Database Management Systems. Ramakrishnan. • Given YoungActiveStudents. Gehrke 20 .Views and Security ✾ Views can be used to present necessary information (or a summary). DBMS checks for violations. Gehrke 19 Relational Model: Summary ✿ A tabular representation of data. Simple and intuitive. Ramakrishnan.

Versant. network model ❅ Recent competitor: object-oriented model • ObjectStore. O2. Ontos • A synthesis emerging: object-relational model ❆ Informix Universal Server. ❄ Old competitors: • hierarchical model. Oracle.Relational Model: Summary ❃ Most widely used model. Informix. Ramakrishnan. Oracle. Gehrke 21 . Microsoft. DB2 Database Management Systems. etc. Sybase. • Vendors: IBM.

Sign up to vote on this title
UsefulNot useful