Download presentation
Presentation is loading. Please wait.
Published byWilla Miles Modified over 9 years ago
1
CS634 Information Systems Dr Deepak B Phatak Subrao Nilekani Chair Professor Kanwal Rekhi Building, Department of CSE IIT Bombay Session 3, Relational Model
2
Dr. Deepak B Phatak2 IIT BOMBAY CS634-Session 3 Relational Model Session overview Review of earlier sessions Introduction to relational model Group projects
3
Dr. Deepak B Phatak3 IIT BOMBAY CS634-Session 3 Relational Model sroll Entity model Student sname shostel sroom scpi scourses shobbies
4
Dr. Deepak B Phatak4 IIT BOMBAY CS634-Session 3 Relational Model Table representation srollsnameshostel 78011012Ajay Shah02 73815034Nandan Nilekani 08 … … …
5
Dr. Deepak B Phatak5 IIT BOMBAY CS634-Session 3 Relational Model ccode Course entity Course cname cstudents cfaculty ccredits
6
Dr. Deepak B Phatak6 IIT BOMBAY CS634-Session 3 Relational Model TABLE REPRESENTATION ccodecnameccredit CS634Modern Info Systems 06 CS101Computer Programming08 … … …
7
Dr. Deepak B Phatak7 IIT BOMBAY CS634-Session 3 Relational Model Student and course association Student Set Course Set 98014012 98014015 99057943 99057961 99101491 99112345 CS634 CS101 ME203 EE102
8
Dr. Deepak B Phatak8 IIT BOMBAY CS634-Session 3 Relational Model Relationship set Student Registers For Course sroll ccode
9
Dr. Deepak B Phatak9 IIT BOMBAY CS634-Session 3 Relational Model Relationship set attribute(s) Student Registers For Course sroll ccode Grade
10
Dr. Deepak B Phatak10 IIT BOMBAY CS634-Session 3 Relational Model 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)
11
Dr. Deepak B Phatak11 IIT BOMBAY CS634-Session 3 Relational Model 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
12
Dr. Deepak B Phatak12 IIT BOMBAY CS634-Session 3 Relational Model Sample relation schema Student = (sroll, sname, sh, sr, scpi) Course = (ccode, cname, ccredit) Reg = (sroll, ccode, grade)
13
Dr. Deepak B Phatak13 IIT BOMBAY CS634-Session 3 Relational Model Student relation Student Set Course Set 98014012 … 98014015 … 99057943 … 99057961 … 99101491 … 99112345 … Number of elements = no. of students - Each element is a 5-tuple
14
Dr. Deepak B Phatak14 IIT BOMBAY CS634-Session 3 Relational Model Student relation as a Table srollsname sh 78011012Ajay Shah02 73815034Nandan Nilekani 08 … … …
15
Dr. Deepak B Phatak15 IIT BOMBAY CS634-Session 3 Relational Model 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
16
Dr. Deepak B Phatak16 IIT BOMBAY CS634-Session 3 Relational Model Keys Super key: A set of attributes that uniquely characterizes a specific n-tuple Candidate key: minimal super key Primary key: chosen candidate key
17
Dr. Deepak B Phatak17 IIT BOMBAY CS634-Session 3 Relational Model 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)
18
Dr. Deepak B Phatak18 IIT BOMBAY CS634-Session 3 Relational Model Sample relation sroll snamesh 06011012Vijay Ambre02 srscpi 2188.49 06D15034Parag Samarth08 7.94 218 06012015 Arul Menezes 03 146 9.25 06012018 Ajay Shah 03 151 8.38 06012014 Rajan Tambe 08 317 6.85
19
Dr. Deepak B Phatak19 IIT BOMBAY CS634-Session 3 Relational Model Sample relation srollsnamesh 99011012Vijay Ambre 02 sr 01D15034 Shaukat Ali08 scpi 2188.49 7.94218 98012015 Arul Menezes 03 146 9.25 98012018 Ajay Shah 03 151 8.38 98012014 Rajan Tambe 08 317 7.25 98012019 Sanjay Mistry 07 169 6.85
20
Dr. Deepak B Phatak20 IIT BOMBAY CS634-Session 3 Relational Model Sample relation srollsnamesh 99011012Vijay Ambre 02 sr 01D15034 Shaukat Ali08 scpi 2188.49 7.94218 98012015 Arul Menezes 03 146 9.25 98012018 Ajay Shah 03 151 8.38 98012014 Rajan Tambe 08 317 7.25 98012019 Sanjay Mistry 07 169 6.85
21
Dr. Deepak B Phatak21 IIT BOMBAY CS634-Session 3 Relational Model 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
22
Dr. Deepak B Phatak22 IIT BOMBAY CS634-Session 3 Relational Model 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
23
Dr. Deepak B Phatak23 IIT BOMBAY CS634-Session 3 Relational Model 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
24
Dr. Deepak B Phatak24 IIT BOMBAY CS634-Session 3 Relational Model 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
25
Dr. Deepak B Phatak25 IIT BOMBAY CS634-Session 3 Relational Model Projection Projects only specified attributes Given as a list R1 = list (R) R1 has a new schema Cardinality (no of rows) ?
26
Dr. Deepak B Phatak26 IIT BOMBAY CS634-Session 3 Relational Model Projection R1 = cname, ccredit (course) SQL query can be written as Select cname, ccredit from Course; cname ccredit Information Systems 6 Data Mining 6 Professional Ethics in IT 3
27
Dr. Deepak B Phatak27 IIT BOMBAY CS634-Session 3 Relational Model Projection- another example R1 = sh (student) SQL query: Select sh From student; How many elements would R1 have?
28
Dr. Deepak B Phatak28 IIT BOMBAY CS634-Session 3 Relational Model Projection example srollsname sh 99011012Vijay Ambre 02 sr 01D15034 Shaukat Ali08 scpi 2188.49 7.94218 98012015 Arul menezes 03 146 9.25 98012018 Ajay Shah 03 151 8.38 98012014 Rajan Tambe 08 317 6.85
29
Dr. Deepak B Phatak29 IIT BOMBAY CS634-Session 3 Relational Model SQL implementation of projection Select distinct sh From student; Result: Sh 2 8 3
30
Dr. Deepak B Phatak30 IIT BOMBAY CS634-Session 3 Relational Model Expressions Sname ( scpi >9 & sh = 8 (student)) Select sname From student Where scpi > 9 And sh = 8;
31
Dr. Deepak B Phatak31 IIT BOMBAY CS634-Session 3 Relational Model 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..
32
Dr. Deepak B Phatak32 IIT BOMBAY CS634-Session 3 Relational Model 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
33
Dr. Deepak B Phatak33 IIT BOMBAY CS634-Session 3 Relational Model 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)
34
Dr. Deepak B Phatak34 IIT BOMBAY CS634-Session 3 Relational Model 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)
35
Dr. Deepak B Phatak35 IIT BOMBAY CS634-Session 3 Relational Model 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
36
Dr. Deepak B Phatak36 IIT BOMBAY CS634-Session 3 Relational Model 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)
37
Dr. Deepak B Phatak37 IIT BOMBAY CS634-Session 3 Relational Model 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)
38
Dr. Deepak B Phatak38 IIT BOMBAY CS634-Session 3 Relational Model SQL query for natural join Select * from student natural join reg; Select * from student, reg Where student.Sroll = reg.Sroll;
39
Dr. Deepak B Phatak39 IIT BOMBAY CS634-Session 3 Relational Model 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
40
Dr. Deepak B Phatak40 IIT BOMBAY CS634-Session 3 Relational Model 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
41
Dr. Deepak B Phatak41 IIT BOMBAY CS634-Session 3 Relational Model SQL query for join Select * from student, reg Where Student.Sroll = reg.Sroll and scpi > 9 and sh = 8;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.