3

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.

1
  • none is not any special value in MySQL of which I'm aware. Can you add sample data which explains what you are trying to do here? Commented Mar 27, 2019 at 10:35

3 Answers 3

0

you can alter column

ALTER TABLE table_name
 MODIFY COLUMN col datatype  DEFAULT null
Sign up to request clarification or add additional context in comments.

2 Comments

Actually the column can be NULL, I just need to set the default value to NULL
I'm sure he knows how to alter one column to have default NULL. He's asking how to do this FOR ALL columns in ALL tables
0

The current accepted answer literally just tells you now to set a single column's default value to NULL. I'm sure you already knew how to do that?

I believe though that you're asking how to set the default value of every single column in every single table in the database which does not already have a default value of NULL, in one go, to save time.

You can do the following

SELECT 
  CONCAT(
    'ALTER TABLE ', table_name, 
    ' MODIFY COLUMN `', column_name, '` ', 
    data_type, 
    IF(data_type IN ('varchar', 'char'), CONCAT('(', character_maximum_length, ')'), ''),
    ' NULL DEFAULT NULL;'
  ) AS alter_statement
FROM information_schema.columns
WHERE table_schema = 'YOUR_DATABASE_NAME'
  AND column_default IS NULL
  AND is_nullable = 'YES';

This will return an alter statement for every column which does not have a default value, you can copy the statements, use search and replace to remove the quotes around the strings. Now you have probably hundreds of alter statements ready to fire off. You can for example paste them into SQLWorkbench and click "run" and it will run them all and update everything in under a second.

Note that it will also give alter statements for columns which already have a default value of NULL, as well as the requirement of columns which don't have any default value. But since there's no harm of altering a column to have a default value of NULL if it already has a default value of NULL, then that doesn't matter.

Look over the alter statements first though to make sure it all looks ok.

Comments

-1

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.

3 Comments

I would like to update all the columns withouta default value (Default value = 'None') in one time, instead of making one query for each column of each table. I need a quicker way I could say.
You can't do this without using some dynamic SQL.
Your second query is setting column value to null instead of setting its default value to null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.