Uploaded by hugo Fung

COMP3278A-2021-Assignment1-Solutions

advertisement
COMP3278A Introduction to Database Management Systems
2021 – 2022
Assignment 1 Solutions: ER Design and SQL
Question 1 (50%) [Entity-Relation Design]
(a) Please model the above users’ requirements by drawing the corresponding E-R Diagram.
(b) Please reduce your E-R Diagram into relational table schemas. For each relation, underline the
primary key and identify all foreign key(s), if any.
User (user_name, profile.birthday, profile.email profile.date_joined)
Seller (user_name)
Foreign keys: user_name refers User
Customer (user_name)
Foreign keys: user_name refers User
Link_Leader_Member (leader_name, member_name, group_name)
Foreign keys: leader_name refers Customer, member_name refers Customer
Account (account_id, account_name, password, user_name)
Foreign keys: username refers User
CashAccount (account_id, balance)
Foreign keys: account_id refers Account
PointAccount (account_id, point)
Foreign keys: account_id refers Account
Link_CashAccount_PointAccount (cashaccount_id, pointaccount_id)
Foreign keys: cashaccount_id refers CashAccount, pointaccount_id refers PointAccount
Transaction (transaction_id, account_id, product_id, item_id, quantity, create_date)
Foreign keys: account_id refers Account, product_id refers Item, item_id refers Item
TransactionMessage (transaction_id, message)
Foreign keys: transaction_id refers Transaction
InternalTransaction (transaction_id, internal_id, from_account_id, to_account_id)
Foreign keys: transaction_id refers Transaction, from_account_id refers CashAccount, to_account_id refers
CashAccount
Product (product_id, product_name, seller_name)
Foreign keys: seller_name refers Seller
Item (product_id, item_id, item_name, price)
Foreign keys: product_id refers Product
Question 2 (50%) [SQL]
(a) Find the hash value of the block(s) that has more than one next block.
SELECT hash
FROM Block
WHERE block_id IN (
SELECT prev_id
FROM Block
GROUP BY prev_id
HAVING COUNT(*) > 1
);
(b) Find the transaction ID of the transaction(s) where the total token value does not match with the
transaction amount.
SELECT Res.trans_id
FROM (
SELECT Tr.trans_id, Tr.amount, SUM(Tok.value)
FROM Transaction Tr, InternalTransaction IT, Token Tok
WHERE Tr.trans_id = IT.trans_id AND IT.token_id = Tok.token_id
GROUP BY Tr.trans_id
HAVING SUM(Tok.value) <> Tr.amount) Res
;
(c) Find internal transaction(s) that is produced by ‘Alice’ and the transferred token value is smaller
than 100. Return internal ID and its token value.
SELECT IT.internal_id, Tok.value
FROM Account A, Token Tok, InternalTransaction IT
WHERE IT.token_id = Tok.token_id
AND A.account_num = Tok.account_num
AND Tok.value < 100
AND A.user_name = ‘Alice’;
(d) Find the token ID of the token(s) that has never been transferred.
SELECT token_id
FROM Token
WHERE token_id
NOT IN (
SELECT token_id
FROM InternalTransaction
);
(e) Show the username and the number of accounts the user owns for the user(s) that has the
greatest number of tokens with value greater than 100.
(1) Understand 1:
WITH user_tokens AS (
SELECT A.user_name as user_name, COUNT(Tok.token_id) as num_tokens
FROM Account A, Token Tok
WHERE A.account_num = Tok.account_num
AND Tok.value > 100
GROUP BY A.user_name
)
SELECT user_name, COUNT(DISTINCT account_num)
FROM Account
WHERE user_name IN (
SELECT user_name
FROM user_tokens
WHERE num_tokens >= ALL (
SELECT num_tokens FROM user_tokens
)
)
GROUP BY user_name;
(2) Understand 2:
SELECT A.user_name, COUNT(DISTINCT A.account_num)
FROM Account A, Token Tok
WHERE A.account_num = Tok.account_num
AND Tok.value > 100
GROUP BY A.user_name
HAVING COUNT(Tok.token_id) >= ALL(
SELECT COUNT(Tok.token_id)
FROM Account A, Token Tok
WHERE A.account_num = Tok.account_num
AND Tok.value > 100
GROUP BY A.user_name
);
Download