Bab 6 Relationalcalculus
Bab 6 Relationalcalculus
&
'
Tuple Relational Calculus
A logical language with variables ranging over tuples: { T | Cond } Return all tuples T that satisfy the condition Cond. { T | R(T ) }: returns all tuples T such that T is a tuple in relation R. { T.name | F ACU LT Y (T ) AND T.DeptId = CS }. returns the values of name eld of all faculty tuples with the value CS in their department id eld. The variable T is said to be free since it is not bound by a quantier (for all, exists). The result of this statement is a relation (or a set of tuples) that correspond to all possible ways to satisfy this statement. Find all possible instances of T that make this statement true.
&
'
Tuple Relational Calculus
Quantied Statements
Each variable T ranges over all possible tuples in the universe. Variables can be constrained by quantied statements to tuples in a single relation: Existential Quantier. T R(Cond) will succeed if Cond succeeds for at least one tuple in T . Universal Quantier. T R(Cond) will succeed if Cond succeeds for at all tuples in T . Any variable that is not bound by a quantier is said to be free. A tuple relational calculus expression may contain at most one free variable. The following two expressions are equivalent: { T.name | F ACU LT Y (T ) AND T.DeptId = CS } is the same as: {R | T F ACU LT Y (T.DeptId = CS AND R.name = T.name)}
&
'
Tuple Relational Calculus
Quantied Statements
{ T.name | F ACU LT Y (T ) AND T.DeptId = CS } can be read as: Find all tuples T eld such that T is a tuple in the FACULTY relation and the value of DeptId eld is CS. Return a tuple with a single eld name which is equivalent to the name eld of one such T tuple. {R | T F ACU LT Y (T.DeptId = CS AND R.name = T.name)} can be read as: Find all tuples R such that there exists a tuple T in FACULTY with the DeptId eld value CS, and the value of the name eld of R is equivalent to the name eld of this tuple T . alternative read: Find all tuples R that can be obtained by copying the name eld of SOME a tuple in FACULTY with the value CS in its DeptId attribute.
&
'
Tuple Relational Calculus
&
'
Tuple Relational Calculus
Query Conditions
Any atomic query condition is a query condition. If C1 and C2 are query conditions, then so are C1 AND C2, C1 OR C2, and NOT C1. If C is a query condition, R is a relation name, and T is a tuple variable, then T R(C) and T R(C) are both query conditions. A well formed tuple relational calculus query is an expression of the form: {T | C} where C is a query condition where all the variables except for T are bound to quantied expressions, and T is restricted a nite domain.
&
'
Tuple Relational Calculus
{ T | ST U DEN T (T ) AND F ACU LT Y (T ) } will evaluate to true if T is a tuple in both STUDENT and FACULTY relations. However, this is not possible since the schema of the two relations are dierent. Two tuples can never be identical. Correct way to write this statement: {T | ST U DEN T (T ) AND T 2 F ACU LT Y (T.N ame = T 2.N ame AND T.Address = T 2.Address AND T.P assword = T 2.P assword AND T.Id = T 2.Id)}. What is the result of the following statement? { T.DeptId | ST U DEN T (T ) AND T.DeptId = CS } Since there is no DeptId eld of T , what is the value of T.DeptId? Answer. NULL How about the following statements? T emp1 = { T | T.A = 5 } T emp2 = { T | T.A > 5 }
&
'
Tuple Relational Calculus
Query Conditions
T emp2 = { T | T.A > 5 } is an example of an unbounded expression, the tuple T can be instantiated to innitely many values. This is not allowed. All tuples variables should be restricted to the tuples of a specic relation, even if they are not quantied. If a tuple variable T is bound to a relation R, then it only has values for the attributes in R. All other attribute values are null. A well formed query will have a single unbounded variable. All other variables will have a quantier over them.
&
'
Tuple Relational Calculus
Examples
Find the equivalent statement to this: SELECT DISTINCT F.Name, C.CrsCode FROM FACULTY F, CLASS C WHERE F.Id = C.InstructorId AND C.Year = 2002 {T | F F ACU LT Y (C CLASS (F.Id = C.InstructorId AND C.Y ear = 2002 AND T.N ame = F.N ame AND T.CrsCode = C.CrsCode))} Find the equivalent statement to this: SELECT DISTINCT F.Name FROM FACULTY F WHERE NOT EXISTS (SELECT * FROM CLASS C WHERE F.Id = C.InstructorId AND C.Year = 2002) {F.N ame | F ACU LT Y (F ) AND NOT(C CLASS( F.Id = C.InstructorId AND C.Y ear = 2002))} or carry the NOT inside the paranthesis: {F.N ame | F ACU LT Y (F ) AND (C CLASS( F.Id <> C.InstructorId OR C.Y ear <> 2002))}
&
'
Tuple Relational Calculus
Examples
Find all students who have taken all the courses required by CSCI4380. {S.N ame | ST U DEN T (S) AND R REQU IRES( R.CrsCode <> CSCI4380 OR (T T RAN SCRIP T ( T.StudId = S.StudId AND T.CrsCode = R.P rereqCrsCode AND T.GradeIN ( A , B , C , D ))}. Find all students who have never taken a course from Prof. Acorn. Return the name of the student. {S.N ame | ST U DEN T (S) AND C CLASS (F F ACU LT Y (F.Id = C.InstructorId AND (NOT(F.N amelike %Acorn ) OR NOT(T T RAN SCRIP T (S.Id = T.StudId AND C.CrsCode = T.CrsCode AND C.Y ear = T.Y ear AND C.SectionId = T.SectionId)))))}
&
'
Tuple Relational Calculus
Relational algebra (RA) and tuple relational calculus (TRC) are equivalent in expressive power. In other words, any query written in RA can be translated to an equivalent TRC expression and vice versa. SQL is more powerfull than the previous two languages due to the GROUP BY/HAVING constructs and aggregrate functions. Extensions of RA and TRC have been proposed to overcome this limitation. They are straightforward extensions.
&
10
'
Tuple Relational Calculus
RA vs. TRC
Selection: Algebra: Cond(R) Calculus: {T | R(T ) AND Cond(T )}, i.e. replace attributes A in Cond with T.A to obtain Cond(T ). Projection: Algebra: A1, ... ,Ak (R) Calculus: {T.A1, . . . T.Ak | R(T )} Cartesian Product: Given R(A1, . . . , An) and S(B1, . . . , Bm): Algebra: R S Calculus: {T | T 1 R, T 2 R( T.A1 = T 1.A1 AND . . . AND T.An = T 1.An AND T.B1 = T 2.B1 AND . . . AND T.Bm = T 2.Bm)} Union: Algebra R S Calculus {T | R(T ) AND S(T )} Set Dierence: Algebra R S Calculus: {T | R(T ) AND T 1 S(T 1 <> T )} where T <> T 1 is a shorthand for T.A1 <> T 1.A1 OR . . . OR T.An <> T 1.An.
&
Rensselaer Polytechnic Institute
11
'
Tuple Relational Calculus
Relational Algebra
Write following relational algebra expressions in tuple relational calculus (results of R1 and R2): T := CrsCode,SectionN o,Semester,Y ear,ClassroomId,InstructorId(CLASS) T1 := T [CRS1, SN O1, SEM 1, Y EAR1, CLR1, IN S1] T2 := T1[CRS2, SN O2, SEM 2, Y EAR2, CLR2, IN S2] T3 := T1 T2 T4 := CRS1<>CRS2 OR SN O1<>SN O2(T3) T5 := SEM 1=SEM 2 AND Y EAR1=Y EAR2(T4) R1 := IN S1,SEM 1,Y EAR1(CLR1=CLR2 AND IN S1=IN S2(T5)) R2 := (ID (F ACU LT Y )[IN S1]) IN S1(R1)
&
12
'
Tuple Relational Calculus
Relational Algebra
R1: all professors who taught at least two dierent courses or two dierent sections of the same course in the same classroom in the same semester and the same year. R1 contains both the faculty id, and the semester/year information. R1 = {T 1.InstructorId, T 1.Semester, T 1.Y ear | CLASS(T 1) AND T 2 CLASS(T 1.InstructorId = T 2.InstructorId AND T 1.Semester = T 2.Semester AND T 1.Y ear = T 2.Y ear AND T 1.ClassroomId = T 2.ClassroomId AND (T 1.CrsCode <> T 2.CrsCode OR T 1.SectionN o <> T 2.SectionN o)}.
&
13
'
Tuple Relational Calculus
Relational Algebra
R2: all professors who never taught two dierent courses or two dierent sections of the same course in the same classroom in the same semester and the same year. R2 = {F.Id | F ACU LT Y (F ) AND T 1 CLASS (F.Id <> T 1.InstructorId OR (T 2 CLASS F.Id <>T 2.InstructorId OR T 1.ClassroomId <> T 2.ClassroomId OR T 1.Semester <> T 2.Semester OR T 1.Y ear <> T 2.Y ear OR (T 1.CrsCode = T 2.CrsCode AND T 1.SectionN o = T 2.SectionN o)))}.
&
14