我想通过函数 uuid() 将 uuid 值设置为“id”字段。 而且我不想用作触发器。
CREATE TABLE `test` (
`id` VARCHAR(36) NOT NULL DEFAULT uuid(),
`username` VARCHAR(250) NULL DEFAULT NULL,
`values` VARCHAR(250) NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB;
最佳答案
规范对此非常明确:http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
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.
因此,要么使用触发器,预先计算值(例如在 php 中),要么使用其他数据库,例如oracle 可能会支持。
关于mysql - 如何在mysql中使用函数uuid()设置默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17589391/