Jump to content
Paul-D

Can't get this SQL code to insert

Recommended Posts

DROP TABLE IF EXISTS `rain-accounts`;
CREATE TABLE IF NOT EXISTS `rain-accounts` (
  `rainID` varchar(10) DEFAULT NULL,
  `AccountDate` varchar(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 

INSERT INTO rain-accounts (rainID , AccountDate) VALUES ('5b6acefb' , '2018-08-08');
 

It was originaly

INSERT INTO rain-accounts (rainID , date) VALUES ('5b6acefb' , '2018-08-08'); But I thought that you may not be able to have a field called date.

Share this post


Link to post
Share on other sites
Posted (edited)

The problem is your table name. To be more precise, the "-" in the name casue it to be interpreted as "rain minus accounts".

You need backticks around the name (or avoid hyphens in favour of underscores in identifiers).

Use DATE type, not varchar, for dates. Even though "date" is keyword, and better avoided, it would have been accepted as it is not reserved.

mysql> DROP TABLE IF EXISTS `rain-accounts`;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> CREATE TABLE IF NOT EXISTS `rain-accounts` (
    ->   `rainID` varchar(10) DEFAULT NULL,
    ->   `AccountDate` varchar(10) DEFAULT NULL
    -> ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.15 sec)

mysql> INSERT INTO rain-accounts (rainID , AccountDate) VALUES ('5b6acefb' , '2018-08-08');
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 '-accounts (rainID , AccountDate) VALUES ('5b6acefb' , '2018-08-08')' at line 1

 

Edited by Barand

Share this post


Link to post
Share on other sites

Consider a simple underscore instead of the hyphen. Or nothing at all and just have it as one word.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.