Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ALTER TABLE not letting me set NULL or default value?

Tags:

mysql

I am trying to change an existing column in a table I have to allow for null values and then set the default value to null. I tried running the following but it does not seem to be updating the table:

mysql> ALTER TABLE answers_form MODIFY sub_id int unsigned NULL DEFAULT NULL;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc answers_form;
+--------------+------------------+------+-----+---------+-------+
| Field        | Type             | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| answer_id    | int(10) unsigned | NO   | PRI | 0       |       |
| sub_id       | int(10) unsigned | NO   | PRI | 0       |       |
| form_id      | int(10) unsigned | NO   | PRI | NULL    |       |
| value        | varchar(255)     | NO   |     | NULL    |       |
| non_response | bit(1)           | YES  |     | b'0'    |       |
+--------------+------------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

Can anyone see what I am doing wrong here?

like image 300
Abe Miessler Avatar asked May 08 '13 21:05

Abe Miessler


People also ask

How do you allow NULL to alter a table?

ALTER TABLE table_name ALTER COLUMN column_name DATA_TYPE [(COLUMN_SIZE)] NULL; In this syntax: First, specify the name of the table from which you want to change the column. Second, specify the column name with size which you want to change to allow NULL and then write NULL statement .

Can a default value be NULL?

If no default value is declared explicitly, the default value is the null value. This usually makes sense because a null value can be considered to represent unknown data. In a table definition, default values are listed after the column data type.

How do you make a column default to NULL?

From the table view, switch to the database structure at the bottom. Select the column you want to modify. Select column_defaul and choose NULL. Remember to hit Cmd + S to commit changes to the server.


1 Answers

its a primary key , mysql doesn't allow any part of the primary key to be null, which does make the fact that it allows a default value of null for the form_id odd, however the docs at

http://dev.mysql.com/doc/refman/5.5/en/optimizing-primary-keys.html

say "Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values".

Just out of curiosity, does it actually allow you to put in null values in the form_id field?

like image 113
JustDanyul Avatar answered Sep 30 '22 12:09

JustDanyul