Mysql setting default value to be items auto increment key
Just doing a site which requires multiple items to be reordered by AJAX.
When I create a given object in the data开发者_运维知识库base, I want to set the position column to the value of the rows' primary key upon creation.
How would I do this please?
A citation from 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”.
You can though obtain the id value on selection instead:
SELECT id, COALESCE(position, id) as position ...
精彩评论