多对多关系

发布于 2024-09-25 19:10:55 字数 798 浏览 2 评论 0原文

我有下表 - 演员、女演员、导演和演员电影。

前两个表与电影表具有多对多关系。

所以我创建了 Celebrity_Movie 表,其主键为 所有这些表作为外键。

所以我的问题是如何插入记录 在 Celebrity_Movie 表中。

因为可以有多个男演员、女演员 一部电影。例如,电影“Race”有多个演员和演员。多名女演员。那么如何 在 Celebrity_Movie 表中填充“Race”电影的数据?

Actor table
Actor_id Actor_name
1        Amitabh
2        Akshay kumar
3        Hritik roshan
4        Amir khan
5        R. Madhavan
6        Sharman joshi
7        Ajay devgan
8        Tushar
9        Arshad varsi

Actress
Actress_id Actress_name
1          Aishwairya
2          Katrina
3          Bipasha
4          Sameera reddy
5          Kareena
6          Amrita rao

Director
Direct_id Direct_name
1         Abbas_Mustan
2         Priyadarshan

Movie
Movie_id Movie_name
M1       Race
M2       Golmal2
M3       3 Idiots

I have the following table - Actor, Actresses, Director & Movie.

First 2 tables have many to many relationship with movie table.

So i have created Celebrity_Movie table which has primary key of
all these tables as foreign key.

So my question is how can I insert records
in Celebrity_Movie table.

Because there can be multiple actors, actresses in
a movie. e.g. Movie 'Race' has multiple actors & multiple actresses. So how to
fill data in Celebrity_Movie table for 'Race' movie?

Actor table
Actor_id Actor_name
1        Amitabh
2        Akshay kumar
3        Hritik roshan
4        Amir khan
5        R. Madhavan
6        Sharman joshi
7        Ajay devgan
8        Tushar
9        Arshad varsi

Actress
Actress_id Actress_name
1          Aishwairya
2          Katrina
3          Bipasha
4          Sameera reddy
5          Kareena
6          Amrita rao

Director
Direct_id Direct_name
1         Abbas_Mustan
2         Priyadarshan

Movie
Movie_id Movie_name
M1       Race
M2       Golmal2
M3       3 Idiots

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没企图 2024-10-02 19:10:55

您需要三个多对多关系表,因为您希望将导演、演员和女演员表与电影相关联。

有几种方法可以做到这一点:

1) 最好的方法可能是简单地创建三个不同的关系表:

Actors_Movies
  id
  actor_id # foreign key
  movie_id

Actresses_Movies
  id
  actress_id
  movie_id

Directors_Movies
  id
  director_id
  movie_id

其他方法更为复杂,其中单个连接表用于不同类型的连接(演员、女演员、导演)。首先,我推荐三个连接表。

添加如果您只想对男演员/女演员进行多对多,最简单的方法是重新定义“人才”表而不是男演员/女演员表来处理两者:

Talent table
  id
  name
  actor   # boolean: true means an actor, false means an actress

Talent_Movie  # many to many
  id
  talent_id
  movie_id

You need three many-to-many relationship tables because you want to relate Director, Actor and Actress tables to Movies.

There are several ways to do this:

1) Probably best is to simply create the three different relationship tables:

Actors_Movies
  id
  actor_id # foreign key
  movie_id

Actresses_Movies
  id
  actress_id
  movie_id

Directors_Movies
  id
  director_id
  movie_id

Other ways are more complicated where a single join table is used for the different types of joins (Actor, Actress, Director). To start with, I recommend the three Join tables.

Added If you only want to many-to-many for the actor/actresses, the easiest way is to redefine a "Talent" table instead of the Actor/Actress tables to handle both:

Talent table
  id
  name
  actor   # boolean: true means an actor, false means an actress

Talent_Movie  # many to many
  id
  talent_id
  movie_id
晚雾 2024-10-02 19:10:55

正如其他人之前所说,您需要三个多对多表。

我是否可以建议您考虑仅使用一个人表并包括该人的性别。此外,您可以在关系表中添加一个“角色”,以指示该人在给定电影中扮演的角色(演员/导演等)。

As other previously has stated you need three many-to-many tables.

Might I suggest that you consider using only one person table and include the sex of the person. Furthermore you could add a 'role' to the relation table to indicate what role the person has (actor/director etc.) in a given movie.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文