简体   繁体   中英

SQL: Set default value to NULL for all columns without default value

The webhosting I use has enabled StrictMode for the Databases. All my php scripts now stopped working because they report I haven't defined a default value for some columns.

As I have a lot of columns in a lot of tables, is there a way to set all the columns with "default value = none" with "default value = NULL" ? In this way it won't report me the error anymore.

Of course, If there's another (better) way, I am available for it.

I tried looking on the net, but I couldn't find anything suitable for this case.

you can alter column

ALTER TABLE table_name
 MODIFY COLUMN col datatype  DEFAULT null

A general approach here which should work for each column causing an error would be to set a default value, and then maybe do an update to backfill records missing a value.

ALTER TABLE yourTable ALTER some_text_column SET DEFAULT 'None';

And here is the update:

UPDATE yourTable SET some_text_column = 'None' WHERE some_text_column IS NULL;

You are not required to do this update, but it might make sense to bring older records missing values in line with what newer records would look like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM