[英]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 和用户名为这个表创建一个复合主键吗?
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.