简体   繁体   English

我可以从 2 个不同表的主键为第三个表创建复合键吗? MySQL支持吗?

[英]Can I create a composite key for a third table from the primary keys of 2 different tables? Does MySQL support it?

I have got 3 different tables:我有 3 个不同的表:

review table-> this has mov_id as the primary key.查看表-> 这有 mov_id 作为主键。 users_table -> this has username as the primary key. users_table -> 这有用户名作为主键。 review_table -> can I make a composite primary key for this table from movie_id and username? review_table -> 我可以根据 movie_id 和用户名为这个表创建一个复合主键吗?

users table用户表

reviews table评论表

comments table评论表

I can't connect these 3 tables and I can't figure out why.我无法连接这 3 个表,我也不知道为什么。

This is the error I'm getting while creating the comments table这是我在创建评论表时遇到的错误

You can create composite primary key with foreign keys (primary keys from others tables), you have nothing special to do.您可以使用外键(来自其他表的主键)创建复合主键,您没有什么特别要做的。 Just constrain the foreign keys and declare them as primary.只需约束外键并将它们声明为主键即可。

With the names of tables and columns you provided, here's a query to create the table you want:使用您提供的表名和列名,下面是创建所需表的查询:

CREATE TABLE review_table (
  movie_id INT,
  username VARCHAR(255),
  -- Change INT and VARCHAR(255) by what you used on the others tables
  PRIMARY KEY (movie_id, username),
  FOREIGN KEY (movie_id) REFERENCES movies_table(movie_id),
  FOREIGN KEY (username) REFERENCES users_table(username)
)

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

相关问题 从mysql中的不同表中获取2个主键 - To get 2 primary keys from different tables in mysql MySQL将一个表的主键添加到其他两个表 - Mysql add primary key from a table to others two tables 如何从由第三个表连接的两个mySQL表中选择数据? - How can I select data from two mySQL tables connected by a third table? 如何通过复合主键从两个表中获取 select a? - How to select a from two tables by composite primary key? 与主复合键的Zend表关系 - 从表中删除记录 - Zend Table Relationship with Primary Composite Key - delete record from table 如何从mysql表中获取自动递增字段名称或主键字段名称? - How can I get the auto incrementing field name or the primary key fieldname from a mysql table? MySQL报告主键,但是不能从表中删除它 - MySQL reports a primary key but can not drop it from the table 如何从其他表(其中主键是外键)添加动态文本到所显示的动态表? - How can I add dynamic text from a different table where primary key is a foreign key to adynamic table being shown? 如何在symfony2中使用其他2个表的主键创建表? - How do I create a table with the primary keys of 2 other tables in symfony2? 如何动态重命名我的数组键,以便该键可以用作表中的主键? - How do I dynamically rename my array keys so that the key can be used as a primary key in a table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM