cs634 information systems dr deepak b phatak subrao nilekani chair professor kanwal rekhi building,...
Post on 13-Dec-2015
215 Views
Preview:
TRANSCRIPT
CS634 Information Systems
Dr Deepak B PhatakSubrao Nilekani Chair Professor
Kanwal Rekhi Building, Department of CSEIIT Bombay
Session 3, Relational Model
CS634-Session 3 Relational ModelDr. Deepak B Phatak 2
IIT BOMBAY
Session overview
• Review of earlier sessions• Introduction to relational model• Group projects
CS634-Session 3 Relational ModelDr. Deepak B Phatak 3
IIT BOMBAY
sroll
Entity model
Student
snameshostel
sroomscpi
scourses
shobbies
CS634-Session 3 Relational ModelDr. Deepak B Phatak 4
IIT BOMBAY
Table representation
sroll sname shostel
78011012 Ajay Shah 02
73815034 Nandan Nilekani 08
… … …
CS634-Session 3 Relational ModelDr. Deepak B Phatak 5
IIT BOMBAY
ccode
Course entity
Course
cname
cstudents
cfaculty
ccredits
CS634-Session 3 Relational ModelDr. Deepak B Phatak 6
IIT BOMBAY
TABLE REPRESENTATION
ccode cname ccredit
CS634 Modern Info Systems 06CS101 Computer Programming 08
…
……
CS634-Session 3 Relational ModelDr. Deepak B Phatak 7
IIT BOMBAY
Student and course association
Student Set Course Set
980140129801401599057943990579619910149199112345
CS634
CS101
ME203
EE102
CS634-Session 3 Relational ModelDr. Deepak B Phatak 8
IIT BOMBAY
Relationship set
Student
Registers For
Course
sroll
ccode
CS634-Session 3 Relational ModelDr. Deepak B Phatak 9
IIT BOMBAY
Relationship set attribute(s)
Student
Registers For
Course
sroll
ccode
Grade
CS634-Session 3 Relational ModelDr. Deepak B Phatak 10
IIT BOMBAY
Introduction to relational model
• Developed by professor Codd• Part of system-R work at IBM - 1972
• Mathematical foundation for database modeling
• Relational algebra and calculus• Structured Query Language (SQL)
• Structured English-like … (SEQUEL)
CS634-Session 3 Relational ModelDr. Deepak B Phatak 11
IIT BOMBAY
Relational model
• A relation is a set of n-tuples• Rows in a table
• Each relation has a schema• List of attributes• Similar to column headers in a table
CS634-Session 3 Relational ModelDr. Deepak B Phatak 12
IIT BOMBAY
Sample relation schema
• Student = (sroll, sname, sh, sr, scpi)• Course = (ccode, cname, ccredit)• Reg = (sroll, ccode, grade)
CS634-Session 3 Relational ModelDr. Deepak B Phatak 13
IIT BOMBAY
Student relation
Student Set Course Set
98014012 …98014015 …99057943 …99057961 …99101491 …99112345 …
Number of elements = no. of students
- Each element is a 5-tuple
CS634-Session 3 Relational ModelDr. Deepak B Phatak 14
IIT BOMBAY
Student relation as a Table
sroll sname sh
78011012 Ajay Shah 02
73815034 Nandan Nilekani 08
… … …
CS634-Session 3 Relational ModelDr. Deepak B Phatak 15
IIT BOMBAY
Relational algebra
• A relation is an element• Operators are defined over relation(s) • A relational operation results in a new
relation having• Different set of rows• Different schema (may be)• Or both
CS634-Session 3 Relational ModelDr. Deepak B Phatak 16
IIT BOMBAY
Keys
• Super key: A set of attributes that uniquely characterizes a specific n-tuple
• Candidate key: minimal super key• Primary key: chosen candidate key
CS634-Session 3 Relational ModelDr. Deepak B Phatak 17
IIT BOMBAY
Examples of keys
Student (sroll, sname, sh, sr, scpi)• Superkeys: (unique entity identifier)
Sroll; Sroll + sname;Sh + sr; Sroll + sh + sr; …
• Candidate keys are reduced minimal superkeys
Sroll; sh + sr (single occupancy assumption)
• Primary key: sroll (practical choice)
CS634-Session 3 Relational ModelDr. Deepak B Phatak 18
IIT BOMBAY
Sample relation
sroll sname sh
06011012 Vijay Ambre 02
sr scpi
218 8.49
06D15034 Parag Samarth
08 7.94218
06012015 Arul Menezes 03 146 9.25
06012018 Ajay Shah 03 151 8.38
06012014 Rajan Tambe 08 317 6.85
CS634-Session 3 Relational ModelDr. Deepak B Phatak 19
IIT BOMBAY
Sample relation
sroll sname sh
99011012 Vijay Ambre 02
sr
01D15034 Shaukat Ali 08
scpi
218 8.497.94218
98012015 Arul Menezes 03 146 9.2598012018 Ajay Shah 03 151 8.3898012014 Rajan Tambe 08 317 7.2598012019 Sanjay Mistry 07 169 6.85
CS634-Session 3 Relational ModelDr. Deepak B Phatak 20
IIT BOMBAY
Sample relation
sroll sname sh
99011012 Vijay Ambre 02
sr
01D15034 Shaukat Ali 08
scpi
218 8.497.94218
98012015 Arul Menezes 03 146 9.2598012018 Ajay Shah 03 151 8.3898012014 Rajan Tambe 08 317 7.2598012019 Sanjay Mistry 07 169 6.85
CS634-Session 3 Relational ModelDr. Deepak B Phatak 21
IIT BOMBAY
Relations
• If any value in a table (or a file) changes (add, delete, update), such a change can be made in the same table or file
• However, a relational operation results in a new relation
• Relational algebra operations are used to extract meaningful information from one or more relations
• Can be directly mapped into SQL
CS634-Session 3 Relational ModelDr. Deepak B Phatak 22
IIT BOMBAY
Relational algebra operators
• Selection• Get rows from a relation
• Projection• Get columns from a relation
• Cross product• Combine rows from multiple relations
• Join• Get meaningful information from the
cross product
CS634-Session 3 Relational ModelDr. Deepak B Phatak 23
IIT BOMBAY
Selection
• Extracts n-tuples from a relation R, based on a predicate (condition) P
• R1 = p (R)
• R1: has the same schema as that of R• P: valid condition on R
CS634-Session 3 Relational ModelDr. Deepak B Phatak 24
IIT BOMBAY
Selection
R1 = scpi > 9 AND sh = 3 (student)
The resulting relation will have one row [98012015 arul menezes 03 146 9.25]
SQL query for this relational operation can be written as
Select * From student Where scpi > 9 and sh = 3; ‘*’ means select ALL attributes of the schema
CS634-Session 3 Relational ModelDr. Deepak B Phatak 25
IIT BOMBAY
Projection
• Projects only specified attributes• Given as a list
• R1 = list (R)
• R1 has a new schema• Cardinality (no of rows) ?
CS634-Session 3 Relational ModelDr. Deepak B Phatak 26
IIT BOMBAY
Projection
R1 = cname, ccredit (course)
SQL query can be written as
Select cname, ccreditfrom Course;
cname ccreditInformation Systems 6Data Mining 6Professional Ethics in IT 3
CS634-Session 3 Relational ModelDr. Deepak B Phatak 27
IIT BOMBAY
Projection- another example
R1 = sh (student)
SQL query: Select sh From student;
How many elements would R1 have?
CS634-Session 3 Relational ModelDr. Deepak B Phatak 28
IIT BOMBAY
Projection example
sroll sname sh
99011012 Vijay Ambre 02
sr
01D15034 Shaukat Ali 08
scpi
218 8.497.94218
98012015 Arul menezes 03 146 9.2598012018 Ajay Shah 03 151 8.3898012014 Rajan Tambe 08 317 6.85
CS634-Session 3 Relational ModelDr. Deepak B Phatak 29
IIT BOMBAY
SQL implementation of projection
Select distinct sh From student;Result: Sh 2 8 3
CS634-Session 3 Relational ModelDr. Deepak B Phatak 30
IIT BOMBAY
Expressions
Sname ( scpi >9 & sh = 8 (student))
Select sname From student Where scpi > 9 And sh = 8;
CS634-Session 3 Relational ModelDr. Deepak B Phatak 31
IIT BOMBAY
CROSS PRODUCT
• Operates on two relations• R = R1 x R2• Concatenates each tuple of R1 with
every tuple of R2• If there are M and N elements in the
two relations, the resultant relation has M x N elements
• Schema of R is concatenation of the two schemas of R1 and R2• R1.A1, R1.A2, … , R2.A1, …, etc..
CS634-Session 3 Relational ModelDr. Deepak B Phatak 32
IIT BOMBAY
Example tables
sroll sname - -
89005012 Sunita Sarawagi - -
89007017 Kesav Nori - -
89004039 Muthukrishnan - -
- - - -
sroll ccode -
89005012 CS413 -
89005012 CS634 -
89007017 CS634 -
89007017 CS413 - 89004039 HS412 -
- - -
Student
Reg
CS634-Session 3 Relational ModelDr. Deepak B Phatak 33
IIT BOMBAY
Cross product
sroll sname --- --- Reg.sroll ccode -
89005012 Sunita Sarawagi - - 89005012 CS413 -89005012 Sunita Sarawagi - - 89005012 CS634 -
89005012 Sunita Sarawagi - - 89007017 CS634 -
89005012 Sunita Sarawagi - - 89007017 CS413 -
89005012 Sunita Sarawagi - - 89004039 HS412 -
89007017 Kesav Nori - - 89005012 CS413 -
89007017 Kesav Nori - - 89005012 CS634 -
89007017 Kesav Nori - - 89007017 CS634 -
Student X Reg(First 8 Rows)
CS634-Session 3 Relational ModelDr. Deepak B Phatak 34
IIT BOMBAY
Cross product
sroll sname --- --- sroll ccode -
89005012 Sunita Sarawagi - - 89005012 CS413 -89005012 Sunita Sarawagi - - 89005012 CS634 -
89005012 Sunita Sarawagi - - 89007017 CS634 -
89005012 Sunita Sarawagi - - 89007017 CS413 -
89005012 Sunita Sarawagi - - 89004039 HS412 -
89007017 Kesav Nori - - 89005012 CS413 -
89007017 Kesav Nori - - 89005012 CS634 -
89007017 Kesav Nori - - 89007017 CS634 -
Student X Reg(First 8 Rows)
CS634-Session 3 Relational ModelDr. Deepak B Phatak 35
IIT BOMBAY
Join
• Join is defined to get meaningful set of rows from a cross product
• R = ρ (R1 X R2)
• Selection predicate provides for meaningful information extraction
• Two categories of join• Natural join• Theta join
CS634-Session 3 Relational ModelDr. Deepak B Phatak 36
IIT BOMBAY
Natural join
• Ensures equality of values of all common attributes
• Result schema is union of two schemas
• R = r1 |x| r2
R = student |X| reg
R = student.Sroll =reg.Sroll (student X reg)
CS634-Session 3 Relational ModelDr. Deepak B Phatak 37
IIT BOMBAY
sroll sname --- --- sroll ccode
89005012 Sunita Sarawagi - - 89005012 CS413 89005012 Sunita Sarawagi - - 89005012 CS634
89005012 Sunita Sarawagi - - 89007017 CS634
89005012 Sunita Sarawagi - - 89007017 CS413
89005012 Sunita Sarawagi - - 89004039 HS412
89007017 Kesav Nori - - 89005012 CS413
89007017 Kesav Nori - - 89005012 CS634
89007017 Kesav Nori - - 89007017 CS634
Cross product Student X Reg(First 8 Rows)
CS634-Session 3 Relational ModelDr. Deepak B Phatak 38
IIT BOMBAY
SQL query for natural join
Select * from student natural join reg; Select * from student, reg Where student.Sroll = reg.Sroll;
CS634-Session 3 Relational ModelDr. Deepak B Phatak 39
IIT BOMBAY
Natural join schema
sroll sname --- --- sroll ccode
89005012 Sunita Sarawagi - - 89005012 CS413 -89005012 Sunita Sarawagi - - 89005012 IT640 -
89007017 Kesav Nori - - 89007017 IT640 -
89007017 Kesav Nori - - 89007017 CS413 -
89004039 Muthukrishnan - - 89004039 HS412 -
Student |X| Reg
CS634-Session 3 Relational ModelDr. Deepak B Phatak 40
IIT BOMBAY
Theta join
• R = R1 |x| R2
• Theta is any (meaningful) predicate• Usually has a “join” condition
• R = (R1 x R2)
• R = student |x| reg : student.sroll = reg.sroll and scpi > 9 and sh = 8
CS634-Session 3 Relational ModelDr. Deepak B Phatak 41
IIT BOMBAY
SQL query for join
Select * from student, reg Where Student.Sroll = reg.Sroll and scpi > 9 and sh = 8;
top related