Selecting DISTINCT rows in relational algebra

左心房为你撑大大i 提交于 2020-04-06 04:19:22

问题


There is a DISTINCT operator in SQL. However, I have an assignment in which I need to get some distinct values from a table, and I can only use relational algebra. Is there a way?


回答1:


Relational projections always return distinct tuples so DISTINCT is never needed. Duplicate tuples are not permitted in the RA - that being one major difference between the relational model and the SQL model.




回答2:


Relational Algebra is based on set theory. SQL is an implementation of multi-set theory. Set always has distinct elements. So tuples are distinct by default in Relational Algebra.




回答3:


The projection in relational algebra is equivalent to select distinct. In select statement of sql, duplicates are allowed as it is based on multiset theory but in projection of relational algebra, which is based on set theory, duplicates are not allowed.

Hence, relational algebra equivalent of select distinct is projection.




回答4:


As you can see in this RA calculator, creating a relation with duplicate tuples only saves distinct rows. You could, in theory, get a sub-tuple duplicate by using a cartesian product on a relation with itself and a projection on the added columns. Yet again the duplicate sub-tuples will be eliminated, meaning - any operation in RA includes the DISTINCT clause if you want to use the SQL analogy.



来源:https://stackoverflow.com/questions/4775728/selecting-distinct-rows-in-relational-algebra

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