Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL throwing syntax error while trying to alter a table

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think it has to be like this now:

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

Note the parenthesis, or (curdate())

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading