Home > Enterprise >  MySQL throwing syntax error while trying to alter a table
MySQL throwing syntax error while trying to alter a table

Time:11-21

I have been trying to alter a table to include a date column with default value of CURDATE() but MySQL is constantly throwing syntax error. Now, I have checked syntax for altering a table from several sources but I believe I do not have any syntax error. When I remove the default value part, the query runs fine but for some reason it cannot add a default value for the date column. I don't know why that is the case.

The code:

mysql> describe test;
 ------- -------------- ------ ----- --------- ------- 
| Field | Type         | Null | Key | Default | Extra |
 ------- -------------- ------ ----- --------- ------- 
| col1  | int          | YES  |     | 0       |       |
| col2  | varchar(100) | YES  |     | hello   |       |
| col3  | varchar(5)   | YES  |     | T       |       |
 ------- -------------- ------ ----- --------- ------- 
3 rows in set (0.02 sec)

mysql> ALTER TABLE test ADD COLUMN col4 DATE DEFAULT CURDATE();
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 'CURDATE()' at line 1
mysql>

Edit: My MySQL version: 8.0.31

CodePudding user response:

I think it has to be like this now:

ALTER TABLE test ADD COLUMN col4 DATE DEFAULT (CURRENT_DATE);

Note the parenthesis, or (curdate())

  • Related