mysql - ERROR 1064 (42000) when using keywords as column name

走远了吗. 提交于 2019-12-01 09:37:11

问题


What's wrong with this? Ran this successfully on a Gentoo system, but now on a Debian-Squeeze (Raspberry PI) it won't work.

Database is setup allright

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| arduino1           |
| mysql              |
| performance_schema |
| test               |
| tmp                |
+--------------------+
6 rows in set (0.01 sec)

mysql>

Command is:

#mysql -u root -p******* arduino1 < arduino-tables.sql

Resulting in:

ERROR 1064 (42000) at line 1: 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 '(8),
    currentTime DATETIME,
    timeDiff INT(10),
    unixTime INT(10),
    currentR1 FL' at line 3

Content of arduino-tables.sql:

#cat arduino-tables.sql:

CREATE TABLE pulseLog (
    id INT(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
    timeStamp TIMESTAMP(8),
    currentTime DATETIME,
    timeDiff INT(10),
    unixTime INT(10),
    currentR1 FLOAT,
    currentS2 FLOAT,
    currentT3 FLOAT,
    currentAverageR1 FLOAT,
    currentAverageS2 FLOAT,
    currentAverageT3 FLOAT,
    temp0 FLOAT,
    temp1 FLOAT,
    temp2 FLOAT,
    temp3 FLOAT,
    temp4 FLOAT,
    temp5 FLOAT,
    pulses INT,
    event char(255),
 ) CHARACTER SET UTF8;

回答1:


There where some typo errors, like timestamp is a key word, you had an extra comma after event char(255),.

Try this:

    CREATE TABLE pulseLog (
    id INT(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
    `timeStamp` TIMESTAMP,
    `currentTime` DATETIME,
    `timeDiff` INT(10),
    `unixTime` INT(10),
    `currentR1` FLOAT,
    `currentS2` FLOAT,
    `currentT3` FLOAT,
    `currentAverageR1` FLOAT,
    `currentAverageS2` FLOAT,
    `currentAverageT3` FLOAT,
    `temp0` FLOAT,
    `temp1` FLOAT,
    `temp2` FLOAT,
    `temp3` FLOAT,
    `temp4` FLOAT,
    `temp5` FLOAT,
    `pulses` INT,
    `event` char(255)
 ) CHARACTER SET UTF8;

Here is the SQL Fiddle DEMO

Edit:

Apart from that your syntax for timestamp was not supported. For reference on date, datetime and timestamp check here




回答2:


you are using keyword that is a datatype. you can do that by escaping it using backtick example

CREATE TABLE pulseLog (
    id INT(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
    `timeStamp` TIMESTAMP(8),
    `currentTime` DATETIME,
    `timeDiff` INT(10),
    `unixTime` INT(10),
    currentR1 FLOAT,
    currentS2 FLOAT,
    currentT3 FLOAT,
    currentAverageR1 FLOAT,
    currentAverageS2 FLOAT,
    currentAverageT3 FLOAT,
    temp0 FLOAT,
    temp1 FLOAT,
    temp2 FLOAT,
    temp3 FLOAT,
    temp4 FLOAT,
    temp5 FLOAT,
    pulses INT,
    event char(255),
 ) CHARACTER SET UTF8;


来源:https://stackoverflow.com/questions/12367145/mysql-error-1064-42000-when-using-keywords-as-column-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!