如何向链接表添加和填充序列列
假设我有一个如下所示的表:
create table filetype_filestatus (
id integer(11) not null auto_increment,
file_type_id integer(11) not null,
file_status_id integer(11) not null,
)
我想添加一个序列列,如下所示:
alter table filetype_filestatus add column sequence integer(11) not null;
alter table filetype_filestatus add unique key idx1 (file_type_id, file_status_id, sequence);
现在我想添加该列,这很简单,并用一些满足唯一键的默认值填充它。
序列列允许用户任意排序特定文件类型的文件状态的显示。我不太关心初始订单,因为可以在应用程序中对其进行修改。
理想情况下,我最终会得到这样的结果:
FileType FileStatus Sequence
1 1 1
1 2 2
1 3 3
2 2 1
2 2 2
我能想到的最好的结果是:
update filetype_filestatus set sequence = file_type_id * 1000 + file_status_id;
有更好的方法吗?
Assuming I have a table like the one below:
create table filetype_filestatus (
id integer(11) not null auto_increment,
file_type_id integer(11) not null,
file_status_id integer(11) not null,
)
I want to add a sequence column like so:
alter table filetype_filestatus add column sequence integer(11) not null;
alter table filetype_filestatus add unique key idx1 (file_type_id, file_status_id, sequence);
Now I want to add the column, which is straightforward, and populate it with some default values that satisfy the unique key.
The sequence column is to allow the user to arbitrarily order the display of file_status for a particular file_type. I'm not too concerned by the initial order since that can be revised in the application.
Ideally I would end up with something like:
FileType FileStatus Sequence
1 1 1
1 2 2
1 3 3
2 2 1
2 2 2
The best I can think of is something like:
update filetype_filestatus set sequence = file_type_id * 1000 + file_status_id;
Are there better approaches?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,我相信这应该可行:
我建议将新列添加到表中,运行
alter table
语句(并获取默认值 0),运行 update 语句,然后 添加约束(好吧,无论如何你都必须这样做)。任何被触及的内容都会更新为大于 0 的序列,因此也可以安全地运行多次。编辑:
正如@Dems所指出的,子查询是在更新之前运行的,因此上面的内容实际上并不能用于此目的。它确实适用于单行插入(这在这里根本没有帮助)。
编辑:
啊,你有一个
id
列,这工作得很好(是的,我首先测试了这个)。但不知道对性能的影响。
Hmm, I believe this should work:
I'd recommend adding the new column to the table, running the
alter table
statement (and getting the default of 0), run the update statement, then add the constraint (well, you have to anyways). Anything that gets touched updates to asequence
greater than 0, so this can be safely run multiple times, too.EDIT:
As @Dems has pointed out, the subquery is being run before the update, and so the above doesn't actually work for this purpose. It does work on single-line inserts (which doesn't help at all here).
EDIT:
Gah, you have an
id
column, this works just fine (and yes, I tested this one first).Don't know about the performance implications, though.
如果您需要的只是“一些符合 idx1 的值”,为什么不直接复制 id 字段呢?毕竟,它是独一无二的...
编辑
如何根据所提出问题的操作更改来获取顺序值。
ROW_NUMBER() 在 MySQL 中不可用,而且我的理解是,您也不能使用源查询中正在更新的表。
If all you need are "some values that conform to
idx1
", why not just copy the id field? It is, after all, unique...EDIT
How to get sequential values based on the OPs changes to the question being asked.
ROW_NUMBER() is not available in MySQL, and it is also my understanding that you can't use the table being updated in the source query as well.