This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Find the names of all customers having a loan at the Perryridge branch
{ t.customer_name | t customer s borrower( t[customer-name] = s[customer-name] u loan(u[branch-name] = “Perryridge” u[loan-number] = s[loan-number])) not v depositor (v[customer-name] = t[customer_name]) }
Find the names of all customers who have a loan at the Perryridge branch, but no account at any branch of the bank
{t.customer_name | t customer s borrower(t[customer-name] = s[customer-name]
u loan(u[branch-name] = “Perryridge” u[loan-number] = s[loan-number]))}
Find the names of all customers who have an account at all branches located in Brooklyn:
{t.customer_name | t customer
not ( b) b branch(b[branch-city] = “Brooklyn” not ( u account ( b[branch-name] = u[branch-name] s depositor ( t[customer-name] = s[customer-name] s[account-number] = u[account-number] )) ) )}
For each output customer, there does not exist a branch in Brooklyn such that
There does not exist an account for that customer in that branch
It is possible to write tuple calculus expressions that generate infinite relations.
For example, {t | t r} results in an infinite relation if the domain of any attribute of relation r is finite
To guard against the problem, we restrict the set of allowable expressions to safe expressions.
An expression {t | P(t)} in the tuple relational calculus is safe if all t values which cause P to be true, are taken from dom (p), where dom (P) is the cartezian product of the domains of all relations appearing in P.
E.g. { t | t[A]=5 true } is not safe --- it defines an infinite set with attribute values that do not appear in any relation or tuples or constants in P.
Retrieve the name and address of all employees who work in the ‘Research’ department.
Q1 : {t.FNAME, t.LNAME, t.ADDRESS | EMPLOYEE(t) and ( d)
(DEPARTMENT (d) and d.DNAME = ‘Research’ and d.DNUMBER=t.DNO) }
QUERY 2
for every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, birthday, and address.
Q2 : {p.PNUMBER, p.DNUM, m.LNAME, m.BDATE, m.ADDRESS | PROJECT(p) and EMPLOYEE (m) and p.PLOCATION=‘Stafford’ and
(( d)(DEPARTMENT(d) and p.DNUM=d.DNUMBER an d.MGRSSN=m.SSN)) }
QUERY 3
find the name of each employee who works on a project controlled by department number 5.
Find the name of employees who work on all the projects controlled by department number 5. One way of specifying this query is by using the universal quantifier as shown.
Q3: {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) ) ) ) }
QUERY 4
Make a list of project numbers for projects that involve an employee whose last name is ‘Smith’, either as a worker or as manager of the controlling department for the project.
Q4 : {p.PNUMBER | PROJECT(p) and
( ( e)( w)(EMPLOYEE(e) and WORKS_ON(w) and
w.PNO=p.PNUMBER and e.LNAME=‘Smith’ and e.SSN=w.ESSN) )
Or( ( m)( d)(EMPLOYEE(m) and DEPARTMENT(d) and
p.DNUM=d.DNUMBER and d.MGRSSN=m.SSN and m.LNAME=‘Smith’) ) ) }
Retrieve the name and address of all employees who work for the ‘Research’ department.
Q1 : {qsv l ( z) ( l) ( m) (EMPLOYEE(qrstuvwxyz) and
DEPARTMENT(lmno) and l=‘Research’ and m=z)} note implicit existenial notation
QUERY 2
For every project located in ‘stafford’, list the project number, the controlling department number, and the department manager’s last name, birthdate and address.
Q2 : {iksuv l ( j) ( m) ( n) ( t)(PROJECT(hijk)and EMPLOYEE(qrstuvwxyz) and DEPARTMENT(lmno) and k=m and n=t and j=‘stanfford’)}
QUERY 6
find the names of employees who have no dependents.
Q6 : {qs l ( t) (EMPLOYEE(qrstuvwxyz) and (( l)(not(DEPENDENT(lmnop)) or not (t=l))))}
Query 6 can be restated using universal quantifiers instead of the existensial quantifiers, as shown in Q6A:
Q6A : {qs l ( t) (EMPLOYEE(qrstuvwxyz) and (( l) (not(DEPENDENT(lmnop))or not (t=l))))}
In some cases, it is not desirable for all users to see the entire logical model (i.e., all the actual relations stored in the database.)
Consider a person who needs to know a customer’s loan number but has no need to see the loan amount. This person should see a relation described, in the relational algebra, by
customer-name, loan-number (borrower loan)
Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view.
attr_name rel_name type positionattr_name Attribute_Cat string 1rel_name Attribute_Cat string 2type Attribute_Cat string 3position Attribute_Cat integer 4sid Students string 1name Students string 2login Students string 3age Students integer 4gpa Students real 5fid Faculty string 1fname Faculty string 2sal Faculty real 3