MYSQL:两个字段作为主键 + 1 字段为唯一,一个问题
我有两个主(复合)键,分别指的是商店和分店。 我想我应该为每一行使用一个相应的ID,所以我添加了一个UNIQUE + AUTO_INCRMENT命名的ID。
所以我在表中有一个名为 ID(自动增量)的列,但它被声明为 PRIMARY - 这是自动完成的,我不希望 ID 为 PRIMARY。只有商店和分店。
我已经学会了如何欺骗 MYSQL 将 ID 字段接受为 UNIQUE 和 AUTO INCRMENT,因为将 AUTO_INCRMENT 设为 AUTO_INCRMENT 并不是一件很简单的事(它希望将其设为 PRIMARY)。 我必须删除 ID 字段(由于某种原因,它不允许我删除其主索引),然后声明它为索引,然后才声明为自动增量。
这是一个好方法吗? 我在这个设计中可能做错了什么吗?
谢谢 !!!
I have two primary (composite) keys that refer to a shop and a branch.
I thought I should have used a corresponding ID for each row, so I added a UNIQUE + AUTO_INCREMENT named ID.
So I had on the table a column named ID (AUTO INCREMENT), but it was declared PRIMARY - which was done automatically, and I don't want the ID to be PRIMARY. Just the shop and branch.
I have learnt how to trick MYSQL to accept the ID field as UNIQUE and AUTO INCREMENT, as it was not extremely trivial to make the AUTO_INCREMENT (it wanted to make it PRIMARY).
I had to ERASE the ID Field (for some reason it didn't let me erase its PRIMARY index), then declare it INDEX, and only then AUTO INCREMENT.
Is that a good approach ?
Could there be something I am doing wrong going with this design ?
Thanks !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
普遍的看法是每个表都应该有一个名为 Id 的唯一自动编号列。
在 Codd 和 Date 开发的经典数据建模中,ID 字段对于数据的完整逻辑模型来说并不是必需的。
ID 字段有什么用处?您是否曾通过 ID 引用此表中的行?如果从来没有,那么就把这个字段排除在外。 (商店、分店)提供了一个完美的PK候选人。
The prevailing wisdom is that every table should have a unique autonumbered column named Id.
In classical data modeling, as developed by Codd and Date, the ID field is not necessary for a complete logical model of the data.
What good does the ID field do you? Do you ever reference a row in this table by its ID? If never, then just leave the field out. (shop, branch) provided a perfectly good candidate to be the PK.
您的
create table
语句是什么样的?因为我想象的是这样的:What did your
create table
statement look like? Because I imagine this: