MySQL schema design: one table with type column VS multiple tables

K. Ayoub

I'm making a website about movies, i want to make a relationship between a movie and the cast (director, writers and actors), there's two possibilities of how i can achieve that, the first is to create a table of each cast:

CREATE TABLE director(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50));  
CREATE TABLE writer(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50));  
CREATE TABLE actor(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50));

And make a Many To Many relationship between each of this tables and movie table.
The second possibility is to create one table for the cast with a type column which can refer to director, writer or actor and make a Many To Many relationship between that table and movie table..

CREATE TABLE cast(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50), type varchar(10));

Note: for the actor, the auto-generate table will have some additional columns: character_name, role...
So which possibility is better for this situation ?

Fikret Basic

I would go with the second approach. It even corresponds to the standard SQL rules.

There are some modifications that you can do in case your types of casts (director, writter and so on...) have also some additional attributes (columns). In that case it is recommended to make them as subtables of the main table.

In your case, the main table would be "cast". It would have ID and other columns. The other tables would be created and would correspond to different cast members, like table: "director", "writter", ... Then do the 1-1 relation with the main "cast" table between every subtable. The relation would be mandatory on the subtable (meaning that e.g. "director" must have the relation to "cast" when it is created). In relational sense, the foreign key for the "director" would be also the primary key for it and it would imply to the primary key of the "cast".

This approach is recommended since not only you can add additional columns to the different cast members, but also other relations in case you want later to expand your database. You can also add additional subtables of the "cast" without changing anything in terms of the structure relative to the "movies" tables.

Эта статья взята из Интернета, укажите источник при перепечатке.

Если есть какие-либо нарушения, пожалуйста, свяжитесь с[email protected] Удалить.

Отредактировано в
0

я говорю два предложения

0обзор
Войти в системуУчаствуйте в комментариях

Статьи по теме

Mysql one middle table for multiple similar tables

Searching with multiple values in one column of Mysql table

Multiple tables with same columns or one table with an additional FK column

MySQL Design: Seperate Schema or Table Prefixes?

Drupal MySQL multiple tables, multiple conditions return table column data which matches a keyword

Join multiple tables on one specific column

MySql join tables with filter at one of table

Joining Multiple MySQL tables with Same Column Name

Why is it considered bad table design to use multiple tables in dynamodb?

Why is it considered bad table design to use multiple tables in dynamodb?

How to create table where one column contain names of other tables

MySQL information_schema.tables не создает новые записи

sql update table column based on values across multiple tables

Delete rows from multiple tables due to data from one table

How do I stack multiple frequency tables into one table in R?

why html table have multiple column's data into one column

Postgresql - Dump database with x tables - schema only but data from one table

How to delete from multiple tables with the same column in mysql?

PHP MySQL Selecting Multiple Tables Based on Column/Row Value

Combine multiple tables in one

Table-multiple-sort in boostrap-table doesn't work for multiple tables in one page

В чем разница между DATA_TYPE и COLUMN_TYPE в MySql INFORMATION_SCHEMA.COLUMNS

Slick - one to many table schema

SQL: Taking one column from two tables and putting them into one predefined table

Joining multiple tables in MySQL

Querying multiple tables on MySQL

Elastic Search: One index with custom type to differentiate document schemas VS multiple index, one per document type?

Add multiple combined PHP form data from multiple foreign MSSQL tables to one primary table

Озадаченный MySQL information_schema.tables info

TOP список

  1. 1

    Распределение Рэлея Curve_fit на Python

  2. 2

    TypeError: store.getState não é uma função. (Em 'store.getState ()', 'store.getState' é indefinido, como posso resolver esse problema?

  3. 3

    В типе Observable <unknown> отсутствуют следующие свойства из типа Promise <any>.

  4. 4

    Как добавить Swagger в веб-API с поддержкой OData, работающий на ASP.NET Core 3.1

  5. 5

    How to click an array of links in puppeteer?

  6. 6

    Merging legends in plotly subplot

  7. 7

    ViewPager2 мигает / перезагружается при смахивании

  8. 8

    Отчеты Fabric Debug Craslytic: регистрация, отсутствует идентификатор сборки, применить плагин: io.fabric

  9. 9

    How to normalize different curves drawn with geom = "step" when using stat_summary

  10. 10

    无法通过Vue在传单中加载pixiOverlay

  11. 11

    как я могу удалить vue cli 2?

  12. 12

    Как я могу нарисовать заполненный прямоугольник в JFreeChart?

  13. 13

    SQL Вычтите две строки друг от друга в одном столбце, чтобы получить результат

  14. 14

    Elasticsearch - Нечеткий поиск не дает предложения

  15. 15

    Single legend for Plotly subplot for line plots created from two data frames in R

  16. 16

    Описание моего типа Parser как серии преобразователей монад

  17. 17

    Как изменить цвета запятых и скобок в VS Code

  18. 18

    Сброс значения <input type = "time"> в Firefox

  19. 19

    Почему прокси в vue.config.js 404

  20. 20

    Как установить параметр -noverify с gradle ktx для робоэлектрических тестов Android?

  21. 21

    В чем разница между ifstream, ofstream и fstream?

популярныйтег

файл