Presentation is loading. Please wait.

Presentation is loading. Please wait.

A First Course in Database Systems

Similar presentations


Presentation on theme: "A First Course in Database Systems"— Presentation transcript:

1 A First Course in Database Systems
Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 12 A First Course in Database Systems

2 Relational Calculus Tuple Relational Calculus Query: { t | COND (t) }
where t is a tuple variable and COND(t) is a conditional expression involving t. The result of such a query is the set of all tuples t that satisfy COND( t ) .

3 Relational Calculus Relational Calculus Query Example:
Find all employees whose salary is above $50000. { t | EMPLOYEE (t) and t.SALARY > 50000}

4 { t | EMPLOYEE (t) and t.SALARY > 50000}
Relational Calculus Tuple Relational Calculus (TRC) Query Example: Find all employees whose salary is above $50000. { t | EMPLOYEE (t) and t.SALARY > 50000} ==> EMPLOYEE(t) => specifies that the range relation of tuple variable t is EMPLOYEE. Each EMPLOYEE tuple t that satisfies the condition t.SALARY > will be retrieved.

5 Relational Calculus NOTE: t.SALARY references attribute SALARY
of tuple variable t; ==> this notation resembles how attribute names are qualified with relation names or aliases in SQL. t.SALARY  t[SALARY] NOTE: This is TRC!

6 { t | EMPLOYEE (t) and t.SALARY > 50000}
Relational Calculus Find all employees whose salary is above $50000. { t | EMPLOYEE (t) and t.SALARY > 50000} The above query retrieves all attribute values for each selected EMPLOYEE tuple t. To retrieve only some of the attributes - say, the first and last names - we write the following: {t.FNAME, t.LNAME EMPLOYEE(t) and t.SALARY > 50000}

7 Relational Calculus To retrieve only some of the attributes - say, the first and last names - we write the following: {t.FNAME, t.LNAME | EMPLOYEE(t) and t.SALARY > 50000} This is equivalent to the following SQL query: SELECT T.FNAME, T.LNAME FROM EMPLOYEE AS T WHERE T.SALARY > ;

8 {t.FNAME, t.LNAME | EMPLOYEE(t) and t.SALARY > 50000}
Relational Calculus To retrieve only some of the attributes - say, the first and last names - we write the following: {t.FNAME, t.LNAME | EMPLOYEE(t) and t.SALARY > 50000} Informally, we need to specify the following information in a TRC expression: 1. For each tuple variable t , the range relation R of t . This value is specified by a condition of the form R(t).

9 Relational Calculus 2. A condition to select particular combinations of tuples. As tuple variables range over their respective range relations, the condition is evaluated for every possible combination of tuples to identify the selected combinations for which the condition evaluates to TRUE. 3. A set of attributes to be retrieved, the requested attributes. The values of these attributes are retrieved for each selected combination of tuples.

10 Relational Calculus Take a simple SQL query query format:
SELECT item 3 above (attribute list) FROM item 1 above (relation names) WHERE item 2 above (condition)

11 Relational Calculus Query 0
Retrieve the birthdate and address of the employee (or employees) whose name is ‘John B. Smith’. Q0: {t,BDATE, t.ADDRESS | EMPLOYEE(t) and t.FNAME = ‘John’ and t.MINIT = ‘B’ and t.LNAME = ‘Smith’}

12 Relational Calculus Q0: {t,BDATE, t.ADDRESS | EMPLOYEE(t) and t.FNAME = ‘John’ and t.MINIT = ‘B’ and t.LNAME = ‘Smith’} First, in TRC, we specify the requested attributes t.BDATE and t.LNAME for each selected tuple t (on left of bar!!) ( | ). Then we specify the condition for selecting a tuple following the bar (right of bar!!) ( | ) - namely, that t be a tuple of the EMLPLOYEE relation whose FNAME, MINIT, and LNAME attribute values are ‘John’, ‘B’, and ‘Smith’, respectively.

13 Relational Calculus Formal Syntax of TRC:
{t1.A1, t2.A2, …., tn.An | COND (t1,t2, …, tn, tn+1, tn+2, …, tn+m) } where t1,t2, …, tn, tn+1, tn+2, …, tn+m are tuple variables, each Ai is an attribute of the relation on which ti ranges, and COND is a condition or formula (wff in mathematical logic) of the TRC.

14 Relational Calculus In another variant, called the domain relational calculus (DRC), the variables range over field values TRC has had more of an influence on SQL, while DRC has strongly influenced QBE In TRC, a tuple variable is a variable that takes on tuples of a particular relation schema as values i.e., every value assigned to a given tuple variabloe has the same number and type of fields

15 Relational Calculus TRC A tuple relational calculus query has the form
{ T | p(T)} where T is a tuple variable and p(T) denotes a formula that describes T

16 Relational Calculus We will shortly define formulas and queries rigorously The results of this query is the set of all tuples t for which the formula p(T) evaluates to true with T = t . The language for writing formulas p(T) is thus at the heart of TRC and is essentially a simple subset of first-order logic.

17 Relational Calculus Example: Find all sailors with a rating above 7.
{ S | S  Sailors  S.rating > 7 } When this query is evaluated on an instance of the Sailors relation, the tuple variable S is instantiated successfully with each tuple, and the test S.rating >7 is applied.

18 TRC Query: { S | S  Sailors  S.rating > 7 }
Relational Calculus TRC Query: { S | S  Sailors  S.rating > 7 } When this query is evaluated on an instance of the Sailors relation, the tuple variable S is instantiated successfully with each tuple, and the test S.rating >7 is applied. The answer contains those instances of S that pass this test

19 NEXT LECTURE Relational Calculus Continued


Download ppt "A First Course in Database Systems"

Similar presentations


Ads by Google