我可以在MySql中使用默认值的函数吗?

时间:2021-04-18 17:11:05

I want to do something like this:

我想做这样的事情:


create table app_users
(
    app_user_id smallint(6) not null auto_increment primary key,
    api_key     char(36) not null default uuid()
);

However this results in a error, is there a way to call a function for a default value in mysql?

但是这会导致错误,有没有一种方法可以调用mysql中的默认值的函数?

thanks.

谢谢。

4 个解决方案

#1


98  

No, you can't.

不,你不能。

However, you could easily create a trigger to do this, such as:

但是,您可以很容易地创建一个触发器来执行此操作,例如:

CREATE TRIGGER before_insert_app_users
  BEFORE INSERT ON app_users 
  FOR EACH ROW
  SET new.api_key = uuid();

#2


16  

As already stated you can't.

如前所述,你不能。

If you want to simulate this behavior you can use a trigger in this way:

如果你想模拟这种行为,你可以这样使用触发器:

CREATE TRIGGER before_insert_app_users
BEFORE INSERT ON app_users
FOR EACH ROW
  IF new.uuid IS NULL
  THEN
    SET new.uuid = uuid();
  END IF;

You still have to update previously existing rows, like this:

您仍然需要更新以前存在的行,比如:

UPDATE app_users SET uuid = (SELECT uuid());

#3


8  

Unfortunately no, MySQL 5 requires constants for the default. The issue was discussed in much more detail in the link below. But the only answer is to allow null and add a table trigger.

不幸的是,MySQL 5要求默认的常量。这个问题在下面的链接中有更详细的讨论。但是唯一的答案是允许null并添加一个表触发器。

MySQL only recently accepted UUID as part of their DB package, and it's not as feature rich as we'd like.

MySQL最近才接受UUID作为其DB包的一部分,而且它并没有我们想要的那么丰富。

http://www.phpbuilder.com/board/showthread.php?t=10349169

http://www.phpbuilder.com/board/showthread.php?t=10349169

#4


5  

I believe you can't:

我相信你不能:

the default value must be a constant; it cannot be a function or an expression

默认值必须是常量;它不能是一个函数或表达式。

#1


98  

No, you can't.

不,你不能。

However, you could easily create a trigger to do this, such as:

但是,您可以很容易地创建一个触发器来执行此操作,例如:

CREATE TRIGGER before_insert_app_users
  BEFORE INSERT ON app_users 
  FOR EACH ROW
  SET new.api_key = uuid();

#2


16  

As already stated you can't.

如前所述,你不能。

If you want to simulate this behavior you can use a trigger in this way:

如果你想模拟这种行为,你可以这样使用触发器:

CREATE TRIGGER before_insert_app_users
BEFORE INSERT ON app_users
FOR EACH ROW
  IF new.uuid IS NULL
  THEN
    SET new.uuid = uuid();
  END IF;

You still have to update previously existing rows, like this:

您仍然需要更新以前存在的行,比如:

UPDATE app_users SET uuid = (SELECT uuid());

#3


8  

Unfortunately no, MySQL 5 requires constants for the default. The issue was discussed in much more detail in the link below. But the only answer is to allow null and add a table trigger.

不幸的是,MySQL 5要求默认的常量。这个问题在下面的链接中有更详细的讨论。但是唯一的答案是允许null并添加一个表触发器。

MySQL only recently accepted UUID as part of their DB package, and it's not as feature rich as we'd like.

MySQL最近才接受UUID作为其DB包的一部分,而且它并没有我们想要的那么丰富。

http://www.phpbuilder.com/board/showthread.php?t=10349169

http://www.phpbuilder.com/board/showthread.php?t=10349169

#4


5  

I believe you can't:

我相信你不能:

the default value must be a constant; it cannot be a function or an expression

默认值必须是常量;它不能是一个函数或表达式。