-4

I'm reading the book "Fundamental of Database Systems" for a Database course and I'd need some directions.

I've tried hard to understand this from the book, but I can't wrap my mind around it, as I think it doesn't make sense from anything in logic I've learnt before. The author presents this query:

{e.Lname, e.Fname | EMPLOYEE(e) AND ((∀x)(NOT(PROJECT(x)) OR NOT (x.Dnum=5) OR ((∃w)(WORKS_ON(w) AND w.Essn=e.Ssn AND x.Pnumber=w.Pno))))}

And states that this equals to -> List the names of employees who work on all the projects controlled by department number 5.

I just can't see how.

In words, this is how I interepret the line query: All e's in the range of the Employee relation that's not part of any project in the range of the Project relation or where any x in the project relation does not equal to 5 ( NOT x.Dnum = 5) or where it exists some w in the WORKS_ON range that matches the ssn of the employee AND matches the Pnumber number of any project x.

What I don't understand here, is how to evaluate this. I'm thinking in programming terms, evalute left to right, if something is TRUE stop and the condition is TRUE. Basically with that logic, if I choose any project that does not have Dnum=5, the condition will be TRUE and we get the result that's not appropiate according to the sentence as we want to find the names of employees who work on all the projects controlled by department number 5. Please, could someone help me make sense of this? It just won't go in and I think it's because of lack of concrete examples.

EDIT: See my answer below and how I interpret this logically.

Caj
  • 55
  • Step-by-step: retrieve eLastname and Firstname of each e that are Employess and such that (Big-Condition). – Mauro ALLEGRANZA Feb 22 '23 at 11:02
  • What is Big-Condition: ((∀x)(NOT(PROJECT(x)) OR NOT (x.Dnum=5) OR ((∃w)(WORKS_ON(w) AND w.Essn=e.Ssn AND x.Pnumber=w.Pno))))? We consider every PROJECT x, because the part NOT(PROJECT(x)) OR Condition means "If PROJECT (x), Then Condition". – Mauro ALLEGRANZA Feb 22 '23 at 11:03
  • Then again: "IF x.Dnum=5, Then... – Mauro ALLEGRANZA Feb 22 '23 at 11:04
  • Thus, we have the "core" of the query: ((∃w)(WORKS_ON(w) AND w.Essn=e.Ssn AND x.Pnumber=w.Pno))) that means: on the PROJECT x there is a "work-element" w such that w.Essn=e.Ssn. – Mauro ALLEGRANZA Feb 22 '23 at 11:06
  • Thanks, but why does it mean "If PROJECT (x) then"? Is it because if there is a project, then it would be turned to false and we must continue evalute the rest? – Caj Feb 22 '23 at 11:08
  • Also, if that's the case, what happens when x.Dnum != 5? Then that would evaluate to true ( NOT (x.Dnum=5)) and we retrieve results we are not interested in? – Caj Feb 22 '23 at 11:11
  • (A) Missing Data : (1) What does PROJECT(x) mean ? (2) What does WORKS_ON(w) mean ? (3) What is the Scope & Domain of $\forall x$ ? (4) What about $\exists w$ ? (5) What are Dnum , Pnumber , Pno , Essn , Ssn (6) What is the DB Structure ? These will help in figuring out the query Might be good to include that in the Post. (B) It might be better on DB Stack Exchange. – Prem Feb 22 '23 at 11:17
  • The formula is universally quantified: thus it returns either True or False. It will be T only if we consider all "x"s of the universe (what) and for each of them we have T. – Mauro ALLEGRANZA Feb 22 '23 at 11:36
  • If x is Not a Project, then T. If it is a Project and its Dnum different from 5 , then T. – Mauro ALLEGRANZA Feb 22 '23 at 11:36
  • Thus, we are left with Projects whose Dnum=5, in which case - for each of them - we have to check if there is... – Mauro ALLEGRANZA Feb 22 '23 at 11:37
  • Alright so let's say Dnum is different from 5, F(x) = TRUE. That means that we will return this particular e tuple that's not part of Dnum? I mean that's the exact opposite of what the statement says. There is something I'm missing here. – Caj Feb 22 '23 at 11:47
  • NO, the query returns the e.Lname and e.Fname of those Employees that work on ALL Project with Dnum=5. For every project with Dnum=5 the formula (∃w)(WORKS_ON(w) AND w.Essn=e.Ssn AND x.Pnumber=w.Pno) must be true. What does it mean that formula? that there is a "work" w in project x such that w.Essn=e.Ssn, i.e. such that the Ssn code of the employee e match the Essn code of the work w. – Mauro ALLEGRANZA Feb 22 '23 at 12:15
  • Hmm this doesn't make sense, I think I interpret ∀x incorrectly or something. This is "mildly" frustrating – Caj Feb 22 '23 at 12:33
  • Correct: you do not understand the meaning of $\forall x$. – Mauro ALLEGRANZA Feb 22 '23 at 15:20
  • "evalute left to right, if something is TRUE stop and the condition is TRUE." Correct, but the condition is NOT "if x is a Project and if Dnum.x=5, then..." But "for every x (f x is a Project and if Dnum.x=5, then...)", in which case if the first x is not a Project of if Dnum.x is not 5 then True and stop. You have to stop after having checked ALL x: better, you have to cycle until you find a False (in which case the output is False) but in order to conclude True you have to cycle on all x and each one must be checked to True. – Mauro ALLEGRANZA Feb 22 '23 at 15:21
  • In Case my earlier comment was too cryptic or got lost # What is e ranging over , that we have to check whether that e is Employee , via EMPLOYEE(e) ? What is $\forall x$ ranging over , Employees , Projects , Departments ? Why should we have to make sure it is not PROJECT(x) ? What is $\exists w$ ranging over , Employees , Projects , Departments ? What is WORKS_ON(w) , who works on what Project in which Department ? With e & w & x having the same range , why w has Essn while e has Ssn , why x has Pnumber while w has Pno ? Surely , it is a DB Question , not a Math Question ! – Prem Feb 22 '23 at 15:31
  • @MauroALLEGRANZA I do finally understand thank you! When you explained it as an iteration, "cycling", you unlocked my brain. I tried it out by hand and it adds up. So that was what I needed to understand, (∀x)(F) simply means that all x within some range (in this case Project) needs to return TRUE for some of the OR conditions inside F. What I think was hard to grasp was that I never thought (∀x) as in the domain of PROJECT, I tried imagining it as infinite. – Caj Feb 22 '23 at 16:43
  • @Prem yep, I directly copied this from the book, I think the lacking of domain specifications threw me off and I'd argue maybe this is bad printing to be honest. Sorry for not adding the tables, but I can't find the definitions for this example in the book. When I google this stuff, the few examples I find often look a bit different compared to the book where it's a bit more clear what domains that are in use. – Caj Feb 22 '23 at 16:47

