CSE 260 - Homework- Warshall’s algorithm and Applications Relations, Predicate Logic and Algebra

Warshall’s Algorithm Use Warshall’s algorithm to find the transitive of the : {(1, 2), (1, 3), 1, 4), (2, 3), (2, 4), (3, 4)} Give all the steps.

Relation Applications The following problems involves applications of relations, predicate logic and set algebra to relational database systems. We define two predicates sc and ct as follows: sc(x,y): student with sno=x is taking course with cno=y ct(x,y): course with cno=x is taught by teacher with tno=y sno: student no, cno: course no, tno: teacher no In relational system an example database based on the above definition can be represented by the following two tables. Thus sc table (student-course) has 5 tuples. first tuple indicates that student s1 is taking course c1.

sc ct sno cno cno tno

s1 c1 c1 t1 s2 c2 c2 t2 s3 c2 s4 c3 s4 c5

For each of the following five queries you need to answer the following questions:

QUESTIONS:

1. Write the tables as relations using set notation, i.e., in the form { . . . }.

2. Wrte the SQL queries given in plain English (do not try to translate the SQL com- mands).

3. Write the query results as relations using set notation, i.e., { . . . }.

4. Write the queries in set builder notation.

SQL Queries:

1. Query 1

Select x.sno, x.cno from sc x where x.sno="s2" 2. Query 2

Select * from sc x, ct y 3. Query 3

Select x.sno, x.cno, y.tno from sc x, ct y where x.cno=y.cno

4. Query 4

Select x.sno from sc x where not exists ( select y.sno From sc y where x.sno=y.sno and y.cno="c3")

5. Query 5

Select x.sno from sc x minus Select x.sno from sc x where x.cno=’c3’

We define a course prerequisites relation as follows. p(x,y): Course with cno=x has prerequisite course with cno=y. Following SQL query implements transitive closure of the prerequisite relation. Thus each course has its own prerequisite given in table p but by computing transitive closures of the relation p all the other courses that have to be taken can be found. For example, c1’s prerequisute is c2 from the the table below. But c3 is also a course one has to have to get into c2.

p cno1 cno2 c1 c2 c2 c3 c3 c4

6. Query 6

With recursive Allprequisites(cno1, cno2) as (select cno1, cno2 from p where p.cno1="c1"

UNION ALL select Allprequisites.cno1, p.cno2 from Allprequisites, p where p.cno1=Allprequisites.cno2)