MySQL - Can you retrieve the default value of a column?

前端 未结 1 2019
盖世英雄少女心
盖世英雄少女心 2021-01-24 23:26

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.

My TRIGGER looks like this:

相关标签:
1条回答
  • 2021-01-25 00:00

    This should work for you

    SET NEW.a = DEFAULT(NEW.a)
    

    EDIT: It looks like that doesn't work.

    Use this workaround

    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;
    

    You can also try

    SET NEW.a = DEFAULT(table_name.a)
    
    0 讨论(0)
提交回复
热议问题