尝试使用外键创建一些新表但是我被抓住了,这是代码和我收到的错误,我认为它有一些东西 用我的外键做什么?
#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 '(`CustomerID`),
) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1' at line 10
DROP TABLE IF EXISTS `Customer`;
CREATE TABLE `Customer` (
`CustomerID` INT UNSIGNED AUTO_INCREMENT,
`Customer_Number` VARCHAR(100),
`Customer_Name` VARCHAR(100),
`Website` VARCHAR(255),
`Logo` VARCHAR(100),
PRIMARY KEY(`CustomerID`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
DROP TABLE IF EXISTS `Reports`;
CREATE TABLE `Reports` (
`ReportsID` INT UNSIGNED AUTO_INCREMENT,
`Role` VARCHAR(70),
`Region` VARCHAR(70),
`Inpection_Type` VARCHAR(70),
`CustomerID` INT UNSIGNED,
`Report_Date` DATE NOT NULL DEFAULT '0000-00-00',
`Order_Date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Customer_Name` VARCHAR(100),
`Customer_Division` VARCHAR(70),
`Memo` VARCHAR(255),
`Billing_Key` VARCHAR(70),
PRIMARY KEY(`ReportsID`),
FOREIGN KEY (`CustomerID`) REFERENCES Customer(`CustomerID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
DROP TABLE IF EXISTS `CustomerContact`;
CREATE TABLE `CustomerContact` (
`ContactID` INT UNSIGNED AUTO_INCREMENT,
`CustomerID` INT UNSIGNED,
`Division` VARCHAR(100),
`Contact` VARCHAR(100),
`Address` VARCHAR(255),
`Phone` VARCHAR(100),
`Fax` VARCHAR(100),
`Email` VARCHAR(100),
`Mobile` VARCHAR(100),
PRIMARY KEY(`ContactID`),
FOREIGN KEY (`CustomerID) REFERENCES Customer(`CustomerID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
答案 0 :(得分:1)
徽标列后面缺少逗号,外键定义中CustomerID列名称末尾缺少反引号。
修复这些并重新运行DDL后,您会发现CustomerContact表没有名为CustomerID的列,所以您也应该添加它。
DROP TABLE IF EXISTS `Customer`;
CREATE TABLE `Customer` (
`CustomerID` INT UNSIGNED AUTO_INCREMENT,
`Customer_Number` VARCHAR(100),
`Customer_Name` VARCHAR(100),
`Website` VARCHAR(255),
`Logo` VARCHAR(100),
PRIMARY KEY(`CustomerID`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
DROP TABLE IF EXISTS `CustomerContact`;
CREATE TABLE `CustomerContact` (
`ContactID` INT UNSIGNED AUTO_INCREMENT,
`Division` VARCHAR(100),
`Contact` VARCHAR(100),
`Address` VARCHAR(255),
`Phone` VARCHAR(100),
`Fax` VARCHAR(100),
`Email` VARCHAR(100),
`Mobile` VARCHAR(100),
PRIMARY KEY(`ContactID`),
CONSTRAINT FOREIGN KEY (`CustomerID`) REFERENCES Customer(`CustomerID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
答案 1 :(得分:0)
你错过了一个逗号:
`Logo` VARCHAR(100),
PRIMARY KEY(`CustomerID`)
等
最后还有一个回拨:
CONSTRAINT FOREIGN KEY (`CustomerID`) REFERENCES Customer(`CustomerID`) ON DELETE CASCADE ON UPDATE CASCADE