MySQL-设置动态默认值 [英] MySQL - set dynamic default value
本文介绍了MySQL-设置动态默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的MySQL数据库中有用户表.是否可以将 profile_url
列的默认值设置为 Id
列的值?
I have table of users in my MySQL database. Is it possible to set default value of column profile_url
to value of column Id
?
推荐答案
您可以使用 trigger
来实现该功能.例如:
You could use trigger
to implement the feature. For example:
CREATE TRIGGER defaultUrl
BEFORE INSERT ON MyTable
FOR EACH ROW
SET NEW.profile_url = IFNULL(NEW.profile_url, NEW.id);
该表达式创建一个名为 defaultUrl
的触发器,该触发器在插入 MyTable
的每一行之前被激活.如果 defaultUrl
为 NULL
,则使用 id
值.
The expression creates a trigger named defaultUrl
that activates before each row inserted into MyTable
. If defaultUrl
is NULL
, the id
value is used.
这篇关于MySQL-设置动态默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文