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...
TheRela(onalDataModeland
Rela(onalAlgebra
Debapriyo Majumdar DBMS – Fall 2016
Indian Statistical Institute Kolkata
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
ARela(on:example
3
A9ributes/Columns/Fields
Tuples/Records/Rows
Fieldnames
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
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
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
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
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
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
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
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
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
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
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
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)
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
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
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
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