views:

48

answers:

2

Let's say there's a table created as follows:

create table testTable ( colA int not null )

How would you drop the not null constraint? I'm looking for something along the lines of

ALTER TABLE testTable ALTER COLUMN colA DROP NOT NULL;

which is what it would look like if I used PostgreSQL. To my amazement, as far as I've been able to find, the MySQL docs, Google and yes, even Stackoverflow (in spite of dozens or hundreds of NULL-related questions) don't seem to lead towards a single simple SQL statement which will do the job.

+2  A: 

In MySQL, nullability is a part of the datatype, not a constraint. So:

ALTER TABLE testTable ALTER COLUMN colA int null; 
Seva Alekseyev
`alter table x alter column a int null` returns "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 'int null' at line 1 [SQL State=42000, DB Errorcode=1064]"I also tried `alter table testTable modify a int default null;` and `alter table x alter column a int default null`: the result is the same. I'm using mysql 5.1.
Tomislav Nakic-Alfirevic
+1  A: 

I would try something like this

ALTER TABLE testTable MODIFY COLUMN colA int;
devil.morpheus
For some reason, `alter table testTable modify col int default null` didn't appear to work when I tried it: the above is in fact the right statement, although withouth the single quotes (I'm removing them to match what worked).
Tomislav Nakic-Alfirevic