MYSQL - set default as NULL to all columns, where default is not set

前端 未结 2 947
迷失自我
迷失自我 2021-01-25 05:45

I have about 12 databases, each with 50 tables and most of them with 30+ columns; the db was running in strict mode as OFF, but now we had to migrate the db to cleardb service w

2条回答
  •  北海茫月
    2021-01-25 06:19

    This is what I came up with on the base of @Ollie-jones script

    https://gist.github.com/brijrajsingh/efd3c273440dfebcb99a62119af2ecd5

    SELECT CONCAT_WS('.',TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME) col,CONCAT('alter table ',TABLE_NAME,' MODIFY COLUMN ', COLUMN_NAME,' ',DATA_TYPE,'(',CHARACTER_MAXIMUM_LENGTH,') NULL DEFAULT NULL') as script_col
      FROM information_schema.COLUMNS
     WHERE is_nullable=0 
     and length(COLUMN_DEFAULT) is NULL and
     CHARACTER_MAXIMUM_LENGTH is not NULL and 
     table_schema = 'immh' 
     Union
     SELECT CONCAT_WS('.',TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME) col,CONCAT('alter table ',TABLE_NAME,' MODIFY COLUMN ', COLUMN_NAME,' ',DATA_TYPE,' NULL DEFAULT NULL') as script_col
      FROM information_schema.COLUMNS
     WHERE is_nullable=0 
     and length(COLUMN_DEFAULT) is NULL and
     CHARACTER_MAXIMUM_LENGTH is NULL and 
     table_schema = 'immh' 
    

提交回复
热议问题