relational algebra for Limit Operator

前提是你 提交于 2019-12-01 23:45:50

问题


What is relational algebra for these two SQL queries:

Select * from film where film_rating = 'PG' limit 20;

How can we show limit?

Select * from actor, country where first_name='BOB' and country='In';

where first_name is actor's column and country is country table's column...there is no relationship between these two tables...they are independent tables...

so can we use join operator here?


回答1:


About the limit in relational algebra. Traditional relational algebra does not support anything like the limit in SQL. This problem has been recognized and studied by Li, Chang, Ilyas and Song in RankSQL: query algebra and optimization for relational top-k queries (SIGMOD 2005). They have proposed a monotonic scoring function F that ranks the results by the sorting operator tauF.




回答2:


I'm only answering the question about joining here.

There is a m : n relation between actors and film. It must be realized through an intermediate table, say film_actors. Also the country table probably has a short name like "In" and a long name like "India".

SELECT film.title, film_actors.role_type, actor.name, country.long_name
FROM
    film
    LEFT JOIN film_actors ON film.film_id = film_actors.film_id
    LEFT JOIN actor ON film_actors.actor_id = actor.actor_id
    LEFT JOIN country ON actor.country = country.short_name

If the country table has only one column with this short name, then it makes no sense to include it into your query at all. Do it only if country has some additional information that you want to include.

I used LEFT OUTER JOINS here. They are only requested, if the table on the right side may not always have related records. A cartoon probably has no actors. Otherwise use INNER JOIN



来源:https://stackoverflow.com/questions/10229535/relational-algebra-for-limit-operator

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