我使用SQL创建了以下表格,以便导入MySQL 5.5.24数据库。
CREATE TABLE customers (
'custId' INT NOT NULL AUTO_INCREMENT,
'custName' NOT NULL VARCHAR(80),
'custfirmName' VARCHAR(80),
'custAddress' NOT NULL VARCHAR(80),
'custAddress2' VARCHAR(45),
'custCity' NOT NULL VARCHAR(45),
'custState' NOT NULL CHAR(2),
'custZip' NOT NULL CHAR(10),
'custPhone' NOT NULL VARCHAR(12),
'custEmail' NOT NULL VARCHAR(45),
'custWebsite' VARCHAR(45),
PRIMARY KEY(custId)
);
我有两个资源,它们表明我的语法正确,但我在客户表上收到错误消息:
#1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在''custId'附近使用正确的语法INT NOT NULL AUTO_INCREMENT,'custName'NOT NULL VARCHAR(80),'c'在第2行
我做错了什么?
答案 0 :(得分:1)
尝试不带引号并将NOT NULL
放在每个字段声明的末尾:
CREATE TABLE customers
(
custid INT NOT NULL auto_increment,
custname VARCHAR(80) NOT NULL,
custfirmname VARCHAR(80),
custaddress VARCHAR(80) NOT NULL,
custaddress2 VARCHAR(45),
custcity VARCHAR(45) NOT NULL,
custstate CHAR(2) NOT NULL,
custzip CHAR(10) NOT NULL,
custphone VARCHAR(12) NOT NULL,
custemail VARCHAR(45) NOT NULL,
custwebsite VARCHAR(45),
PRIMARY KEY(custid)
);
或使用反引号(允许字段名称中的空格):
CREATE TABLE customers
(
`custid` INT NOT NULL auto_increment,
`custname` VARCHAR(80) NOT NULL,
`custfirmname` VARCHAR(80),
`custaddress` VARCHAR(80) NOT NULL,
`custaddress2` VARCHAR(45),
`custcity` VARCHAR(45) NOT NULL,
`custstate` CHAR(2) NOT NULL,
`custzip` CHAR(10) NOT NULL,
`custphone` VARCHAR(12) NOT NULL,
`custemail` VARCHAR(45) NOT NULL,
`custwebsite` VARCHAR(45),
PRIMARY KEY(custid)
);
请参阅fiddle。
答案 1 :(得分:1)
试试这个:
CREATE TABLE customers (
`custId` INT NOT NULL AUTO_INCREMENT,
`custName` VARCHAR(80) NOT NULL,
`custfirmName` VARCHAR(80),
`custAddress` VARCHAR(80) NOT NULL,
`custAddress2` VARCHAR(45),
`custCity` VARCHAR(45) NOT NULL,
`custState` CHAR(2) NOT NULL,
`custZip` CHAR(10) NOT NULL,
`custPhone` VARCHAR(12) NOT NULL,
`custEmail` VARCHAR(45) NOT NULL,
`custWebsite` VARCHAR(45),
PRIMARY KEY(custId)
);
以上使用MySQL Workbench进行了测试。
答案 2 :(得分:0)
对列名称使用反引号: -
CREATE TABLE customers (
`custId` INT NOT NULL AUTO_INCREMENT,
`custName` VARCHAR(80) NOT NULL,
`custfirmName` VARCHAR(80),
`custAddress` VARCHAR(80) NOT NULL,
`custAddress2` VARCHAR(45),
`custCity` VARCHAR(45) NOT NULL,
`custState` CHAR(2) NOT NULL,
`custZip` CHAR(10) NOT NULL,
`custPhone` VARCHAR(12) NOT NULL,
`custEmail` VARCHAR(45) NOT NULL,
`custWebsite` VARCHAR(45),
PRIMARY KEY(custId)
);