I’m using MySQL: Server version: 8.0.36 Homebrew
I’m trying to remove the default value of a column (currently set to 0), and it’s erroring:
ALTER TABLE comments ALTER COLUMN commentable_id integer DROP DEFAULT;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'integer DROP DEFAULT' at line 1
The above is the syntax i’ve seen everywhere according to a google search. I also tried this which i saw on the O’Reilly site: (note no ‘COLUMN’)
ALTER TABLE comments ALTER commentable_id integer DROP DEFAULT;
but that didn’t work either.
If I set a default of null, then it worked:
ALTER TABLE comments MODIFY COLUMN commentable_id integer DEFAULT null;
and that is effectively the same, since my database’s "default default" is null anyway.
But, i’d still like to know why the first command didn’t work. Is it a change in V8 perhaps?
2
Answers
You have it almost right, but you can’t use the column type, so it is just
I would prefer using
instead of
When using the first method the table would have the following structure
Using the second method,
If I insert some data only on id column.
For example
With the first method I would have
with the second one an error will be thrown ,
See example