Sql BealdWe)包括聚合运算符,因为它们非常有用,例如所有的数据库约束可以用计数> /代码>表示。@ OnDayay:何时有可能编写一个查询,发现船只保留了2,而只有2个水手?@ IMLIE:考虑“由两个或多个水手保留的船只”减去“三个或更多水手保留的船只

Sql BealdWe)包括聚合运算符,因为它们非常有用,例如所有的数据库约束可以用计数> /代码>表示。@ OnDayay:何时有可能编写一个查询,发现船只保留了2,而只有2个水手?@ IMLIE:考虑“由两个或多个水手保留的船只”减去“三个或更多水手保留的船只,sql,aggregate,relational-algebra,Sql,Aggregate,Relational Algebra,BealdWe)包括聚合运算符,因为它们非常有用,例如所有的数据库约束可以用计数> /代码>表示。@ OnDayay:何时有可能编写一个查询,发现船只保留了2,而只有2个水手?@ IMLIE:考虑“由两个或多个水手保留的船只”减去“三个或更多水手保留的船只”。相当于“正好有两个水手预订的船”。非常好的解释。 SELECT B.bid, B.bname FROM Boats B, Reserves R WHERE B.bid = R.bid GROUP BY R.bid HAVING 2 = (S


BealdWe)包括聚合运算符,因为它们非常有用,例如所有的数据库约束可以用<代码>计数> /代码>表示。@ OnDayay:何时有可能编写一个查询,发现船只保留了2,而只有2个水手?@ IMLIE:考虑“由两个或多个水手保留的船只”减去“三个或更多水手保留的船只”。相当于“正好有两个水手预订的船”。非常好的解释。
SELECT B.bid, B.bname
FROM Boats B, Reserves R
WHERE B.bid = R.bid
GROUP BY R.bid
HAVING 2 = (SELECT COUNT(*)
FROM Reserves R2
WHERE R2.bid = B.bid);
SELECT DISTINCT R1.bid
  FROM Reserves AS R1 
       JOIN Reserves AS R2
          ON R1.bid = R2.bid
             AND R1.sid < R2.sid;
SELECT DISTINCT R1.bid
  FROM Reserves AS R1
       JOIN Reserves AS R2
          ON R1.bid = R2.bid
             AND R1.sid < R2.sid
       JOIN Reserves AS R3
          ON R1.bid = R3.bid
          AND R2.sid < R3.sid;
SELECT DISTINCT R1.bid
  FROM Reserves AS R1 
       JOIN Reserves AS R2
          ON R1.bid = R2.bid
             AND R1.sid < R2.sid
EXCEPT
SELECT DISTINCT R1.bid
  FROM Reserves AS R1
       JOIN Reserves AS R2
          ON R1.bid = R2.bid
             AND R1.sid < R2.sid
       JOIN Reserves AS R3
          ON R1.bid = R3.bid
          AND R2.sid < R3.sid;
SELECT DISTINCT R1.bid
  FROM Reserves AS R1 
       JOIN Reserves AS R2
          ON R1.bid = R2.bid
             AND R1.sid < R2.sid
 WHERE R1.bid NOT IN (
                      SELECT DISTINCT R1.bid
                        FROM Reserves AS R1
                             JOIN Reserves AS R2
                                ON R1.bid = R2.bid
                                   AND R1.sid < R2.sid
                             JOIN Reserves AS R3
                                ON R1.bid = R3.bid
                                AND R2.sid < R3.sid
                     );
B - A = B ∩ A'
SELECT DISTINCT R0.x
FROM R as R1
JOIN R as R0 ON R1.x<>R0.x
WHERE R1.x=val
SELECT DISTINCT R.x FROM R WHERE R.x=val
π( R.bid ) (
   σ( R.bid=R2.bid and R.sid<R2.sid )( R x ρ(R, R2) ) 
)
 π(R.bid)(σ(R.bid<>R1.bid)(
    π(R.bid)(R)
      x
    π(R1.bid) (
        σ( R1.bid=R2.bid and R2.bid=R3.bid and R1.sid<R2.sid and R2.sid<R3.sid )( ρ(R, R1) x ρ(R, R2) x ρ(R, R3) )
     )
  ))
π( R.bid ) (
   σ( R.bid=R2.bid and R.sid<R2.sid )( R x ρ(R, R2) ) 
) ∩ π( R.bid ) (
    σ(R.bid<>R1.bid)(
       π(R.bid)(R)
         x
       π(R1.bid) (
           σ( R1.bid=R2.bid and R2.bid=R3.bid and R1.sid<R2.sid and R2.sid<R3.sid )( ρ(R, R1) x ρ(R, R2) x ρ(R, R3) )
        )
     )
 )
(SELECT DISTINCT R1.bid
  FROM Reserves AS R1 
    JOIN Reserves AS R2 ON R1.bid = R2.bid AND R1.sid < R2.sid
) INTERSECT (
SELECT DISTINCT R.bid
  FROM Reserves AS R1
    JOIN Reserves AS R2 ON R1.bid = R2.bid AND R1.sid < R2.sid
    JOIN Reserves AS R3 ON R1.bid = R3.bid AND R2.sid < R3.sid
    JOIN Reserves AS R ON R.bid<>R1.bid
)