MySQL将默认值设置为项目自动增量键 [英] Mysql setting default value to be items auto increment key

查看:118
本文介绍了MySQL将默认值设置为项目自动增量键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需做一个需要多个项目由AJAX重新排序的站点.

Just doing a site which requires multiple items to be reordered by AJAX.

当我在数据库中创建给定对象时,我想在创建时将position列设置为行的主键的值.

When I create a given object in the database, I want to set the position column to the value of the rows' primary key upon creation.

请问我该怎么做?

推荐答案

DEFAULT子句为列指定默认值.除一个例外,默认值必须为常数;否则为0.它不能是函数或表达式.例如,这意味着您不能将日期列的默认值设置为诸如NOW()或CURRENT_DATE之类的函数的值.唯一的例外是,您可以将CURRENT_TIMESTAMP指定为TIMESTAMP列的默认值.请参见第10.3.1.1节"TIMESTAMP属性".

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值:

You can though obtain the id value on selection instead:

SELECT id, COALESCE(position, id) as position ...

这篇关于MySQL将默认值设置为项目自动增量键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