mySQL date column is messed up
I've made a table:
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| date | date | NO | | NULL | |
| user_id | int(8) | YES | | NULL | |
| isbn | varchar(13) | NO | | NULL | |
| price | decimal(6,2) | NO 开发者_JAVA百科 | | NULL | |
+---------+--------------+------+-----+---------+----------------+
And, the date colomn isn't working. I figured I need to modify the table to default to "CURDATE()". That's what I've got from googling. I'm not even sure if that's the problem, but my alter statement isn't work.
I'm trying to alter with this:
mysql> ALTER TABLE buybacks MODIFY date date not null 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
Can someone help?
It's generally not a good idea to have column (or table) names that are MySQL reserved words. If you must do this, then use backticks (`) around the name
ALTER TABLE buybacks MODIFY `date` date not null default curdate();
EDIT
However, you can't set a date column to a default of curdate() See this reference from the MySQL buglist for details
The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column. This might be a viable alternative to give you that default
EDIT 2
Another alternative would be a trigger:
CREATE TRIGGER buybacks_insert BEFORE INSERT ON `buybacks`
FOR EACH ROW
SET NEW.`date` = CURDATE();
But watch out that you update a trigger when you do change the name of the date column
精彩评论