我试图在MySQL中实现关系代数的等价性。MySQL部门等效不起作用
create table tham_gia(
MaNV int unsigned not null ,
MaDA int unsigned not null ,
So_Gio int unsigned not null default 0,
primary key (MaNV, MaDA)
);
现在我想找到这MaNV
在所有的MaDA
可放在桌子上。这需要一个部门,这是不支持的,所以我打算在关系代数用5个基本操作使用它的等价,所列出的位置: https://en.wikipedia.org/wiki/Relational_algebra#Division_.28.C3.B7.29 司(÷)
划分为二元运算那写作
R ÷ S
。结果由元组在R
以独特的R
的 属性名称,即限制,在R
头但不是在 的S
头,为它认为在S
与 元组所有的组合均存在在R
。
举一个例子看表已完成,DBProject和他们的分裂:
Completed Student Task
Fred Database1
Fred Database2
Fred Compiler1
Eugene Database1
Eugene Compiler1
Sarah Database1
Sarah Database2
DBProject Task
Database1
Database2
Completed ÷ DBProject Student
Fred
Sarah
If DBProject contains all the tasks of the Database project, then the result of the division above contains exactly the students who have completed both of the tasks in the Database project.
More formally the semantics of the division is defined as follows:
R ÷ S = { t[a1,...,an] : t \in R \wedge \foralls \in S ((t[a1,...,an] \cup s) \in R) }
where {a1,...,an} is the set of attribute names unique to R and t[a1,...,an] is the restriction of t to this set. It is usually required that the attribute names in the header of S are a subset of those of R because otherwise the result of the operation will always be empty.
The simulation of the division with the basic operations is as follows. We assume that a1,...,an are the attribute names unique to R and b1,...,bm are the attribute names of S. In the first step we project R on its unique attribute names and construct all combinations with tuples in S:
T := πa1,...,an(R) × S
In the prior example, T would represent a table such that every Student (because Student is the unique key/attribute of the Completed table) is combined with every given Task. So Eugene, for instance, would have two rows, Eugene -> Database1 and Eugene -> Database2 in T.
In the next step we subtract R from T relation:
U := T − R
Note that in U we have the possible combinations that "could have" been in R, but weren't. So if we now take the projection on the attribute names unique to R then we have the restrictions of the tuples in R for which not all combinations with tuples in S were present in R:
V := πa1,...,an(U)
So what remains to be done is take the projection of R on its unique attribute names and subtract those in V:
W := πa1,...,an(R) − V
这里是我的代码:
select MaNV as EmpCode1
from tham_gia
where EmpCode1 not in(
select MaNV as EmpCode
from (
select MaNV as ECode, MaDA as PrCode
from (
select MaNV as E1Code
from tham_gia)
cross join (
select MaDA as Pr1Code
from tham_gia)
where ECode, PrCode not in(
select MaNV as E2Code, MaDA as Pr2Code
from tham_gia)
)
) ;
但没”工作!请帮助我,非常感谢你!
这个问题可能足够精确的回答,但似乎很抽象。您能否提供一些真实的数据示例(表中的记录和预期结果)? –
此外,目前还不清楚MaNV与MaDA的关系。 –