Professional Documents
Culture Documents
Relational Model: Prof P Sreenivasa Kumar Department of CS&E, IITM 1
Relational Model: Prof P Sreenivasa Kumar Department of CS&E, IITM 1
Proposed by Edgar. F. Codd (1923-2003) in the early seventies. [ Turing Award 1981 ] Most of the modern DBMS are relational. Simple and elegant model with a mathematical basis. Led to the development of a theory of data dependencies and database design. Relational algebra operations crucial role in query optimization and execution. Laid the foundation for the development of Tuple relational calculus and then Database standard SQL
Prof P Sreenivasa Kumar Department of CS&E, IITM 1
Relation Scheme
Consists of relation name, and a set of attributes or field names or column names. Each attribute has an associated domain. Example:
student ( studentName rollNumber phoneNumber yearOfAdmission Relation branchOfStudy name
Attribute names
: : : : :
domains
Relation Instance
A finite set of tuples constitute a relation instance. A tuple of relation with scheme R = (A1, A2, , Am) is an ordered sequence of values (v1,v2, ... ,vm) such that vi domain (Ai), 1 i m student
studentName Ravi Teja Rajesh rollNumber CS05B015 CS04B125
branch Of Study CS EC
enrollment
studentName Rajesh Rajesh Suresh rollNumber CS04B125 CS04B125 CS04B130 courseNo CS320 CS370 CS320
sectionNo 2 1 2
Foreign Keys
Tuples in one relation, say r1(R1), often need to refer to tuples in another relation, say r2(R2) to capture relationships between entities Primary Key of R2 : K = {B1, B2, , Bj} A set of attributes F = {A1, A2, , Aj} of R1 such that dom(Ai) = dom(Bi), 1 i j and whose values are used to refer to tuples in r2 is called a foreign key in R1 referring to R2. R1, R2 can be the same scheme also. There can be more than one foreign key in a relation scheme
Department
name
ALGORITHMS A.I D.S.P AERO DYNAMIC
credits
3 4 3 3
deptNo
1 1 2 3
deptId
1 2 3
name
COMPUTER SCIENCE ELECTRICAL ENGG MECHANICAL ENGG
hod
CS01 ES01 ME01
phone
22576235 22576234 22576233
10
11
12
DEPARTMENT
name credits
3 4 3 3 3
deptNo
1 1 2 3 4
deptId
1 2 3
name
COMPUTER SCIENCE ELECTRICAL ENGG. MECHANICAL ENGG.
hod
CS01 ES01 ME01
phone
22576235 22576234 22576233
The new course refers to a non-existent department and thus violates the RIC
13
14
16
Relational Algebra
A set of operators (unary and binary) that take relation instances as arguments and return new relations. Gives a procedural method of specifying a retrieval query. Forms the core component of a relational query engine. SQL queries are internally translated into RA expressions. Provides a framework for query optimization. RA operations: select (), project (), cross product (), union (), intersection (), difference (), join ( )
18
19
Selection Condition Select condition: Basic condition or Composite condition Basic condition: Either Ai <compOp> Aj or Ai <compOp> c Composite condition: Basic conditions combined with logical operators AND, OR and NOT appropriately. Notation: <compOp> : one of < , , > , , = , Ai, Aj : attributes in the scheme R of r c : constant of appropriate data type
Prof P Sreenivasa Kumar Department of CS&E, IITM 20
21
22
name
Mahesh Rajesh Piyush Deepak Lalitha Mahesh
degree
M.S M.S M.E M.E M.E M.S
year
2004 2003 2004 2004 2004 2003
sex
M M M M F M
deptNo advisor
1 1 1 2 3 3 CS01 CS02 CS01 ES01 ME01 ME01
name
Mahesh Rajesh Piyush Deepak Lalitha Mahesh
Note: Mahesh is displayed only once because project operation results in a set.
Prof P Sreenivasa Kumar Department of CS&E, IITM 23
24
Set Operations
r1 - relation with scheme R1 r2 - relation with scheme R2 - union compatible with R1 r1 r2 = {t | t r1 or t r2}; r1 r2 = {t | t r1 and t r2} r1 r2 = {t | t r1 and t r2}; By convention, in all the cases, the scheme of the result is that of the first operand i.e r1.
26
r1 r2
A1 A 2 ... A m
a11 a12 ... a1 m a11 a12 ... a1 m
. .
B1 B 2 ... B n
b1 1 b1 2 ... b1 n b 21 b 2 2 ... b 2 n
. .
bt 1 bt 2 ... btn
a 21 a 2 2 ... a 2 m a 21 a 2 2 ... a 2 m
. .
a 21 a 2 2 ... a 2 m
bt 1 bt 2 ... btn
. . .
r1 r2 : s t tuples
Prof P Sreenivasa Kumar Department of CS&E, IITM 27
28
Join Operation
Cross product : produces all combinations of tuples often only certain combinations are meaningful cross product is usually followed by selection Join : combines tuples from two relations provided they satisfy a specified condition (join condition) equivalent to performing cross product followed by selection a very useful operation Depending on the type of condition we have theta join equi join
Prof P Sreenivasa Kumar Department of CS&E, IITM 29
Theta join
Let r1 - relation with scheme R1 = (A1,A2,,Am) r2 - relation with scheme R2 = (B1,B2,,Bn) and R1 R2 = Notation for join expression : r1 r2 , - join condition is of the form : C1 ^ C2 ^ ^ Cs Ci is of the form : Aj <CompOp> Bk <CompOp> : = , , < , , > , Scheme of the result relation Q = (A1,A2,,Am,B1,B2,,Bn) r = {(a1,a2,,am,b1,b2,,bn) (a1,a2,,am) r1, (b1,b2,,bn) r2 and (a1,a2,,am , b1,b2,,bn) satisfies }
Prof P Sreenivasa Kumar Department of CS&E, IITM 30
Professor
empId
CS01 CS02 ES01 ME01
name
GIRIDHAR KESHAV MURTHY RAJIV GUPTHA TAHIR NAYYAR
sex
M M M M
startYear
1984 1989 1980 1999
deptNo
1 1 2 3
phone
22576345 22576346 22576244 22576243
Courses Department
deptId
1 2 3
cname
Algorithms A.I D.S.P Aero Dynamics
credits
3 4 3 3
deptNo
1 1 2 3
phone
22576235 22576234 22576233
31
Examples
For each department, find its name and the name, sex and phone number of the head of the department. Prof (empId, p-name, sex, deptNo, prof-phone) Result
deptId 1 2 3
32
33
Examples Equi-join
Find courses offered by each department
deptId
1 1 2 3
name
Computer Science Computer Science Electrical Engg. Mechanical Engg.
courseId
CS635 CS636 ES456 ME650
cname
Algorithms A.I D.S.P Aero Dynamics
credits
3 4 3 3
34
Teaching
empId
CS01 CS02 ES01 ME650
courseId
CS635 CS636 ES456 ME01
sem
1 1 2 1
year
2005 2005 2004 2004
classRoom
BSB361 BSB632 ESB650 MSB331
Professor * Teaching
result
empId
CS01 CS02 ES01 ME01
name
Giridhar Keshav Murthy Rajiv Guptha Tahir Nayyar
sex
M M M M
startYear
1984 1989 1989 1999
deptNo
1 1 2 3
phone
22576345 22576346 22576244 22576243
courseId
CS635 CS636 ES456 ME650
sem
1 1 2 1
year
2005 2005 2004 2004
classRoom
BSB361 BSB632 ESB650 MSB331
35
Division operator
The necessary condition to determine r s on instances r(R) and s(S) is S R The relation r s is a relation on schema R S. A tuple t is in r s if and only if 1) t is in R-S (r) 2) For every tuple ts in s, there is tr in r satisfying both a) tr [S] = ts b) tr [R S] = t Another Definition r = r1 r2 Division operator produces a relation R(X) that includes all tuples t[X] in R1(Z) that appear in R1 in combination with every tuple from R2(Y) where Z = X Y
Prof P Sreenivasa Kumar Department of CS&E, IITM 36
a1 b1 a 2 b2 a1 b1 a1 b1 a 2 b2
c1 d1 c1 d1 c2 d 2 c3 d 3 c3 d 3
c1 d 1 c3 d 3
(c2, d2) is not present in the result of division as it does not appear in combination with all the tuples of s in r
Prof P Sreenivasa Kumar Department of CS&E, IITM 37
38
result of step 2
csCourse
courseId
CS635 CS636
courseId
CS635 CS636 CS635 CS636 CS635 ES456 ME650 ME650
39
40
Example Queries
Retrieve the list of female PhD students degree = phD ^ sex = F (student) Obtain the name and rollNo of all female Btech students rollNo, name ( degree = BTech ^ sex = F (student)) Obtain the rollNo of students who never obtained an E grade rollNo ( grade E (enrollment)) is incorrect!! (what if some student gets E in one course and A in another?) rollNo (student) rollNo ( grade = E (enrollment))
Prof P Sreenivasa Kumar Department of CS&E, IITM 41
Schema
42
Schema
Obtain the names, roll numbers of students who have got S grade in the CS370 course offered in 2006 odd semester along with his/her advisor name. reqStudsRollNo rollNo(courseId =CS370& year =2006& semester = odd&grade=S(enrollment)) reqStuds-Name-AdvId ( rollNo, sName, advId) rollNo, name, advisor(reqStudsRollNo * student) result( rollNo, studentName, advisorName) rollNo, sName, name(reqStuds-Name-AdvId advId=empId professor)
43
Schema
Obtain the courses that are either direct or indirect prerequisites of the course CS767. Indirect prerequisite (prerequisite of )+ a prerequisite course Prerequisites at all levels are to be reported levelOnePrereq(cId1) preReqCourse(courseId =CS767(preRequisite)) levelTwoPrereq(cId2) preReqCourse (preRequisite
courseId = cId1 levelOnePrereq))
Similarly, level k prerequisites can be obtained. But, prerequisites at all levels can not be obtained as there is no looping mechanism.
Prof P Sreenivasa Kumar Department of CS&E, IITM 44
46
name
Mahesh Amrish Piyush Deepak Lalitha Mahesh
degree
M.S M.S M.E M.E M.E M.S
year
2004 2003 2004 2004 2004 2003
sex
M M M M F M
deptNo advisor
1 1 1 2 3 3 CS01 null CS01 null ME01 ME01
Professor
empId
CS01 CS02 ES01 ME01
name
GIRIDHAR KESHAV MURTHY RAJIV GUPTHA TAHIR NAYYAR
sex
M M M M
startYear
1984 1989 1980 1999
deptNo
1 1 2 3
phone
22576345 22576346 22576244 22576243
47
professor)
name
Mahesh Amrish Piyush Deepak Lalitha Mahesh
advisor
Giridhar Null Giridhar Null Tahir Nayyer Tahir Nayyer
48
professor)
name
Mahesh Piyush null null Lalitha Mahesh
advisor
Giridhar Giridhar Keshav Murthy Rajiv Guptha Tahir Nayyer Tahir Nayyer
49
professor)
name
Mahesh Piyush Amrish null Deepak null Lalitha Mahesh
advisor
Giridhar Giridhar Null Keshav Murthy Null Rajiv Guptha Tahir Nayyer Tahir Nayyer
50
51
The primary key of Ei will also be the primary key of Ti. The primary key can be referred to by other tables via foreign keys in them to capture relationships as we see later
52
Example
Name CourseID Credits has Section SectionNo Year RoomNo Professor
Course
Section
section
sectionNo courseId year roomNo professor
54
student
rollNo name
mailIds
emailId rollNo
55
Example
HostelName RollNo Name homeAddress 1 resides In 1 Hostel Room RoomNo
STUDENT
Note: Assuming every student resides in hostel. S-STUDENT R-residesIn T-Hostel Room Student
RollNo Name homeAddress RoomId
address
Hostel
RoomNo HostelName address
Foreign key name need not be same as primary key of the other relation
Prof P Sreenivasa Kumar Department of CS&E, IITM 57
58
Example
Name Phone Name ProfID
1 N
RollNo
Professor
guides
Student
Student
RollNo Name guide
Professor
ProfId Name phone
59
E2
T
FK1 FK2
R2
PK2
60
Example
Name Name RollNo CourseID
Student
enrolls
Course
student
name rollNo
enrollment
rollNo courseId
course
name courseID
61
Example
M
is PreReq Of
CourseTable
N CourseID Credits Timing
PreRequisiteTable
preReqCourse CourseID
Credits
Prof P Sreenivasa Kumar Department of CS&E, IITM 62