sql - Error 1005 thrown in MySQL -
i'm trying run 5 create tables statements. 3 of them working fine foreign keys i've declared, tables 'generalweather' , 'userreg' throwing error 1005, know has mismatch in foreign keys. i've been looking error, can't find anywhere. wrong 2 create table statements? (my default schema titled "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) );
there couple of problems. must build locations
table before other tables reference it. move 1 top.
then, need matching index every foreign key
define. have composite primary key
of
primary key (`date`,`time`,`location`,`am/pm`)
already, of foreign key
definitions work. however, need create additional indexes fk definitions don't use full composite pk
. due fact both parent , referencing table must have index defined across complete set of columns use foreign key
.
# move table definition 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 2 keys later fk definitions # referenced generalweatherfk: index (`date`, `location`), # referenced userregfk index (`location`) );
here's whole thing building properly: http://sqlfiddle.com/#!2/d208e
Comments
Post a Comment