MySQL - set dynamic default value [duplicate]

本小妞迷上赌 提交于 2021-01-29 07:23:33

问题


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 ?


回答1:


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);

The expression creates a trigger named defaultUrl that activates before each row inserted into MyTable. If defaultUrl is NULL, the id value is used.



来源:https://stackoverflow.com/questions/53250756/mysql-set-dynamic-default-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!