The Relational Model
Total Page:16
File Type:pdf, Size:1020Kb
The Relational Model Fabrice Rossi CEREMADE Université Paris Dauphine 2020 Relational model History I invented by Edgar F. Codd in 1969-1970 I based on a mathematical model, the relational algebra I associated relational calculus I still the dominant model Based on relations/tables id first_name last_name gender film_count 567368 Olivia Burnette F 1 758314 Beata Pozniak F 1 636385 Joanne Gordon F 1 588101 Suzanne Cox F 1 683913 Melissa Kurtz F 1 2 Outline Tables and relations Schemas and instances Keys Integrity constraints Using a relational database 3 Outline Tables and relations Schemas and instances Keys Integrity constraints Using a relational database 4 Informal point of view A relational database I is a collection of tables I each table has I a name I columns and rows I a header which gives names to the columns I values in a column are all of the same type (such as integer) I rows that record information/data 5 Example IMDB database Actors id first_name last_name gender film_count I 3 tables 933 Lewis Abernathy M 1 2547 Andrew Adamson M 1 I Actors 2700 William Addy M 1 Movies 2898 Seth (I) Adkins M 1 I 2925 Charles (I) Adler M 1 I Roles facts about entities Movies I id name year rank I actor first name, last name, 192017 Little Mermaid, The 1989 7.30 etc. 300229 Shrek 2001 8.10 306032 Snatch. 2000 7.90 I movie title, year of release, 333856 Titanic 1997 6.90 etc. Roles I relationship between entities actor_id movie_id role an actor played a role in a 933 333856 Lewis Bodine I 2547 300229 Duloc Mascot movie 2700 306032 Tyrone 2898 333856 Slovakian three-year-old boy 2925 192017 Additional Voices 6 Formal point of view Definitions I a domain is a set of values I a relation is a subset of the Cartesian product of n domains Qn i=1 Di n Y R ⊂ Di = f(t1;:::; tn) j t1 2 D1;:::; tn 2 Dng i=1 I each dimension of a relation is an attribute, identified by a unique name (for the relation) I a tuple is an element of a relation I a database is a collection of relations 7 Vocabulary and notations Informal $ formal Notations I if t is a tuple and A an informal formal attribute name, t:A (or t[A]) table relation denotes the value of the column attribute attribute for in the tuple row tuple I if R is a relation and A an type domain attribute name, R:A denotes the attribute with name A in R 8 Examples Domains Relations? I numerical values R age weight height 25 50.00 1.60 I non negative integers N 30 52.30 1.58 I short texts (strings) 35 64.00 1.70 I ad hoc domains such as age weight height dates 30 65.00 1.70 35 95.00 1.88 I arbitrary finite sets such 35 95.00 1.88 Gender = fF; Mg age height weight 30 1.70 65.00 35 1.88 95.00 Cartesian product 40 1.90 87.30 + + N × R × R age weight height |{z} |{z} |{z} -30 65.00 1.70 age weight height -35 95.00 1.88 -40 87.30 1.90 9 Outline Tables and relations Schemas and instances Keys Integrity constraints Using a relational database 10 Schema of a relation Logical schema I specifying the possible values of a relation (i.e. the acceptable subsets) I naming things Definitions A relation schema I specifies the name of the relation, e.g. X I specifies the (unique) names and the domains of its attributes, e.g. A : N, B : R I is denoted Name(Attribute1 : domain1; Attribute2 : domain2;:::), e.g. X(A : N; B : R) A relation is an instance of a relation schema. 11 Example Cartesian product + + N × R × R |{z} |{z} |{z} age weight height Schema Person(age : N; weight : R; height : R) Remark I frequently the domains are obvious or implicit I we can drop them, e.g. Person(age; weight; height) 12 + Actors(id : N ; first_name : string; last_name : string; + gender : fF; Mg ; film_count : N ) + Movies(id : N ; name : string; + + year : N ; rank : R ) + + Roles(actor_id : N ; movie_id : N ; role : string) Examples IMDB database Actors id first_name last_name gender film_count 631024 Kim Genell F 1 620283 Carrie Fisher F 1 567368 Olivia Burnette F 1 623010 Gloria Foster F 1 737979 Pat Nixon F 1 Movies id name year rank 167324 JFK 1991 7.80 207992 Matrix, The 1999 8.50 257264 Planes, Trains & Automobiles 1987 7.20 313459 Star Wars 1977 8.80 Roles actor_id movie_id role 567368 257264 Marti 620283 313459 Princess Leia Organa 623010 207992 Oracle 631024 257264 Receptionist 737979 167324 Herself (with Richard) 13 + Movies(id : N ; name : string; + + year : N ; rank : R ) + + Roles(actor_id : N ; movie_id : N ; role : string) Examples IMDB database Actors id first_name last_name gender film_count 631024 Kim Genell F 1 Actors(id : +; first_name : string; 620283 Carrie Fisher F 1 N 567368 Olivia Burnette F 1 last_name : string; 623010 Gloria Foster F 1 + 737979 Pat Nixon F 1 gender : fF; Mg ; film_count : N ) Movies id name year rank 167324 JFK 1991 7.80 207992 Matrix, The 1999 8.50 257264 Planes, Trains & Automobiles 1987 7.20 313459 Star Wars 1977 8.80 Roles actor_id movie_id role 567368 257264 Marti 620283 313459 Princess Leia Organa 623010 207992 Oracle 631024 257264 Receptionist 737979 167324 Herself (with Richard) 13 + + Roles(actor_id : N ; movie_id : N ; role : string) Examples IMDB database Actors id first_name last_name gender film_count 631024 Kim Genell F 1 Actors(id : +; first_name : string; 620283 Carrie Fisher F 1 N 567368 Olivia Burnette F 1 last_name : string; 623010 Gloria Foster F 1 + 737979 Pat Nixon F 1 gender : fF; Mg ; film_count : N ) Movies id name year rank + 167324 JFK 1991 7.80 Movies(id : N ; name : string; 207992 Matrix, The 1999 8.50 + + 257264 Planes, Trains & Automobiles 1987 7.20 year : N ; rank : R ) 313459 Star Wars 1977 8.80 Roles actor_id movie_id role 567368 257264 Marti 620283 313459 Princess Leia Organa 623010 207992 Oracle 631024 257264 Receptionist 737979 167324 Herself (with Richard) 13 Examples IMDB database Actors id first_name last_name gender film_count 631024 Kim Genell F 1 Actors(id : +; first_name : string; 620283 Carrie Fisher F 1 N 567368 Olivia Burnette F 1 last_name : string; 623010 Gloria Foster F 1 + 737979 Pat Nixon F 1 gender : fF; Mg ; film_count : N ) Movies id name year rank + 167324 JFK 1991 7.80 Movies(id : N ; name : string; 207992 Matrix, The 1999 8.50 + + 257264 Planes, Trains & Automobiles 1987 7.20 year : N ; rank : R ) 313459 Star Wars 1977 8.80 Roles actor_id movie_id role Roles(actor_id : +; movie_id : +; 567368 257264 Marti N N 620283 313459 Princess Leia Organa role : string) 623010 207992 Oracle 631024 257264 Receptionist 737979 167324 Herself (with Richard) 13 Database schema Definitions I a database schema is a collection of relation schemas I relation names must be unique I a instance of a database schema is database whose relations are instances of the relation schemas Conventions I relation schemas in a database schema can share attribute names I to avoid confusion, use common names only for identical domains 14 Outline Tables and relations Schemas and instances Keys Integrity constraints Using a relational database 15 Unicity Relations are sets I no order I each tuple/row is unique Superkeys and keys I a superkey I any subset of the attributes such that no tuples of the relation share exactly the same values for these attributes I default superkey: all the attributes I a key: a minimal superkey (removing attributes removes it superkey status) 16 Example France administrative structure Region Department region_id region region_id department_id department 27 Bourgogne-Franche-Comté 27 25 Doubs 4 La Réunion 84 3 Allier 32 Hauts-de-France 75 40 Landes 11 Île-de-France 44 88 Vosges 93 Provence-Alpes-Côte d’Azur 84 26 Drôme Region(region_id, region) Department(region_id, department_id, department) Superkeys Superkeys I (region_id, region) I (region_id, department_id, department) I (region_id) I (region_id, department_id) I (region_id, department) (region) I I (department_id, department) I (department_id) I (department) 17 Primary keys Primary key I several keys: candidate keys I one of the is the primary key I other are alternative keys Region Department region_id region region_id department_id department 4 La Réunion 93 83 Var 11 Île-de-France 75 33 Gironde 84 Auvergne-Rhône-Alpes 11 91 Essonne 75 Nouvelle-Aquitaine 84 3 Allier 93 Provence-Alpes-Côte d’Azur 44 57 Moselle Primary key Primary key I region_id I department_id I Region(region_id, region) I Department(region_id, department_id, department) 18 Missing values Null value I relaxing the relation definition I an attribute value can be either a value of its domain or the special value NULL I useful for I missing values (not recorded for instance) I non applicable attribute I should not be accepted for all attributes, e.g. primary keys 19 Foreign keys Foreign keys I a set of attributes FK in a relation R1 is a foreign key of R1 if 1. a candidate key in a relation R2 has exactly the same domains as the ones of the attributes in FK 2. values on FK in a tuple in r1 are either NULL or occur in a tuple in r2 I in simple terms: a foreign key links R1 to R2 Example: region_id Department Region region_id department_id department region_id region 1 971 Guadeloupe 84 Auvergne-Rhône-Alpes 24 41 Loir-et-Cher 24 Centre-Val de Loire 75 23 Creuse 44 Grand-Est 53 29 Finistère 28 Normandie 84 42 Loire 4 La Réunion 20 Example IMDB database I Actors(id, first_name, last_name, gender, film_count) I Movies(id, name, year, rank) I Roles(#actor_id,#movie_id,role) I Roles.actor_id is a foreign key to Actors.id I Roles.movie_id is a foreign key to Movies.id I (actor_id, movie_id) is the primary key of Roles 21 Outline Tables and relations Schemas and instances Keys Integrity constraints Using a relational database 22 Principle Legal instances I