gpt4 book ai didi

mysql - 如何使用 DEFAULT 约束为 mysql 中的列创建随机数?

转载 作者:行者123 更新时间:2023-11-29 05:14:13 26 4
gpt4 key购买 nike

DEFAULT 约束在接受字符串或当前日期值时没有问题。我需要的是一个约束,它会在每次创建实体时创建一个随机的 4 位数字。我尝试了以下代码,但它返回语法错误。

ALTER TABLE client_number ADD(code INT(4) DEFAULT FLOOR(RAND()*9999)+1111);

上述语句返回以下错误:

#1064 - 您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“floor(rand()*9999)+1111)”附近使用的正确语法

需要解决方案。

最佳答案

documentation很清楚:

The DEFAULT value clause in a data type specification indicates adefault value for a column. With one exception, the default value mustbe a constant; it cannot be a function or an expression.

你的表达式不是常量,所以它不起作用。您需要改用触发器。

关于mysql - 如何使用 DEFAULT 约束为 mysql 中的列创建随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35380406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com