sql - Mysql设置默认值为项目自动递增键

标签 sql mysql

只是做一个需要通过 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/

相关文章:

mysql - 更新 sql 查询 where select max

SQL 先进先出忠诚点

MYSQL 查询返回特定父项的所有后代

c# - MYSQL Connector.net 连接长时间后丢失

mysql - 当部分迁移失败时,为什么不是 "undo"?

mysql - SQL 连接两个查询,同一个表

mysql - 集团每月及城市数据

MySQL在一列中选择具有不同值的相同ID的不同行并排除另一列中的特定值

SQL SELECT 多列合二为一

MySQL SELECT 部分匹配来自其他 TABLE 的字符串的行