I am working on SQL and Relational Algebra these days. And I am stuck on the below questions. I am able to make a SQL for the below questions but somehow my Relational Algebra that I have made doesn't looks right.

Below are my tables-

Employee (EmployeeId, EmployeeName, EmployeeCountry)
Training (TrainingCode, TrainingName, TrainingType, TrainingInstructor)
Outcome (EmployeeId, TrainingCode, Grade)

All the keys are specified with star *.

Below is the question and its SQL query as well which works fine-

Find an Id of the Employee who has taken every training.

SQL Qyery:

SELECT X.EmployeeID
FROM   (SELECT EmployeeID, COUNT(*) AS NumClassesTaken 
        FROM OutCome GROUP BY EmployeeID ) 
        AS X
  JOIN (SELECT COUNT(*) AS ClassesAvailable 
        FROM Training) 
        AS Y
  ON X.NumClassesTaken = Y.ClassesAvailable

I am not able to understand what will be the relational algebra for the above query? Can anyone help me with that?

有帮助吗?

解决方案

Relational algebra for:

Find an Id of the Employee who has taken every training.

Actually you need division % operator in relational algebra:

r ÷ s is used when we wish to express queries with “all”:

Example:

  1. Which persons have a bank account at ALL the banks in the country?
  2. Retrieve the name of employees who work on ALL the projects that Jon Smith works on?

Read also this slid for division operator:

You also need query % operator for your query: "Employee who has taken all training".

First list off all Training codes:

Training (TrainingCode, TrainingName, TrainingType, TrainingInstructor)

Primary key is: TrainingCode:

TC = TrainingCode(Training)

A pair of employeeID and trainingCode: a employee take the training.

ET = EmployeeId, TrainingCode(Outcome)

Apply % Division operation which gives you desired employee's codes with trainingCode then apply projection to filter out employee code only.

Result = EmployeeId(ET % TC)

"Fundamentals of Database Systems" is the book I always keep in my hand.

6.3.4 The DIVISION Operation

The DIVISION operation is defined for convenience for dealing with queries that involves universal quantification or the all condition. Most RDBMS implementation with SQL as the primary query language do not directly implement division. SQL has round way of dealing with the type of query using EXISTS, CONTAINS and NOT EXISTS key words.

The general DIVISION operation applied to two relations T(Y) = R(Z) % S(X), where X ⊆ Z and Y = Z - X (and hence Z = X ∪ Y); that is Y is the set of attributes of R that are not attributes of S e.g. X = {A}, Z = {A, B} then Y = {B}, B attribute is not present in relation S.

T(Y) the result of DIVISION is a relation includes a tuple t if tuple tR appear in relation R with tR[Y] = t, and with tR[X] = tS for every tuple in S. This means that. for a tuple t to appear in the result T of the DIVISION, the value of t must be appear in R in combination with every tuple in S.

I would also like to add that the set of relational algebra operations {σ,,,Χ,-} namely Selection, Projection, Join, Cartesian Cross and Minus is a complete set; that is any of the other original relational algebra operation can be expressed as a sequence of operations from this set. Division operation % can also be expressed in the form of , , and - operations as follows:

T1 <-- ∏Y(R)
T2 <-- ∏Y((S Χ T1) - R)
T3 <-- T1 - T2

To represent your question using basic relational algebraic operation just replace R by Outcome, S by Training and attribute set Y by EmployeeId.

I hope this help.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top