MySQL唯一ID或组合ID [英] MySQL unique id or combined id
问题描述
我的项目和开发人员程序具有以下结构:
I have the following structure for my project and developer program:
developer table
id
developer name
etc...
project table
id
project name
etc...
developer _project table
???
因为一个开发人员可以在多个项目中,而一个项目可以有多个开发人员,所以我必须创建一个新表,但是从我的阅读中得知,他们没有告诉我应该使用什么ID.
Because a developer can be in multiple projects and a projects can have multiple developer I have to create a new table but from what I read is they don't tell what id should I use.
如果我使用id自动递增+ user_id + project_id,因为id是主键,我会重复吗?
If I use an id auto increment + user_id + project_id will I have duplicate since the id is the primary key?
推荐答案
使用唯一的组合键:
CREATE TABLE `developer_project` (
developer_id INT(10) UNSIGNED /* etc... */,
project_id INT(10) UNSIGNED /* etc... */,
PRIMARY KEY dev_project (developer_id, project_id)
);
如果创建ID
,则可能永远不会使用它,因为您将在LEFT JOIN
If you create an ID
you will probably never use it since you will query the developer_id and/or project_id in your LEFT JOIN
注意:确保列定义与developer
和project
表相同.
NOTE: make sure the column definitions are the same as the developer
and the project
tables.
这篇关于MySQL唯一ID或组合ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!