有一张过期DVD租赁报告表。商店有同一张DVD的多个副本(它们都被编号以便识别)。如何规范化这些数据以满足3NF要求?
Normalization http://img193.imageshack.us/img193/7804/normalization.jpg
答案 0 :(得分:10)
数据模型:
VIDEO_ARTIST
表ARTIST_ID
,pk FIRST_NAME
LAST_NAME
VIDEOS
表VIDEO_ID
,pk VIDEO_TITLE
ARTIST_ID
,fk RUNNING_TIME
VIDEO_COPIES
表VIDEO_COPY_ID
,pk VIDEO_ID
,fk VIDEO_COPY_NUMBER
请注意,我没有使用主键显示给用户显示的值。
VIDEO_RENTALS
表VIDEO_COPY_ID
,pk,fk ACCOUNT_ID
,pk,fk DUE_DATE
,pk VIDEO_RENTALS_ACCOUNTS
表ACCOUNT_ID
,pk ACCOUNT_NUMBER
,唯一FIRST_NAME
LAST_NAME
ACCOUNT_NUMBER
与VIDEO_COPY_NUMBER
...
以下是基于数据模型使用的SQL,以获取您提供的报告示例:
SELECT v.video_title 'Video Title',
aa.artist_name 'Artist',
vc.video_copy_number 'Copy Number',
v.running_time 'Length',
vr.due_date 'Date Due',
acct.borrower_name 'Borrower',
acct.account_number 'Card Number'
FROM VIDEO_RENTALS vr
JOIN VIDEO_COPIES vc ON vc.video_copy_id = t.video_copy_id
JOIN VIDEOS v ON v.video_id = vr.video_id
JOIN (SELECT a.artist_id,
a.firstname +' '+ a.lastname AS artist_name
FROM ARTIST a) aa ON aa.artist_id = vr.artist_id
JOIN (SELECT vra.account_id,
vra.account_number,
vra.firstname +' '+ vra.lastname AS borrower_name
FROM VIDEO_RENTALS_ACCOUNTS vra) acct ON acct.account_id = vr.account_id
答案 1 :(得分:3)