sách gpt4 ăn đã đi

mysql - 书籍-文章-图像的数据库架构建议

In lại Tác giả: Walker 123 更新时间:2023-11-29 22:00:59 28 4
mua khóa gpt4 giày nike

我有以下型号。

Book has Articles (Article has foreign key to Book)

Article has Images (Article has upto #max number of foreign keys to images, Article have #max number of foreign key to images. which maybe null)

Furthermore I have Book - Image relation
because certain images are included in the book but not owned by article

我制作的模型可以快速回答诸如此类的问题

  • 给我一篇文章的图片。
  • 给我一本书的图片(按时间排序,如果需要可分页)

人们认为我的造型很糟糕。(when foreign key points to something else, move other data as well)

想了解更好的替代架构。

câu trả lời hay nhất

我会选择简单的解决方案(假设 postgresql 数据库):

create table book (
id integer primary key
);

create table article (
id integer primary key,
id_book integer references book(id)
);

create table image (
id integer primary key,
id_article integer references article(id),
id_book integer references book(id),
check ((id_article is null or id_book is null) and
(id_article is not null or id_book is not null))
);

检查约束不允许将图像分配给文章或书籍。

  • 如果图片来自文章 => id_article 已填充
  • 如果仅来自书籍(而不来自文章)=> id_book 已填充

Give me images for a book

select *
from book b
left join
(select *, id_book as book from image where id_article is null
liên minh tất cả
select i.*, a.id_book as book from article a join image i on i.id_article=a.id) i on i.book = b.id;

关于mysql - 书籍-文章-图像的数据库架构建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32650092/

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress