I have a situation where I am nowhere. I don't know how to solve the relational algebra questions.
Deposit (Branch, Acc-No, Cust-Name, Balance)
Loan (Branch, Loan-No, Cust-Name, Balance)
Branch (Branch, Assets, Branch-County)
Customer (Cust-Name, Cust-County, Branch)
Produce a relation that shows the branch, customer name, balance and account number for all customers that have a loan bigger than £2500.00 and all customers that have a deposit account with a balance smaller than £100.00. All customers should be at the Romford branch.
This is what I came up with so far. Is it correct?
π Branch, Acc-No, Cust-Name, Balance (
σ(Loan.Balance > 2000 ∧ branch='Romford')(Loan)
∪ σ(Deposit.Balance < 150 ∧ branch='Romford')(Customer ∩ Deposit)
)
Correct Ansers is:
π Branch, Cust-Name, Balance, Acc-No,
(σ Balance < 100^branch=”Romford” (Deposit))
∪
π Branch, Cust-Name, Balance, Loan-No,
(σ Balance > 2500 ^branch=”Romford”(Loan))
Given statements. Every table/relation has a statement parameterized by columns/attributes. (Its "characteristic predicate".) The rows/tuples that make the statement true go in the table/relation. First find the statements for the given tables/relations:
Notice that the table/relation definition is shorthand for the statement.
Query statements. Now put these given statements together to get a statement that only the rows we want satisfy. Use AND, OR, AND NOT, AND condition. Keep or drop names. Use a new name if you need one.
I will do an example like part of your assignment:
I want those rows. So I want a statement that exactly those rows make true. So I make statements that get closer and closer to the one I want. So I start:
Now I want to use a different name for the loan balances because I want to end up using Balance for account balances only:
Now I want account balances too:
If I had only used one name Balance then it would have had to be a loan balance and an account balance. The rows/tuples would have been for customers with a loan balance the same as an account balance. And the Balance column/attribute would have been those values.
Now I want to limit the balances and the branch:
Now I only want some of the columns/attributes:
This is a statement for the example rows.
You can use "Keeping names to keep" or "Dropping names to drop. (In logic, Dropping is called FOR SOME or THERE EXISTS. Because we want the statement inside it to be true FOR SOME value(s) for a name, ie we want that THERE EXISTS value(s) for a name that makes the statement true.)
Query shorthand. Now replace each statement by its shorthand.
In my example I get:
(Notice that in the second half of your question's attempt you don't need Customer. Because you don't need its statement. Because you can state everything about the rows you want without it. So it just adds Cust-County which you eventually throw away without using.)
Query algebra Now to get the algebra replace:
∩ (intersection) and x (product) are special cases of ⋈ (∩ for both sides the same columns/attributes and x for no shared columns/attributes).
Remember that column/attribute names get introduced by table/relation statements & tables/relations but removed by Keeping/Dropping & π. Remember that a renaming in a given statement becomes a ρ.
I get:
(I don't know what particular algebra notation you are supposed to use. Learn its rules for dotting names and using equijoin vs natural join. Also I don't know what kind of σ conditions it allows.)
Follow the example. So take a description of rows and write a statement that exactly those rows make true. Then convert to given statements. Then replace by shorthands. Then replace by algebra.
Continue for another part of your overall statement.
Keep going.
You will have to find the right order to say things in. Try different orders. Because you have to use NOT via AND NOT of statements/tables or via a condition. And OR and AND NOT of statements/tables must have the same columns/attributes on each side. And a name in in a condition has to be mentioned in a statement it is ANDed with.
Your question. It took me a while to parse and correct the goal you gave:
This is:
(I had to add a word to make sense of this. But it is unbelievable that this is supposed to mean "branch, customer name, balance and number [labelled what??] where balance and number are loan balance and number for customers with loans > 2000 or balance and number are account balance and number for customers with account balances < 150".)
This has an AND in the middle so you might think it will give an algebra ⋈ (natural join) or ∩ (intersection). But you must describe your columns/attributes only in terms of given statements and conditions. It turns out that the AND gets turned into an OR. Also it turns out that we have to add an extra Deposit statement. So that we have the loan customers account info. Remember that you have to have the same columns/attributes on both side of an OR (or AND NOT).
First "branch, customer name, account balance, account number for" "all the customers that have a loan bigger than £2000". This looks like what we did up above. But this time lets limit the branches later:
Now "branch, customer name, account balance, account number for" "all the customers with deposit account with a balance smaller than £150". This is simpler than before so I hope you can understand it directly:
Now we want the rows that make the statement "statement A OR statement B" true:
Now we limit the branch:
This is a statement for the wanted rows. Now we replace by shorthands:
An answer. Now we replace by algebra:
PS: Algebra = loopless calculation The whole point of the relational algebra is that statements exactly correspond to algebraic expressions: statements correspond to tables/relations and (statements') logic operators correspond to algebra operators. But the algebra version is a loopless description that can be automatically calculated. The rows that make a statement true are the value of its algebraic version. We give the rows for the table/relation statements and the algebra calculates the rows for any other statement we combine from them.
This is the answer I came up with: