简体   繁体   English

数据库设计一个表或两个表

[英]Database design one Table or two Tables

I have a question about database design. 我对数据库设计有疑问。
For example, I have two type of objects, one is movie, the other is TvShow. 例如,我有两种类型的对象,一种是电影,另一种是TvShow。
Of course, there are some same field, like name cast story line etc. 当然,也有一些相同的字段,例如名称转换故事行等。
There are also lots of different columns, like season episode etc. 也有很多不同的栏目,例如季节插曲等。
Actually every episode is movie-like. 实际上,每一集都像电影一样。

Maybe there are large quantities of data. 也许有大量数据。
How to design ? 如何设计?
A) One table named video and two tables named movie and TvShow. A)一个表名为video,两个表名为movie和TvShow。 The video table has the common columns. 视频表具有公共列。

B) Two tables named movie and TvShow. B)名为movie和TvShow的两个表。

Which is better and why? 哪个更好?为什么? Thank you~ 谢谢〜

Unless you have specific reasons to do otherwise, I would recommend alternative "A", with three tables. 除非您有其他特殊原因,否则我建议使用带有三个表的替代“ A”。 Especially if you need to refer to videos in general from other tables. 特别是如果您需要参考其他表格中的视频。

Use a common primary key, where the primary keys in the two tables TVshow and Movie are also foreign keys to the primary key in Video. 使用公共主键,其中两个表TVshow和Movie中的主键也是Video中主键的外键。 Now you can refer to TV shows, to movies, or to videos in general, by using foreign keys to one of the three tables. 现在,通过使用三个表之一的外键,您可以参考电视节目,电影或一般视频。

Performance will be slower than with two tables (or a solution with a single table), since now the data is spread out in several places, and the queries will be a bit more complicated. 性能将比使用两个表(或一个具有单个表的解决方案)要慢,因为现在数据分散在多个位置,并且查询将更加复杂。 If you are sure you need better performance you may have to use a different design. 如果您确定需要更好的性能,则可能必须使用其他设计。 But it is a very common error to just assume that performance will be an issue. 但是,仅假设性能将是一个问题,这是一个非常常见的错误。 With proper indexing standard relational databases can be very fast, even when searching in very large data sets. 使用适当的索引,即使在非常大的数据集中进行搜索时,关系数据库也可以非常快速。

Having One Video table and two tables movie and TvShow complicates things a bit. 具有一个视频表和两个表的movie和TvShow会使事情变得有些复杂。 Say you want to retrieve all TV Shows, you will need a link to the videos table. 假设您要检索所有电视节目,则需要一个指向视频表的链接。 Of course on the downside you will have some "duplicate" field values, but since performance is critical and directly related to your selects, better go for option B. 当然,在缺点方面,您将拥有一些“重复的”字段值,但是由于性能至关重要,并且与您的选择直接相关,因此最好选择选项B。

Even your inserts will be quicker in B since in A, 2 inserts are required. 在B中,甚至您的刀片也会更快,因为在A中,需要2个刀片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM