将SQL转换为关系代数 - 下一步该怎么做?

时间:2016-05-01 07:43:26

标签: sql relational-algebra

我试图将SQL命令转换为关系代数。最大的问题是,有一个" NOT IN"不在关系代数中的SQL语句。

我的查询是这样做的:

从员工中选择员工ID,姓氏和training_id(来自培训),他们接受过本季度培训,但没有完成此培训的所有先决条件 - 因此他们没有完成从业资格培训。

如果结果以X结尾,则表示未成功完成。

TABLES:

employee(employee_id, first_name, last_name)
training(training_id, quartal, year, name)
enrollment(employee_id, training_id, quartal, year, result)
prerequisity(training_id, prerequisity_id)

我的SQL查询:

select e.employee_id, lastname, training_id from employee as e NATURAL JOIN enrollment NATURAL JOIN training NATURAL JOIN prerequisity WHERE quartal = 'first' and year = '2016'  and training_id NOT IN (select e.training_id from enrollment as e NATURAL JOIN employee as ee where e.employee_id=ee.employee_id and result not like '%X' and result is not NULL)

这可能是正确的SQL所以现在我要把它转换成关系代数:

PROJECT[employee_id,lastname, training_id](SELECT[quartal='first',year='2016'](employee@enrollment@training@prerequisity)) # here is the problem

如何模拟" NOT IN" RA中的命令?

1 个答案:

答案 0 :(得分:0)

我建议您使用Set Difference (−)

设置差异操作的结果是元组,它们存在于一个关系中但不在第二个关系中。

PROJECT[employee_id,lastname, training_id](SELECT[quartal='first',year='2016']
    (employee@enrollment@training@prerequisity))  - (PROJECT training_id ....