你可以帮我解决这些SQL问题吗?

时间:2011-07-21 07:45:02

标签: php mysql phpmyadmin

嗨,我正在使用phpmyadmin 3.3.9。我有一个SQL问题,其中我根本不知道什么是问题..

这里的代码如下:

CREATE TABLE metars(
metar varchar( 255 ) NOT NULL default '',
timestamp timestamp( 14 ) NOT NULL ,
station varchar( 4 ) NOT NULL,
PRIMARY KEY ( station ) ,
UNIQUE KEY station( station )
) ENGINE = MYISAM 

并且发生了错误:

#1064 - 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 '( 14 ) NOT NULL , station varchar( 4 ) NOT NULL default '', PRIMARY KE' at line 3

3 个答案:

答案 0 :(得分:4)

使用timestamp timestamp NOT NULL ,来尝试 时间戳没有长度/格式选项(即:不适用于mysql 5.1,请参阅ypercube的评论)

请参阅http://dev.mysql.com/doc/refman/5.1/en/create-table.html

data_type:
    BIT[(length)]
  | TINYINT[(length)] [UNSIGNED] [ZEROFILL]
  | SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
  | MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
  | INT[(length)] [UNSIGNED] [ZEROFILL]
  | INTEGER[(length)] [UNSIGNED] [ZEROFILL]
  | BIGINT[(length)] [UNSIGNED] [ZEROFILL]
  | REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
  | DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
  | FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
  | DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL]
  | NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL]
  | DATE
  | TIME
  | TIMESTAMP
  | DATETIME
  | YEAR
  | CHAR[(length)]
 [...]

但我会避免使用关键字和/或类型名称作为标识符。

答案 1 :(得分:3)

'timestamp'是MySQL的关键字。因此,将列名'timestamp'更改为other或将其包装为grave(`)。而且您不必设置时间戳类型的列长度。

PRIMARY KEY也意味着独特性。因此,此处不需要UNIQUE KEY站(站)。

CREATE TABLE metars(
metar varchar( 255 ) NOT NULL default '',
`timestamp` timestamp,
station varchar( 4 ) NOT NULL,
PRIMARY KEY ( station )
) ENGINE = MYISAM 

答案 2 :(得分:0)

CREATE TABLE metars(
`metar` varchar( 255 ) NOT NULL default '',
`timestamp` timestamp( 14 ) NOT NULL ,
`station` varchar( 4 ) NOT NULL,
PRIMARY KEY ( `station` ) ,
UNIQUE KEY station( `station` )
) ENGINE = MYISAM

这个应该是好的