CS634 Information Systems Dr Deepak B Phatak Subrao Nilekani Chair Professor Kanwal Rekhi Building, Department of CSE IIT Bombay Session 3, Relational Model
Dr. Deepak B Phatak2 IIT BOMBAY CS634-Session 3 Relational Model Session overview Review of earlier sessions Introduction to relational model Group projects
Dr. Deepak B Phatak3 IIT BOMBAY CS634-Session 3 Relational Model sroll Entity model Student sname shostel sroom scpi scourses shobbies
Dr. Deepak B Phatak4 IIT BOMBAY CS634-Session 3 Relational Model Table representation srollsnameshostel Ajay Shah Nandan Nilekani 08 … … …
Dr. Deepak B Phatak5 IIT BOMBAY CS634-Session 3 Relational Model ccode Course entity Course cname cstudents cfaculty ccredits
Dr. Deepak B Phatak6 IIT BOMBAY CS634-Session 3 Relational Model TABLE REPRESENTATION ccodecnameccredit CS634Modern Info Systems 06 CS101Computer Programming08 … … …
Dr. Deepak B Phatak7 IIT BOMBAY CS634-Session 3 Relational Model Student and course association Student Set Course Set CS634 CS101 ME203 EE102
Dr. Deepak B Phatak8 IIT BOMBAY CS634-Session 3 Relational Model Relationship set Student Registers For Course sroll ccode
Dr. Deepak B Phatak9 IIT BOMBAY CS634-Session 3 Relational Model Relationship set attribute(s) Student Registers For Course sroll ccode Grade
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 Mathematical foundation for database modeling Relational algebra and calculus Structured Query Language (SQL) Structured English-like … (SEQUEL)
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
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)
Dr. Deepak B Phatak13 IIT BOMBAY CS634-Session 3 Relational Model Student relation Student Set Course Set … … … … … … Number of elements = no. of students - Each element is a 5-tuple
Dr. Deepak B Phatak14 IIT BOMBAY CS634-Session 3 Relational Model Student relation as a Table srollsname sh Ajay Shah Nandan Nilekani 08 … … …
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
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
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)
Dr. Deepak B Phatak18 IIT BOMBAY CS634-Session 3 Relational Model Sample relation sroll snamesh Vijay Ambre02 srscpi D15034Parag Samarth Arul Menezes Ajay Shah Rajan Tambe
Dr. Deepak B Phatak19 IIT BOMBAY CS634-Session 3 Relational Model Sample relation srollsnamesh Vijay Ambre 02 sr 01D15034 Shaukat Ali08 scpi Arul Menezes Ajay Shah Rajan Tambe Sanjay Mistry
Dr. Deepak B Phatak20 IIT BOMBAY CS634-Session 3 Relational Model Sample relation srollsnamesh Vijay Ambre 02 sr 01D15034 Shaukat Ali08 scpi Arul Menezes Ajay Shah Rajan Tambe Sanjay Mistry
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
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
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
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 [ arul menezes ] 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
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) ?
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
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?
Dr. Deepak B Phatak28 IIT BOMBAY CS634-Session 3 Relational Model Projection example srollsname sh Vijay Ambre 02 sr 01D15034 Shaukat Ali08 scpi Arul menezes Ajay Shah Rajan Tambe
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
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;
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..
Dr. Deepak B Phatak32 IIT BOMBAY CS634-Session 3 Relational Model Example tables sroll sname Sunita Sarawagi Kesav Nori Muthukrishnan sroll ccode CS CS CS CS HS Student Reg
Dr. Deepak B Phatak33 IIT BOMBAY CS634-Session 3 Relational Model Cross product sroll sname Reg.sroll ccode Sunita Sarawagi CS Sunita Sarawagi CS Sunita Sarawagi CS Sunita Sarawagi CS Sunita Sarawagi HS Kesav Nori CS Kesav Nori CS Kesav Nori CS634 - Student X Reg (First 8 Rows)
Dr. Deepak B Phatak34 IIT BOMBAY CS634-Session 3 Relational Model Cross product sroll sname sroll ccode Sunita Sarawagi CS Sunita Sarawagi CS Sunita Sarawagi CS Sunita Sarawagi CS Sunita Sarawagi HS Kesav Nori CS Kesav Nori CS Kesav Nori CS634 - Student X Reg (First 8 Rows)
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
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)
Dr. Deepak B Phatak37 IIT BOMBAY CS634-Session 3 Relational Model sroll sname sroll ccode Sunita Sarawagi CS Sunita Sarawagi CS Sunita Sarawagi CS Sunita Sarawagi CS Sunita Sarawagi HS Kesav Nori CS Kesav Nori CS Kesav Nori CS634 Cross product Student X Reg (First 8 Rows)
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;
Dr. Deepak B Phatak39 IIT BOMBAY CS634-Session 3 Relational Model Natural join schema sroll sname sroll ccode Sunita Sarawagi CS Sunita Sarawagi IT Kesav Nori IT Kesav Nori CS Muthukrishnan HS412 - Student |X| Reg
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
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;