Can I, and, if I can, how can I set the default value of a field in a MySQL table to the value of another field?
我可以,如果可以的话,我如何将MySQL表中字段的默认值设置为另一个字段的值?
Thing is: I have data, and each data object has its ID in the table. But, I would like the possibility to rearrange the data, changing their sorting index, without altering their ID. Thus, the field sort_num
should by default be set to the value given to the auto-incremented indexed field ID
.
事情是:我有数据,每个数据对象在表中都有自己的ID。但是,我希望有可能重新排列数据,改变他们的排序索引,而不改变他们的ID。因此,默认情况下,字段sort_num应设置为赋予自动递增的索引字段ID的值。
Thanks in advance!
提前致谢!
2 个解决方案
#1
8
I see two possible solutions for this:
我看到两个可能的解决方案:
1. Possibility:
1.可能性:
You use a function to simply ignore sort_num
if it is not set:
如果未设置,则使用函数简单地忽略sort_num:
`SELECT * FROM mytable ORDER BY coalesce(sort_num, id)`
coalesce()
returns the first non-null value, therefore you would insert values for sort_num
if you really need to reorder items.
coalesce()返回第一个非null值,因此如果确实需要重新排序项,则应插入sort_num的值。
2. Possibility:
2.可能性:
You write a trigger, which automatically sets the value if it is not set in the insert statement:
您编写了一个触发器,如果未在insert语句中设置该值,则会自动设置该值:
DELIMITER //
CREATE TRIGGER sort_num_trigger
BEFORE INSERT ON mytable
FOR EACH ROW BEGIN
DECLARE auto_inc INT;
IF (NEW.sort_num is null) THEN
-- determine next auto_increment value
SELECT AUTO_INCREMENT INTO auto_inc FROM information_schema.TABLES
WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME = 'mytable';
-- and set the sort value to the same as the PK
SET NEW.sort_num = auto_inc;
END IF;
END
//
(inspired by this comment)
(受此评论的启发)
However, this might run into parallelization issues (multiple queries inserting at the same time)
但是,这可能会遇到并行化问题(同时插入多个查询)
#2
3
Its a bad idea to have an auto-increment column rearranged, hence better idea would be
将一个自动增量列重新排列是一个坏主意,因此更好的想法
Add a column sort_num to the table
向表中添加列sort_num
ALTER TABLE data ADD sort_num column-definition; (column definition same as ID)
UPDATE data SET sort_num = ID
Now play with sort_num column as it has no effect on column ID
现在使用sort_num列,因为它对列ID没有影响
#1
8
I see two possible solutions for this:
我看到两个可能的解决方案:
1. Possibility:
1.可能性:
You use a function to simply ignore sort_num
if it is not set:
如果未设置,则使用函数简单地忽略sort_num:
`SELECT * FROM mytable ORDER BY coalesce(sort_num, id)`
coalesce()
returns the first non-null value, therefore you would insert values for sort_num
if you really need to reorder items.
coalesce()返回第一个非null值,因此如果确实需要重新排序项,则应插入sort_num的值。
2. Possibility:
2.可能性:
You write a trigger, which automatically sets the value if it is not set in the insert statement:
您编写了一个触发器,如果未在insert语句中设置该值,则会自动设置该值:
DELIMITER //
CREATE TRIGGER sort_num_trigger
BEFORE INSERT ON mytable
FOR EACH ROW BEGIN
DECLARE auto_inc INT;
IF (NEW.sort_num is null) THEN
-- determine next auto_increment value
SELECT AUTO_INCREMENT INTO auto_inc FROM information_schema.TABLES
WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME = 'mytable';
-- and set the sort value to the same as the PK
SET NEW.sort_num = auto_inc;
END IF;
END
//
(inspired by this comment)
(受此评论的启发)
However, this might run into parallelization issues (multiple queries inserting at the same time)
但是,这可能会遇到并行化问题(同时插入多个查询)
#2
3
Its a bad idea to have an auto-increment column rearranged, hence better idea would be
将一个自动增量列重新排列是一个坏主意,因此更好的想法
Add a column sort_num to the table
向表中添加列sort_num
ALTER TABLE data ADD sort_num column-definition; (column definition same as ID)
UPDATE data SET sort_num = ID
Now play with sort_num column as it has no effect on column ID
现在使用sort_num列,因为它对列ID没有影响