简体   繁体   中英

One-to-One Relation or Use the Same Table?

I have the following tables created:

Animes(id,title,date), Comics(id,title,date), TVSeries(id,title,season,episode,date)

Some of them have already foreign keys (for one-to-many or many-to-many relations) of directors, genres, articles and so on.

Now i would like to create two more tables Reviews(id,rating,date) and Posts(id,thumbid,articleid,reviewid). A review is about one Anime and/or Comic TVSerie and vise-versa but properties of a review may be in more than one table. Its the same about a posts.

Is this a typical example of one-to-one relation in separate table or is it more efficient to add more properties to the existing tables? So more tables and relations or less tables more columns?

Thank you and i hope my question isnt that stupid but im a bit confused.

In my view, It is better to avoid foreign key relationship for one-to-one relationship. It is best suitable for one - many relationships.

I'm not exactly sure what your requirements are, but the choices are as follows:

  1. Have Reviews have 2 columns, either being a foreign key to the applicable table, can be NULL. This is really for when a single review can be about both.

  2. Have a ReviewsComics and ReviewsAnime table. You'd then have all the fields from Reviews in each table (and no Reviews table).

  3. An alternative (2) is to use them in conjunction with a Reviews table, then those 2 tables only has 2 fields which are foreign keys to Reviews and Comics/Anime respectively (thus no direct link between Reviews and Comics/Anime).

  4. Have a base table to which Anime and Comics are linked to 1-to-1 and have reviews link to that table instead.

  5. (Edit) If all the fields are all going to be the same (or similar) for Anime/Comics, you can merge them into a single table and add a type field, indicating Anime/Comics, then the problem goes away. This is similar to the base table option.

EDIT: The 2 reviews tables will probably give the best performance (unless you want to select all reviews for either, often), but with proper indices the performance shouldn't be an issue with any of the above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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