1 Answers1

0

So finally with some help from the comments, I could understand this fully. I have seen others asked similar questions before and I will leave this here for them to save some time.

To solve this problem, let's do it step-by-step

{e.Lname, e.Fname | EMPLOYEE(e) AND ((∀x)(NOT(PROJECT(x)) OR NOT (x.Dnum=5) OR ((∃w)(WORKS_ON(w) AND w.Essn=e.Ssn AND x.Pnumber=w.Pno))))}

Let's break the above query down into parts:

$F$ = EMPLOYEE(e) AND F1

$F_1$ = (∀x)(NOT(PROJECT(x)) OR NOT (x.Dnum=5) OR ((∃w)(WORKS_ON(w) AND $F_2$)

$F_2$ = (∃w)(WORKS_ON(w) AND w.Essn=e.Ssn AND x.Pnumber=w.Pno)

If you are a programmer like me, imagine a finite set of EMPLOYEE($e$) that you just do a for loop over, one at a time. When we are at the first $e$, we will only return that e IF AND ONLY IF $F_1$ returns TRUE.

When does F1 return TRUE? When ALL projects $x$ in condition F1 returns TRUE, else it will always return FALSE.

Imagine that F1 is a nested For loop, so for $e = 1$, we need to check every tuple in the universe (We can think of this as an infinite set minus our finite set), but fortunately because of how the query is written infinitely many of them (minus the finite ones we have) will turn to TRUE, so we don't even have to consider them at all. Because remember, in order for $∀x$ to return true, ALL $x$'s in the universe needs to satisfy this set. So what will happen, depending on the size of your finite set, is that maybe some project $x$ for some employee $e$ will be false and that will be enough for NOT returning that specific one employee i.e $F_1$ = FALSE.

So we check for every $x$ in the "infinite" set of tuples, where most will return TRUE because it's not part of the range PROJECT or it does not have the department attribute = 5. This leads to the final OR statement in those cases. As I said earlier, the logic here is that when looking at the expression, we can simply just exclude everything that's not in the range PROJECT($x$) and those that does not have the attribute of department = 5, because they are already TRUE.

Then when we get to the final OR statement $F_2$, now we can loop through the whole relation/set of WORKS_ON, but we can stop as soon as we find a $w$ that satisfy the expression. Because of $∃w$, we only need to find one $w$ that satisfy the condition.

To make an example of when this would return FALSE, let's say we have an employee, $e = 1$, and we are evaluating some arbitary project, by index $x = 6$ (of infinitely many). This project is part of department 5 AND is in the range PROJECT, this means that we will have 2 false before reaching $F_2$ for evaluation. Inside $F_2$, let's say we look through every $w$ and we can't find one with the project number $x$.Pnumber, that means that we will also get FALSE for $F_2$. Now we can stop the $F_1$ loop because, One FALSE = FALSE for the whole condition! (Remember that the $∀x$ "loop" will only evaluate to TRUE if all $x$'s are TRUE).

To make an example of when this would return TRUE, well, let's say that all $x$'s we have already evaluated have been TRUE and we are at the last $x$ (project tuple) in the loop $F_1$. If $x$ is in range of Project AND x.Dnum = 5, we have two false and must evaluate the last $F_2$. The project has the attribute Dnum = 5 and if this particular employee is assigned to this project in some $w$ (THERE EXISTS), we will return TRUE here. As this was the LAST $x$, the whole condition $F_1$ will finally return TRUE.

The condition is simply, the employee needs to be assigned to ALL projects when we reach this particular state with two falses first in $F_1$, so that $F_2$ always evaluate to TRUE. So to simplify things, exclude all the stuff we are not interested by the first NOT statements in $F_1$ in and check if every $x$ holds for the last condition $F_2$.

Caj
  • 55
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. – Community Feb 22 '23 at 17:37