问题
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?
回答1:
Relational algebra for:
Find an
Idof theEmployeewho has taken everytraining.
Actually you need division % operator in relational algebra:
r ÷ sis used when we wish to express queries with“all”:Example:
- Which persons have a bank account at
ALLthe banks in the country?- Retrieve the name of employees who work on
ALLthe projects thatJon Smithworks 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 quantificationor theallcondition. 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), whereX ⊆ ZandY = Z - X(and henceZ = X ∪ Y); that isYis the set of attributes ofRthat are not attributes ofSe.g.X = {A}, Z = {A, B} then Y = {B},Battribute is not present in relationS.
T(Y)the result of DIVISION is a relation includes a tupletif tupletRappear in relationRwithtR[Y] = t, and withtR[X] = tSforeverytuple inS. This means that. for a tupletto appear in the resultTof the DIVISION, the value oftmust be appear inRin combination with every tuple inS.
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.
来源:https://stackoverflow.com/questions/18997845/how-to-convert-sql-to-relational-algebra-in-case-of-sql-joins