MySQL将默认值设置为项目自动增量键

编程入门 行业动态 更新时间:2024-10-13 10:29:12
本文介绍了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.

请问我该怎么做?

推荐答案

MySQL文档:

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将默认值设置为项目自动增量键

本文发布于:2023-07-15 13:06:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1111394.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:增量   设置为   默认值   项目   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!