Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

mysql - 在MariaDB中创建桥表(Creating bridge table in MariaDB)

I'm trying to create to link two tables together with a bridge table in MariaDB.

(我正在尝试在MariaDB中创建将两个表与一个桥表链接在一起的方法。)

The table itself is called 'ActsIn' and is between the 'Actors' and 'Movies' tables.

(该表本身称为“ ActsIn”,位于“ Actors”和“ Movies”表之间。)

In my ER diagram, the 'ActsIn' table consists of 3 attributes-

(在我的ER图中,“ ActsIn”表由3个属性组成-)

  1. The Primary Key for the 'ActsIn' table which is a composite primary key made up of the two foreign keys below.

    (“ ActsIn”表的主键是由下面的两个外键组成的复合主键。)

  2. ActorName (A foreign key referencing the primary key in the 'Actors' table)

    (ActorName(引用“ Actors”表中主键的外键))

  3. Title (A foreign key referencing the primary key in the 'Movies' table)

    (标题(引用“电影”表中主键的外键))

When I'm creating this table in MariaDB, do I treat the composite primary key as an attribute in its own right named ActorName/Title?

(当我在MariaDB中创建此表时,是否将复合主键作为其本身名为ActorName / Title的属性来对待?)

//Create table ActsIn
//
CREATE TABLE IF NOT EXISTS ActsIn (
ActorName/Title VARCHAR(255) NOT NULL,
ActorName VARCHAR(255) NOT NULL,
Title VARCHAR(255) NOT NULL,
PRIMARY KEY (ActorName/Title)
);

Or make the 2 attribute fields ActorName and Title and then create a primary key referencing both of these attributes?

(还是在2个属性字段中定义ActorName和Title,然后创建引用这两个属性的主键?)

CREATE TABLE IF NOT EXISTS ActsIn (
ActorName VARCHAR(255) NOT NULL,
Title VARCHAR(255),
PRIMARY KEY (ActorName, Title)
);
  ask by Goselnator translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You would normally have a table of actors (actor_id*, name), a table of movies (movie_id*, title), and a table relating one to the other (actor_id*,movie_id*) - * = (component of) PRIMARY KEY

(通常,您将有一个演员表(actor_id *,名称),电影表(movie_id *,标题)和一个相互关联的表(actor_id *,movie_id *)- * = (component of) PRIMARY KEY)

If an actor can appear as several characters within the same movie, then you may want amend this model slightly.

(如果演员可以在同一部电影中显示为几个角色,那么您可能需要稍微修改此模型。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...