1

我正在尝试在 MySQL 中创建一个表,但它不想玩:

create table traders(
    traderID INT(9) ZEROFILL NOT NULL AUTO_INCREMENT UNSIGNED,
    traderProfileName VARCHAR(64) NOT NULL,
    traderPassword CHAR(128) NOT NULL,
    traderFirstName VARCHAR(40) NOT NULL,
    traderSurname VARCHAR(40) NOT NULL,
    traderContactPhone VARCHAR(14) NOT NULL,
    locationPostCode CHAR(4) NOT NULL,
    traderEmail VARCHAR(120) NOT NULL,
    traderBio VARCHAR(255) DEFAULT NULL,
    traderReviewRating DECIMAL(5,2) DEFAULT NULL,
    traderLastLogin DATETIME DEFAULT NULL,
    PRIMARY_KEY(traderID)
);

我收到错误:

"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 'UNSIGNED,
traderProfileName VARCHAR(64) NOT NULL,
traderPassword CHAR(128) NOT ' at line 2"

这很简单,因为我为表格设置使用了不正确的参数?

4

4 回答 4

1

使用UNSIGNED时必须放在数据类型旁边,即:INT UNSIGNED.

您更正后的CREATE陈述应如下所示:

create table traders(
    traderID INT(9) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY,
--           ^^^^^^^^^^^^^^^ Here is the problem.
--  Also, you can define this column as a primary key +---^^^^^^^^^^^
--  directly in the column definition                 |
    traderProfileName VARCHAR(64) NOT NULL,
    traderPassword CHAR(128) NOT NULL,
    traderFirstName VARCHAR(40) NOT NULL,
    traderSurname VARCHAR(40) NOT NULL,
    traderContactPhone VARCHAR(14) NOT NULL,
    locationPostCode CHAR(4) NOT NULL,
    traderEmail VARCHAR(120) NOT NULL,
    traderBio VARCHAR(255) DEFAULT NULL,
    traderReviewRating DECIMAL(5,2) DEFAULT NULL,
    traderLastLogin DATETIME DEFAULT NULL,
);

你可能会问:“为什么?” 那是因为整数有两种“类型”:

  • INT签名(可以存储从 -2147483648 到 2147483647 的值)
  • INT UNSIGNED(可以存储从 0 到 4294967295 的值)

参考:

于 2015-02-03T18:25:14.873 回答
0

尝试这个

  create table traders
(
traderID INT(9) ZEROFILL NOT NULL  AUTO_INCREMENT PRIMARY KEY,
traderProfileName VARCHAR(64) NOT NULL,
traderPassword CHAR(128) NOT NULL,
traderFirstName VARCHAR(40) NOT NULL,
traderSurname VARCHAR(40) NOT NULL,
traderContactPhone VARCHAR(14) NOT NULL,
locationPostCode CHAR(4) NOT NULL,
traderEmail VARCHAR(120) NOT NULL,
traderBio VARCHAR(255) DEFAULT NULL,
traderReviewRating DECIMAL(5,2) DEFAULT NULL,
traderLastLogin DATETIME DEFAULT NULL
);
  • 不需要未签名

  • 在同一行中使用与交易者(id)相同的主键

这里工作演示

于 2013-10-14T19:41:30.530 回答
0

自动增量默认为整数,无需定义无符号。

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

于 2013-10-14T19:33:36.497 回答
-1

您有一些 MySQL 语法错误。这是修复:

CREATE TABLE IF NOT EXISTS `traders` (
`traderID` INT(9) NOT NULL AUTO_INCREMENT,
`traderProfileName` VARCHAR(64) NOT NULL,
`traderPassword` CHAR(128) NOT NULL,
`traderFirstName` VARCHAR(40) NOT NULL,
`traderSurname` VARCHAR(40) NOT NULL,
`traderContactPhone` VARCHAR(14) NOT NULL,
`locationPostCode` CHAR(4) NOT NULL,
`traderEmail` VARCHAR(120) NOT NULL,
`traderBio` VARCHAR(255) DEFAULT NULL,
`traderReviewRating` DECIMAL(5,2) DEFAULT NULL,
`traderLastLogin` DATETIME DEFAULT NULL,
PRIMARY KEY (`traderID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;

我会添加首选引擎、字符集、排序规则和自动递增起始编号。如果您想这样做,只需将最后一行替换为:

) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;

并根据需要进行修改。否则留下右括号。

);
于 2013-10-14T19:50:12.783 回答