3NF 数据库规范化

发布于 2024-08-10 23:28:02 字数 251 浏览 7 评论 0原文

有一张 DVD 租赁逾期报告表。该商店有多张同一张 DVD 的副本(它们都已编号以便识别)。如何标准化这些数据以满足 3NF 要求?

标准化 http://img193.imageshack.us/img193/7804/normalization.jpg

There is a report table for overdue DVD rentals. The store has multiple copies of the same DVD (they are all numbered to be identified). How can I normalize this data to meet 3NF requirements?

Normalization http://img193.imageshack.us/img193/7804/normalization.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷途知返 2024-08-17 23:28:02

数据模型:

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

请注意,我没有使用主键作为向用户显示的值。

  1. 副本编号可能会更改,但您不想破坏引用完整性
  2. 不要公开表键

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

The data model:

VIDEO_ARTIST table

  • ARTIST_ID, pk
  • FIRST_NAME
  • LAST_NAME

VIDEOS table

  • VIDEO_ID, pk
  • VIDEO_TITLE
  • ARTIST_ID, fk
  • RUNNING_TIME

VIDEO_COPIES table

  • VIDEO_COPY_ID, pk
  • VIDEO_ID, fk
  • VIDEO_COPY_NUMBER

Notice that I'm not using the primary key for the value displayed to the users.

  1. Copy numbers could change, but you don't want to break referencial integrity
  2. Do not expose table keys

VIDEO_RENTALS table

  • VIDEO_COPY_ID, pk, fk
  • ACCOUNT_ID, pk, fk
  • DUE_DATE, pk

VIDEO_RENTALS_ACCOUNTS table

  • ACCOUNT_ID, pk
  • ACCOUNT_NUMBER, unique
  • FIRST_NAME
  • LAST_NAME

Same logic regarding the ACCOUNT_NUMBER as with the VIDEO_COPY_NUMBER...

Here's the SQL to use based on the data model to get the report example you provided:

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
沉鱼一梦 2024-08-17 23:28:02

看看对象——演员:

1. Customer
2. Title
3. Physical Medium (DVD, the thing you take home when borrowing)
4. Artist
5. Rental (act of renting = transaction)
  • 一位艺术家可以创作许多作品(动作、专辑);一个标题(专辑)可以由多名艺术家表演。
  • 一个标题可以有许多物理媒体(DVD、磁带、蓝光)副本;一个媒体只有一个标题
  • 一张介质(DVD)可供多名客户租用(一次租用一名); 客户可以租用许多媒体 (DVD)。 Media.Status 跟踪介质 (DVD) 的可用性。

“rental_model_01"

Look at objects--actors:

1. Customer
2. Title
3. Physical Medium (DVD, the thing you take home when borrowing)
4. Artist
5. Rental (act of renting = transaction)
  • One artist can author many titles (moves, albums); a title (album) can be performed by several artists.
  • One title can have many physical-media (dvd, tape, blue ray) copies; a medium has one title only.
  • One medium (DVD) can be rented by many customers (one at the time); a customer can rent many media (DVD). Media.Status tracks availability of a medium (DVD).

rental_model_01

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文