导入时MySQL数据库代码出错

时间:2016-10-08 08:32:04

标签: mysql

我的代码:

CREATE TABLE IF NOT EXISTS `friends` (
  `Id`         INT(10)   NOT NULL AUTO_INCREMENT,
  `providerid` INT(10)   NOT NULL AUTO_INCREMENT,
  `requestid`  INT(10)   NOT NULL AUTO_INCREMENT,
  `status`     BINARY(1) NOT NULL,
  PRIMARY KEY (`Id`)
);

CREATE TABLE IF NOT EXISTS `messages` (
  `Id`          INT(255)                    NOT NULL AUTO_INCREMENT,
  `fromuid`     INT(255)                    NOT NULL,
  `touid`       INT(255)                    NOT NULL,
  `sentdt`      DATETIME                    NOT NULL,
  `read`        TINYINT(1)                  NOT NULL DEFAULT '0',
  `readdt`      DATETIME                             DEFAULT NULL,
  `messagetext` LONGTEXT CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`Id`)
);


CREATE TABLE IF NOT EXISTS `users` (
  `Id`                 INT(10) UNSIGNED    NOT NULL AUTO_INCREMENT,
  `username`           VARCHAR(45)         NOT NULL DEFAULT '',
  `password`           VARCHAR(32)         NOT NULL DEFAULT '',
  `email`              VARCHAR(45)         NOT NULL DEFAULT '',
  `date`               DATETIME            NOT NULL DEFAULT '0000-00-00 00:00:00',
  `status`             TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
  `authenticationTime` DATETIME            NOT NULL DEFAULT '0000-00-00 00:00:00',
  `userKey`            VARCHAR(32)         NOT NULL DEFAULT '',
  `IP`                 VARCHAR(45)         NOT NULL DEFAULT '',
  `port`               INT(10) UNSIGNED    NOT NULL DEFAULT '0',
  PRIMARY KEY (`Id`)
);
  

错误:#1064 - 您的SQL语法出错;检查手册   对应于您的MySQL服务器版本,以获得正确的语法   在'CREATE TABLE IF NOT NOT EXISTS messages附近使用(       Id int(255)NOT NULL AUTO_INCREMENT,'第8行

所以plz帮我纠正代码......

谢谢

1 个答案:

答案 0 :(得分:0)

您的脚本中存在少量错误。

1)只能有一个自动增量键,必须将其定义为键

2)

DEFAULT 的第三个查询中拼写错误
    `email` varchar(45) NOT NULL DEFAULT '',

3)DEFAULT关键字在第3个查询中使用两次以下字段,并且对于NOT NULL缺少NULL

`authenticationTime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',

CORRECT查询: -

CREATE TABLE IF NOT EXISTS `friends` (
    `Id` int(10) NOT NULL AUTO_INCREMENT,
    `providerid` int(10) NOT NULL ,
    `requestid` int(10) NOT NULL ,
    `status` binary(1) NOT NULL ,
    PRIMARY KEY (`Id`));

CREATE TABLE IF NOT EXISTS `messages` (
    `Id` int(255) NOT NULL AUTO_INCREMENT,
    `fromuid` int(255) NOT NULL,
    `touid` int(255) NOT NULL,
    `sentdt` datetime NOT NULL,
    `read` tinyint(1) NOT NULL DEFAULT '0', 
    `readdt` datetime DEFAULT NULL,
    `messagetext` longtext CHARACTER SET utf8 NOT NULL ,
    PRIMARY KEY (`Id`)
);


CREATE TABLE IF NOT EXISTS `users` ( 
    `Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `username` varchar(45) NOT NULL DEFAULT '',
    `password` varchar(32) NOT NULL DEFAULT '',
    `email` varchar(45) NOT NULL DEFAULT '',
    `date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `status` tinyint(3) unsigned NOT NULL DEFAULT '0',
    `authenticationTime` datetime NOT NULL  DEFAULT CURRENT_TIMESTAMP,
    `userKey` varchar(32) NOT NULL DEFAULT '',
    `IP` varchar(45) NOT NULL DEFAULT '',
    `port` int(10) unsigned NOT NULL DEFAULT '0',
    PRIMARY KEY (`Id`)
)