我可以从 2 个不同的 table 的主键为第三个 table 创建复合键吗? MySQL支持吗?
Can I create a composite key for a third table from the primary keys of 2 different tables? Does MySQL support it?
我有 3 个不同的 tables:
审查 table-> 这有 mov_id 作为主键。
users_table -> 这将用户名作为主键。
review_table -> 我可以根据 movie_id 和用户名为这个 table 创建一个复合主键吗?
users table
reviews table
comments table
我无法连接这 3 个 table,我也不知道为什么。
This is the error I'm getting while creating the comments table
您可以使用外键(来自其他 tables 的主键)创建复合主键,您没有什么特别的事情要做。只需约束外键并将它们声明为主键即可。
根据您提供的 table 的名称和列,这里是创建您想要的 table 的查询:
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)
)
我有 3 个不同的 tables:
审查 table-> 这有 mov_id 作为主键。 users_table -> 这将用户名作为主键。 review_table -> 我可以根据 movie_id 和用户名为这个 table 创建一个复合主键吗?
users table
reviews table
comments table
我无法连接这 3 个 table,我也不知道为什么。
This is the error I'm getting while creating the comments table
您可以使用外键(来自其他 tables 的主键)创建复合主键,您没有什么特别的事情要做。只需约束外键并将它们声明为主键即可。
根据您提供的 table 的名称和列,这里是创建您想要的 table 的查询:
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)
)