MySQL - 您可以检索列的默认值吗?
我正在考虑创建一个 TRIGGER,如果 INSERT 值恰好是空字符串,它将将列的值设置为其默认值.
I was looking at creating a TRIGGER that will set the value of a column to its DEFAULT if the INSERT value happens to be an empty string.
我的触发器如下所示:
CREATE TRIGGER column_a_to_default
BEFORE INSERT ON table
FOR EACH ROW
BEGIN
IF NEW.a = '' THEN
SET NEW.a = 'some value';
END IF;
END
我想知道是否可以将某个值"替换为将其设置为列的默认值的方法.即
I would like to know if I can replace 'some value' with a way to set it to the DEFAULT value of the column. i.e.
SET NEW.a = a.DEFAULT
谢谢
推荐答案
这应该适合你
SET NEW.a = DEFAULT(NEW.a)
好像不行.
使用此解决方法
IF NEW.a = '' THEN
SELECT COLUMN_DEFAULT INTO @def
FROM information_schema.COLUMNS
WHERE
table_schema = 'database_name'
AND table_name = 'your_table'
AND column_name = 'a';
SET NEW.a = @def;
END IF;
你也可以试试
SET NEW.a = DEFAULT(table_name.a)
相关文章