我只是学习如何将sql查询转换为关系代数,反之亦然。
Customer(customer_name, customer_street, customer_city)
Loan(loan_number, branch_name, amount)
Borrower(customer_name, loan_number)
Account(account_number, branch_name, balance)
Depositor(customer_name, account_number)
我想找到住在费尔法克斯的客户,有贷款,但没有银行账户。
CustomersInFFX = PROJECT customer_name (SELECT customer_city = 'Fairfax'(Customer))
LoanOwners = (CustomersInFFX NATURAL JOIN Borrower NATURAL JOIN Loan)
AccountHolders = (Depositor NATURAL JOIN ACCOUNT)
PROJECT customer_name (LoanOwners - AccountHolders)
这有意义吗?
* SELECT:σ,PROJECT:π,NATURAL JOIN:⋈
答案 0 :(得分:0)
这是我尝试使用标准SQL:
WITH CustomersInFFX
AS
(
SELECT *
FROM Customer
WHERE customer_city = 'Fairfax'
),
LoanOwners
AS
(
SELECT *
FROM CustomersInFFX
NATURAL JOIN Borrower
NATURAL JOIN Loan
),
AccountHolders
AS
(
SELECT *
FROM Depositor
NATURAL JOIN ACCOUNT
)
SELECT *
FROM LoanOwners
EXCEPT CORRESPONDING DISTINCT
SELECT *
FROM AccountHolders;
但是,这仅在LoanOwners
和AccountHolders
表具有相同数量的属性且每个具有相同名称的情况下才有效。情况不太可能如此,因此您只需要投射(SELECT
)所需的属性,例如。
SELECT customer_name
FROM LoanOwners
EXCEPT DISTINCT
SELECT customer_name
FROM AccountHolders;