mysql - 如何在mysql中使用函数uuid()设置默认值?

标签 mysql

我想通过函数 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/

相关文章:

mysql - 将 MSSQL 函数转换为 MySQL 函数

php - 来自Mysql数据库的表名列表

mysql - 在不区分大小写的情况下,是否有任何排序规则来区分 'е' 和 'ё'?

mysql - 根据日期选择不起作用

php - 在 'country_id' 子句中获取列 'where' 是不明确的 - cakephp 1.3 中的搜索查询错误

Java:Callablestatement.prepareCall 在 SELECT 中变成 CALL

mysql - 'root' @'localhost'(使用密码 : NO) - can't access anything in mysql

php - 我是否正确实现了我的 DAO?

php - 如果表中不存在 userID,则添加 mysql 行

mysql - 安装 MySQLdb Python 架构错误