The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and...
Transcript of The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and...
![Page 1: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/1.jpg)
TheRela(onalDataModeland
Rela(onalAlgebra
Debapriyo Majumdar DBMS – Fall 2016
Indian Statistical Institute Kolkata
![Page 2: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/2.jpg)
TheRela(onalModel§ Codd proposed the relational model in 1970 § Simple and elegant § A relation is a table with rows and columns § A database is a collection of one or more relations § Advantages – Easy to understand – Permits the use of high level language to query the data – Simplicity
2
![Page 3: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/3.jpg)
ARela(on:example
3
A9ributes/Columns/Fields
Tuples/Records/Rows
Fieldnames
![Page 4: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/4.jpg)
A9ributetypes§ The set of allowed values for each attribute is called the
domain of the attribute§ Attribute values are (normally) required to be atomic;
that is, indivisible§ The special value null is a member of every domain.
Indicated that the value is “unknown”§ The null value causes complications in the definition of
many operations
4
![Page 5: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/5.jpg)
Rela(on:Formally§ Let A1, A2, …, An be attributes, taking values in sets D1, D2,
…., Dn respectively § A relation schema R = (A1, A2, …, An ) is a subset of D1 × D2 × … × Dn
§ In other words, a relation r(R) is a set of n-tuples (a1, a2, …, an) where each ai ∈ Di
§ Example: Employee (emp_no, birth_date, first_name, last_name, gender, hire_date )
§ The current values (relation instance) of a relation are specified by a table
§ An element t of r is a tuple, represented by a row in a table § Relations are unordered; order of the tuples does not matter
5
![Page 6: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/6.jpg)
Keys§ Let K ⊆ R § K is a superkey of R if values for K are sufficient to identify a unique tuple
of each possible relation r(R)
– Example: {emp_no} and {emp_no, first_name} are both superkeys of employees
§ Superkey K is a candidate key if K is minimal Example: {emp_no} is a candidate key for employees
§ One of the candidate keys is selected to be the primary key
– Which one? A design question. § Foreign key constraint: Value in one relation must appear in another
– Referencing relation – Referenced relation
6
![Page 7: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/7.jpg)
Rela(onalQueryLanguage§ “Pure” languages:– Relational algebra– Tuple relational calculus– Domain relational calculus
§ The above 3 pure languages are equivalent in computing power
§ Relational Algebra– Not turning-machine equivalent– consists of 6 basic operations
7
![Page 8: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/8.jpg)
Select–Selec(onofrows
8
§ Schema R(A, B, C, D) § Relation r(R)
§ σ(A=B) ∧ (D > 5) (r)
A B C D a a 1 7 a b 5 7 b b 12 3 b b 23 10
A B C D a a 1 7 b b 23 10
![Page 9: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/9.jpg)
Project–Selec(onofcolumns
9
§ Schema R(A, B, C, D) § Relation r(R)
§ ∏A,C (r)
A B C D a a 1 7 a b 5 7 b b 12 3 b b 23 10
A C a 1 a 5 b 12 b 23
![Page 10: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/10.jpg)
Unionoftworela(ons
10
§ Schema R(A, B) § Relation r(R) and s(R) A B
a 1 b 12 b 23
A B a 1 a 5 b 23
r s
r∪ s A B a 1 b 12 b 23 a 5
![Page 11: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/11.jpg)
Setdifferenceoftworela(ons
11
§ Schema R(A, B) § Relation r(R) and s(R) A B
a 1 b 12 b 23
A B a 1 a 5 b 23
r s
r − s A B b 12
![Page 12: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/12.jpg)
Setintersec(onoftworela(ons
12
§ Schema R(A, B) § Relation r(R) and s(R) A B
a 1 b 12 b 23
A B a 1 a 5 b 23
r s
r − s A B a 1 b 23
![Page 13: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/13.jpg)
Joiningtworela(ons:Cartesianproduct
13
§ Schema R(A, B) and S(C, D)
§ Relation r(R) and s(S) A B a 1 b 12 b 23
C D b 10 c 5
r s
r× sA B C D a 1 b 10 b 12 b 10 b 23 b 10 a 1 c 5 b 12 c 5 b 23 c 5
![Page 14: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/14.jpg)
Cartesianproduct:Namingofcolumns
14
§ Schema R(A, B) and S(B, D)
§ Relation r(R) and s(S) A B a 1 b 12 b 23
B D b 10 c 5
r s
r× sA r.B s.B D a 1 b 10 b 12 b 10 b 23 b 10 a 1 c 5 b 12 c 5 b 23 c 5
Denote columns
prefixed by the relation they come
from
![Page 15: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/15.jpg)
Renamingarela(on(table)§ Allows us to refer to a relation r by more than one name ρx(r) returns the expression r under the name x
15
A B b 10 c 5
r A B b 10 c 5
x = ρx(r)
r.A r.B x.A x.B b 10 b 10 c 5 b 10 b 10 c 5 c 5 c 5
r×ρx (r)
![Page 16: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/16.jpg)
Composi(onofopera(ons§ Expressions using multiple operations
16
A B C D a 1 b 10 b 12 b 10 b 23 b 10 a 1 c 5 b 12 c 5 b 23 c 5
r× s
σ A=C (r× s)A B C D b 12 b 10 b 23 b 10
![Page 17: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/17.jpg)
Naturaljoin§ Let r and s be relations on schemas R and S respectively § Then, the natural join of relations R and S is a relation on
schema R ∪ S obtained as follows: – Consider each pair of tuples tr from r and ts from s – If tr and ts have the same value on each of the attributes in
R ∩ S, add a tuple t to the result, where • t has the same value as tr on r • t has the same value as ts on s
17
![Page 18: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/18.jpg)
Naturaljoin:example
18
A B C D a a 1 7 a b 5 7 b b 12 3 b b 23 10
B D E a 7 3 a 5 2 b 10 1
s r
r s A B C D E a a 1 7 3 b b 23 10 1
![Page 19: The Relaonal Data Model and Relaonal Algebradebapriyo/teaching/dbms/...The Relaonal Data Model and Relaonal Algebra Debapriyo Majumdar DBMS – Fall 2016 Indian Statistical Institute](https://reader035.fdocuments.us/reader035/viewer/2022062916/5eb9e09672e324650f376337/html5/thumbnails/19.jpg)
Referencesandacknowledgements § Database Management Systems: book by Raghu
Ramakrishnan and Johannes Gehrke, 3rd Edition – Supporting teaching material at:
§ Database System Concepts: book by Silberschatz, Korth and Sudarshan – Some of the slides are adapted from the slides at:
www.db-book.com
19