只是做一个需要通过 AJAX 重新排序多个项目的网站。
当我在数据库中创建给定对象时,我想在创建时将位置列设置为行主键的值。
请问我该怎么做?
最佳答案
来自 MySQL documentation 的引文:
The DEFAULT clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column. See Section 10.3.1.1, “TIMESTAMP Properties”.
您可以改为在选择时获取 id 值:
SELECT id, COALESCE(position, id) as position ...
关于sql - Mysql设置默认值为项目自动递增键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2714787/