数据库关系代数,sql查询

时间:2017-04-14 10:03:52

标签: database relational-algebra

5.考虑以下库架构

Books (book_no,title,authors,publisher)
Borrower(borrower_id, name, DOB, job)
Borrowed(borrower_id,book_no,date)

在关系代数中编写以下查询

  • 一个。找到借用“Wiley”出版的3本以上书籍的借款人的姓名和借款人
  • 湾找出借用“wiley”
  • 出版的任何书籍的借款人的姓名
  • ℃。找出每个借款人借来的平均书籍数量,。

1 个答案:

答案 0 :(得分:0)

根据你的问题,sql查询就像这样

问题1

select B.borrower_id, B.name 
  from Borrower as B, Borrowed as Bw, Books as Bk
 where B.borrower_id = Bw.borrower_id
   and Bw.book_no = Bk.book_no
   and Bk.publisher = 'Wiley'
group by B.borrower_id, B.name 
having count(Bw.borrower_id) > 3

问题2

select B.name 
  from Borrower as B, Borrowed as Bw, Books as Bk
 where B.borrower_id = Bw.borrower_id
   and Bw.book_no = Bk.book_no
   and Bk.publisher = 'Wiley'
  group by B.name

问题3

select B.borrower_id, B.name, count(Bw.borrower_id) as cnt 
  from Borrower as B, Borrowed as Bw, Books as Bk
 where B.borrower_id = Bw.borrower_id
   and Bw.book_no = Bk.book_no
group by B.borrower_id, B.name