1

我正在尝试运行 5 个创建表语句。其中 3 个在我声明的外键上工作正常,但是表'generalweather''userreg'抛出错误 1005,我知道这与我的外键不匹配有关。我一直在寻找错误,但我无法在任何地方找到它。这两个创建表语句有什么问题?(我的默认架构名为“tester”)

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

1 回答 1

3

有几个问题。Locations您必须在引用它的任何其他表之前构建该表。所以把那个移到顶部。

然后,您需要为FOREIGN KEY您定义的每一个匹配索引。你有一个PRIMARY KEY复合

PRIMARY KEY (`Date`,`Time`,`Location`,`AM/PM`)

已经,所以你的一些FOREIGN KEY定义会起作用。但是,您需要为那些不使用完整组合的 FK 定义创建额外的索引PK。这是因为父表和引用表都必须在您用作FOREIGN KEY.

# Move this table definition to be first
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`),

  # Added these two keys for the later FK definitions
  # Referenced by generalweatherFK:
  INDEX (`Date`, `Location`),
  # Referenced by userregFK
  INDEX (`Location`)
);

这是正确构建的整个过程:http ://sqlfiddle.com/#!2/d208e

于 2013-04-18T18:20:07.627 回答