关系数据库 除法


假设有两个关系,R(a,b,c)和T(b,c)

  使用投影、笛卡尔积和集合的差,表示R÷T

  R÷T = πa(R) - πa[ πa(R) × T – R ]

  SQL:

  select a from R

  minus(

  select a from(

  select * from (select a from R), T

  minus

  select * from R

  )

  )

  使用NOT EXISTS的实现

  理解:为了帮助理解,假设有关系S(a,d,e),其中a为主键,对于关系T,(b,c)即为主键,那么R就是连接S,T的中间表。

  1. 找出T中的一些元组,这些元组的中的(b,c)对于R中的不同的a并没有全部包含在R的元组中;

  2. 将上个步骤中的R的那些元组从R中排除掉。

  SQL:

  select distinct a from R r1 where not exists(  -- 相当于关系S

  select * from T where not exists(

  select * from R r2 where r1.a = r2.a and  r2.b = t.b and r2.c = t.c

  )

  )


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3