1
我想運行5創建表語句。其中3人正在與我聲明的外鍵工作正常,但表'generalweather'和'userreg'正在拋出錯誤1005,我知道這與我的外鍵不匹配有關。我一直在尋找錯誤,但我無法找到任何地方。這兩個創建表語句有什麼問題? (我的默認模式名爲「測試儀」)錯誤1005拋出MySQL
CREATE TABLE `coastal` (
`Date` varchar(50) NOT NULL,
`Time` varchar(50) NOT NULL DEFAULT '',
`AM/PM` varchar(45) NOT NULL,
`Location` varchar(50) NOT NULL DEFAULT '',
`Tide Description` varchar(50) DEFAULT NULL,
`Estimated Swell(ft)` int(11) DEFAULT NULL,
`Wind Speed(mph)` int(11) DEFAULT NULL,
`Wind Direction` varchar(45) DEFAULT NULL,
`Visibility (%)` varchar(50) DEFAULT NULL,
PRIMARY KEY (`Date`,`Time`,`Location`,`AM/PM`),
CONSTRAINT `coastalFK`
FOREIGN KEY (Date, Time, Location, `AM/PM`)
REFERENCES tester.locations (Date, Time, Location, `AM/PM`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
INDEX `DateFK_idx` (`Date` ASC) ,
INDEX `TimeFK_idx` (`Time` ASC) ,
INDEX `AM/PMFK_idx` (`AM/PM` ASC) ,
INDEX `LocationFK_idx` (`Location` ASC)
);
CREATE TABLE `generalweather` (
`Date` varchar(50) NOT NULL,
`Location` varchar(50) NOT NULL,
`ClimateDescription` varchar(50) NOT NULL DEFAULT '',
`Average Temperature(F)` int(11) DEFAULT NULL,
`Elevation(ft)` varchar(50) DEFAULT NULL,
`Recommended Activity` varchar(50) DEFAULT NULL,
PRIMARY KEY (`Date`,`Location`,`ClimateDescription`),
CONSTRAINT `generalweatherFK`
FOREIGN KEY (Date, Location)
REFERENCES tester.locations (Date, Location)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
INDEX `DateFK_idx` (`Date` ASC) ,
INDEX `LocationFK_idx` (`Location` ASC)
);
CREATE TABLE `locations` (
`Date` varchar(50) NOT NULL DEFAULT '',
`Time` varchar(50) NOT NULL DEFAULT '',
`AM/PM` varchar(45) NOT NULL,
`Location` varchar(50) NOT NULL DEFAULT '',
`Location Type` varchar(50) DEFAULT NULL,
`Temperature (F)` varchar(45) DEFAULT NULL,
PRIMARY KEY (`Date`,`Time`,`Location`,`AM/PM`)
);
CREATE TABLE `precipitation` (
`Date` varchar(50) NOT NULL,
`Time` varchar(50) NOT NULL DEFAULT '',
`AM/PM` varchar(45) NOT NULL,
`Location` varchar(50) NOT NULL DEFAULT '',
`Precipitation Chance (%)` text,
`Wind Speed(mph)` int(11) DEFAULT NULL,
`Wind Direction` varchar(50) DEFAULT NULL,
`Visibility (%)` varchar(50) DEFAULT NULL,
`Humidity (%)` varchar(50) DEFAULT NULL,
PRIMARY KEY (`Date`,`Time`,`AM/PM`,`Location`),
CONSTRAINT `DateFK`
FOREIGN KEY (Date, Time, Location, `AM/PM`)
REFERENCES tester.locations (Date, Time, Location, `AM/PM`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
INDEX `DateFK_idx` (`Date` ASC) ,
INDEX `TimeFK_idx` (`Time` ASC) ,
INDEX `AM/PMFK_idx` (`AM/PM` ASC) ,
INDEX `LocationFK_idx` (`Location` ASC)
);
CREATE TABLE `userreg` (
`User Name` varchar(50) NOT NULL DEFAULT '',
`Real Name` varchar(50) DEFAULT NULL,
`Email address` varchar(50) DEFAULT 'NONE ON FILE',
`Location` varchar(50) NOT NULL,
PRIMARY KEY (`User Name`),
CONSTRAINT `userregFK`
FOREIGN KEY (`Location`)
REFERENCES tester.locations (`Location`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
INDEX `LocationFK_idx` (`Location` ASC)
);
這是因爲MySQL需要在引用表和引用表中的索引兩者兼而有之?如果我沒有記錯的話,主鍵將被自動編入索引,但如果被另一個表引用,仍然需要索引,對嗎? – Baelix
@Belix是的 - 我只是補充說。被引用表上必須有一個索引,它與引用表上的FK def中使用的列完全匹配,所以即使列包含在另一個組合索引中,您仍然需要自行索引該列。 –
明白了。這就是我感到困惑的地方。複合鍵對我來說總是有點棘手。謝謝。 (注意:如果從頭開始構建,此代碼可以工作,但在構建這個表之前構建任何其他表都會造成同樣的錯誤,可能是由於位置已經創建,沒有索引) – Baelix