1

I have a table schema that looks like this:

mysql> desc category;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| name        | varchar(255) | NO   |     | NULL    |                |
| create_date | datetime     | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

I have tried to insert data and I continue to get this error:

mysql> INSERT INTO category (id, name, create_date) VALUES (NULL, Family Events, 2016-25-12 00:00:00), (NULL, Work Events, 2016-25-12 00:00:00);
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 'Events, 2016-25-12 00:00:00), (NULL, Work Events, 2016-25-12 00:00:00)' at line 1

I have tried several other ways with the same result and I haven't found any good documentation.

I tried this:

mysql> INSERT INTO category(id, name, create_date) VALUES(NULL, 'Family Events', 'NOW()');

and got this error:

ERROR 1292 (22007): Incorrect datetime value: 'NOW()' for column 'create_date' at row 1
Daniel
  • 14,004
  • 16
  • 96
  • 156

1 Answers1

1

As the OP agreed with my answer, I am adding it. The problem with the procedure you follow is, you should surround your string values with ':

-- If the column is a `DATETIME`, then use:
VALUES (NULL, 'Family Events', NOW()),
//------------^-------------^
-- If the column is a `TIMESTAMP`, then use:
VALUES (NULL, 'Family Events', CURRENT_TIMESTAMP),
//------------^-------------^

Also I am using CURRENT_TIMESTAMP for the timestamp insertion alternatively to NOW().

One more thing that others have pointed out is, you don't need to add the extra id in your INSERT as it automatically does it based on PRIMARY KEY and since it takes NULL values, you can get rid of it completely.

Your final SQL should look like:

INSERT INTO category(name, create_date) VALUES('Family Events', CURRENT_TIMESTAMP);
Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252