Relational Algebra [关系代数]

Relational Algebra is essential in database since it plays an important role in query optimization. To reach this goal, the first step is transforming the SQL to Relational Algebra.

 

Many materials can be found on the net, such as http://www.databasteknik.se/webbkursen/relalg-lecture/index.html and phungdinhvu.110mb.com/uploadfordown/shiyong_DB1_exercise1.ppt  

 

Besides, I want to give a more complex example, as follows.

 

Consider a movie review database with the following schema:

 

MOVIE (Mid, Mname, Director, Earning)

CRITIC (Cid, Cname, City)

PAPER (Pid, Pname, City)

REVIEW (Mid, Cid, Pid, Rating)

 

The goal is to find cities such that all critics living there have reviewed all movies directed by ABC.

 

It can be well solved by SQL:

  • select c1.City from CRITIC as c1 where not exists (  select * from CRITIC as c2 where c2.City = c1.City and c2.Cid not in (select r1.Cid from REVIEW as r1, MOVIE as m1 where r1.Mid=m1.Mid) )

After transforming the SQL to Relational Algebra:

A1 <- /pi_Mid (/sigma_{Director=ABC} (MOVIE) )

A2 <- /pi_{Mid, Cid} (Review)  / A1

A3 <- /pi_Cid (CRITIC) - A2

A4 <- /pi_City (CRITIC) - /pi_City( CRITIC /join_{Cid=A3.Cid} A3)

 

Finally, A4 contains the result that we want.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值