Вопрос

I've hit a complete blank here.

I have a schema that has two relations. One is loan which has the attributes loan-number, branch-name and amount. The second relation is borrower which has customer-name and loan-number as its attributes. These two relations are linked via loan-number.

How would I go about writing a query in relational algebra form if I wanted to find the names of all customers with a balance less than 10000.

Out of curiosity how would I also do this as a basic SQL query.

Это было полезно?

Решение

Do some research on relational algebra 8 main operators: restriction, projection, cartesian product, join, union, intersection, set difference and division.

To answer your question:

loan(loan_number, branch_name, amount)
borrower(customer_name, loan_number)

Perform a natural join of the both relations, apply the restriction (balance less than 10000) and then display the names with the use of a projection. The following 2 relational algebra expressions below will both answer your questionrelational algebra code

meaning of symbols

Both expressions evaluate to the following SQL query:

select customer_name
from borrower b, loan l
where b.loan_number=l.loan_number and amount>10000;

Другие советы

Disclaimer: I am not too familiar with relational algebra.

SQL can be quickly seen as using an implicit inner join and then an filter on loan.amount:

SELECT customer-name
FROM Borrower, Loan
WHERE Customer.loan-number = Loan.loan-number
 AND Loan.amount > 10000

And then translate this into an relational algebra keeping in mind that the following symbols are the only ones needed for this:

The select operation (σ): - to identify a set of tuples which is a part of a relation and to extract only these tuples out. The select operation selects tuples that satisfy a given predicate or condition.

The project operation (Π): - returns its argument relation with certain attributes left out.

 Π customer-name (σ Borrower.loan-number=Loan.loan-number (σ Loan.amount>10000 (Borrower X Loan)))

I believe the SQL would be relatively straightforward... something like the following will probably suffice:

SELECT 
  b.customer-name,
  SUM(l.amount)
FROM
  borrower b
  JOIN loan l
    ON b.loan-number = l.loan-number
GROUP BY
  b.customer-name
HAVING
  SUM(l.amount) < 10000
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top