Is there set division in SQL?

送分小仙女□ 提交于 2019-12-20 01:36:05

问题


I'm fully aware that set division can be accomplished through a series of other operations, so my question is:

Is there a command for set division in SQL?


回答1:


http://vadimtropashko.files.wordpress.com/2007/02/ch3.pdf

From Page 32:

Relational Division is not a fundamental operator. It can be expressed in terms of projection, Cartesian product, and set difference.

So, no. :)




回答2:


Related question: Database Design for Tagging

And relevant part of answer is this article

So in short, no, there is no set division in SQL.




回答3:


Here is a nice explanation using relational algebra syntax.

Given tables sailors, boats and reserves (examples from Ramakrishnan & Gehrke's "Database Management Systems") you can compute sailors who have reserved all boats with the following query:

SELECT name FROM sailors
WHERE Sid NOT IN (
    -- A sailor is disqualified if by attaching a boat,
    -- we obtain a tuple <sailor, boat> that is not in reserves
    SELECT s.Sid
    FROM sailors s, boats b
    WHERE (s.Sid, b.Bid) NOT IN (
        SELECT Sid, Bid FROM reserves
    )
);

-- Alternatively:
SELECT name FROM sailors s
WHERE NOT EXISTS (
    -- Not reserved boats
    (SELECT bid FROM boats)
    EXCEPT
    (SELECT r.bid FROM reserves r
    WHERE r.sid = s.sid)
);


来源:https://stackoverflow.com/questions/110088/is-there-set-division-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!