简体   繁体   中英

MySQL Create Table Error 1064

I am trying to create a table in MySQL but it doesn't want to play:

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)
);

And I am getting error:

"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"

Is this something simple as I am using incorrect parameters for the table settings?

When you use UNSIGNED you must put it right beside the data type, that is: INT UNSIGNED .

Your corrected CREATE statement should look like this:

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,
);

You may ask, "Why?" that's because there are two "types" of integer:

  • INT signed (which can store values from -2147483648 to 2147483647)
  • INT UNSIGNED (which can store values from 0 to 4294967295)

Reference:

Auto increment is an integer by default, no need to define unsigned.

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

try this

  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
);
  • no need for unsigned

  • use primary key in same line as trader(id)

here working demo

You have some MySQL syntax errors. Here's the fix:

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;

I would add the preferred engine, charset, collation and auto increment start number. If you'd like to do so just substitute the last line with:

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

And modify as desired. Otherwise leave the closing parenthesis.

);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM