How to find all tuples in a table if and only if the tuple appears once?

不问归期 提交于 2020-01-03 03:19:30

问题


I have a table:

x | y | z
------------
1 | 1 | *
1 | 1 | *
1 | 3 | *
2 | 2 | *
2 | 3 | *
3 | 4 | *
3 | 4 | * 
3 | 3 | *

What is the relational algebra representation of only returning all unique (x, y) tuples?

For example, I would like the following (x,y) tuples returned in the above table: (1,3), (2,2) (2,3), and (3,3).

Thanks


回答1:


  1. Rename R to S

    S := ρS/R(R)

  2. Join R and S on x,y

    D := R ⋈S.x = R.x ∧ S.y = R.y S

    This squares the number of tuples with a particular value for (x,y). Particularly, if a value for (x,y) appears only once in R, it appears only once in D.

  3. Join R and S on x,y,z

    E := R ⋈S.x = R.x ∧ S.y = R.y ∧ S.z = R.z S

    This basically adds some columns to R. It does not add or remove tuples.

  4. Subtract E from D and project to the attributes of R

    F := πx,y,z(D\E)

    This removes the tuples from D, that where created by joining a tuple from R to the corresponding tuple in S. The remaining tuples are the ones that where created by joining a tuple in R to a different tuple in S. Particularly, if a value for (x,y) appears only once in R, no tuple in F exists with that value.

  5. Remove the tuples in F from R

    R\F



来源:https://stackoverflow.com/questions/19482046/how-to-find-all-tuples-in-a-table-if-and-only-if-the-tuple-appears-once

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