Trying to create a few new tables with foreign keys but am getting caught up, here’s the code and the error I’m receiving, I think it has something to
do with my foreign key?
#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;
You’ve got a missing comma after the Logo column, and a missing backtick at the end of the CustomerID column name in the foreign key definition.
Once you fix those and re-run the DDL you will discover that the CustomerContact table does not have a column named CustomerID, so you should add that, too.