2015-10-21 50 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)); 

它的工作!無論如何感謝:)