2015-10-21 51 views
0

我试图在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) 
     ) 
    ) ; 

但没”工作!请帮助我,非常感谢你!

+0

这个问题可能足够精确的回答,但似乎很抽象。您能否提供一些真实的数据示例(表中的记录和预期结果)? –

+0

此外,目前还不清楚MaNV与MaDA的关系。 –

回答

0

实际上,tham_gia是Participate,MaNV for EmpCode和MaDA for PrjCode(项目代码)的别名。基本上我想要的是找到参与所有可用项目的所有员工参与(对于奇怪的别名,家伙!抱歉) 我刚刚从这个链接找到答案: https://faculty.utpa.edu/lianx/old_courses/CSCI4333_2014fall/MySQL-set-operators.pdf 基本上它使用了与我一样的原则,但更明显的(它有一个表的 “a” 与列 “X,Y”,表 “b” 与列 “×”,它希望用b来分割):在此基础上

SELECT DISTINCT c1.y AS y 
FROM c c1 
WHERE NOT EXISTS 
(SELECT d.x FROM d 
    WHERE d.x NOT IN (SELECT c2.x FROM c c2 WHERE c2.y = c1.y)); 

,我做了一些修改:

select Par1.EmpCode 
from Participate as Par1 
where not exists (
    select Par2.PrjCode 
    from Participate as Par2 
    where Par2.PrjCode not in (
     select Par3.PrjCode 
     from Participate as Par3 
     where Par3.EmpCode = Par1.EmpCode)); 

它的工作!无论如何感谢:)