使用外键和主键理解数据库 table 引用

Making sense of database table references with foreign and primary keys

我对数据库逻辑比较陌生,正在尝试弄清楚多个 table should/shouldn 是如何相互引用的。

我有一个 table、'Book',应该有列:

| 'title' | 'genre' | 'author' | 'buyOption' | 'pubDate'
  1. 我希望每本书都有可能有 1 种或多种类型。
  2. 我希望每本书都有可能有一位或多位作者。
  3. 我希望每本书都可以有 1 个或多个购买选项 ('buyOption')。
    • 每本书的每个购买选项(亚马逊、沃尔玛等)都有唯一的 url。

我觉得有道理的(不对的地方请指正):

   __________________________
   |                        |
   |           Book         |
   |________________________|
   |Primary Key | book_id   | //seems redundant (same as title_id)...would like to just use title_id, but idk if that's possible
   |------------|-----------|
   |Foreign Key | title_id  | <--------------------------------------------|
   |Foreign Key | bo_id     | <----------------------------------|         |
   |Foreign Key | genre_id  | <--------------------------|       |         |
   |Foreign Key | author_id | <-------------------|      |       |         |
   | - - - - -  | - - - - - |                     |      |       |         |
   |            | pubDate   | //publish date      |      |       |         |
   |________________________|                     |      |       |         |
                                                  |      |       |         |
                                                  |      |       |         |
                                                  |      |       |         |
      __________________________                  |      |       |         |
      |                        |                  |      |       |         |
      |         Authors        |                  |      |       |         |
      |________________________|                  |      |       |         |
      |Primary Key | author_id |------------------|      |       |         |
      |------------|-----------|                         |       |         |    
 |--->|Foreign Key | title_id  |                         |       |         |
 |    | - - - - -  | - - - - - |                         |       |         |
 |    |            |  author   |                         |       |         |
 |    |____________|___________|                         |       |         |
 |                                                       |       |         |
 |                                                       |       |         |
 |    __________________________                         |       |         |
 |    |                        |                         |       |         |
 |    |         Genres         |                         |       |         |
 |    |________________________|                         |       |         |
 |    |Primary Key |  genre_id |-------------------------|       |         |
 |    |------------|-----------|                                 |         |
 |--->|Foreign Key |  title_id |                                 |         |
 |    | - - - - -  | - - - - - |                                 |         |
 |    |            |   genre   |                                 |         |
 |    |____________|___________|                                 |         |
 |                                                               |         |
 |    __________________________                                 |         |
 |    |                        |                                 |         |
 |    |       Buy Options      |                                 |         |
 |    |________________________|                                 |         |
 |    |Primary Key |  bo_id    |---------------------------------|         |
 |    |------------|-----------|                                           |
 |--->|Foreign Key | title_id  |                                           |
 |    | - - - - -  | - - - - - |                                           |
 |    |            | buyBrand  |   //(Walmart, Amazon, etc.)               |                   
 |    |            | buyUrl    |   //(ex: https://www.amzn.com/buyBook1)   |
 |    |____________|___________|                                           |
 |                                                                         |
 |                                                                         |
 |                                                                         |
 |         __________________________                                      |
 |         |                        |                                      |
 |         |          Title         |                                      |   
 |         |________________________|                                      |
 |---------|Primary Key | title_id  |--------------------------------------|
           |------------|-----------|                                           
           |            |   title   |
           |____________|___________|

有标题table有意义吗?如果是这样,我可以使用它的主键来填充其他各种 table 吗,如图所示?

如果 'Buy Options' table 每本书都有一堆不同的选项和关联的 url,是否可以直接从中获取 buyBrand 和 buyUrl主要'Book'table?最后,我只想要一个可以从中获取单元格数据的巨型 table。现在我正在尝试弄清楚如何用我的数据填充 tables,以及为每条数据填充什么 tables。

(再一次,我是数据库逻辑的新手,如果我的措辞难以理解,我深表歉意)

你的设计看起来不太好。您正在描述书籍与流派、书籍与作者、书籍与选项之间的多对多关系。

在书中存储对相关流派、作者和选项的引用 table 不是正确的方法:每本书只能存储一个相关值(一种流派、一位作者、一个选项), 而你需要很多。相反,对于这些关系中的每一个,您应该有一个单独的 table,称为桥 table,它引用关联。

另一方面,依赖于书的信息(比如书名)应该存储在书中 table。

这是书籍和类型的一个示例:

create table books(
    book_id int primary key,
    title varchar(100),  --dependent column
    pub_date date        --dependent column
);

create table genres(
    genre_id int primary key,
    name varchar(100)
);

create table book_genres(
    book_id int references book(book_id),
    genre_id int references genre(genre_id),
    primary key (book_id, genre_id)
);

现在,假设您要列出属于类型 'Sci-Fi' 的所有书籍;你会去:

select b.*
from books b
inner join book_genres bg on bg.book_id = b.book_id
inner join genres g on g.genre_id = bg.genre_id
where g.name = 'Sci-Fi'

应该为架构中的每个多对多关系实现相同的逻辑。