mysql - Error (1005) when trying to create foreign key -
i'm trying create basic foreign constraint, i'm getting syntax error. #1005 - can't create table 'my_database'.'#sql-334f_952bc' (errno: 150 "foreign key constraint incorrectly formed")
i first create tables , use 'alter table' method create foreign constraint.
creating tables:
create table `tbl_flights` ( `flight_id` int(11) not null auto_increment `aircraft_id` int(11) not null `date` date not null `auth_by` varchar(255) not null `auth_duration` time not null primary key (`flight_id`) ) ; create table `tbl_aircraft` ( `aircraft_id` int(11) not null auto_increment `registration` char(6) not null `insurance` date not null `awrc` date not null primary key (`aircraft_id`) ) ;
creating foreign key/constraint:
alter table `tbl_aircraft` add constraint `fk_aircraft_id` foreign key ( `aircraft_id` ) references `my_database`.`tbl_flights` ( `aircraft_id` ) on delete restrict on update cascade ;
if identify issue here appreciate it.
base in error message missed ; @ end of statement
create table `tbl_flights` ( `flight_id` int(11) not null auto_increment `aircraft_id` int(11) not null `date` date not null `auth_by` varchar(255) not null `auth_duration` time not null primary key (`flight_id`) ) ; /* add */ create table `tbl_aircraft` ( `aircraft_id` int(11) not null auto_increment `registration` char(6) not null `insurance` date not null `awrc` date not null primary key (`aircraft_id`) ) ; /* add this*/
could have inverted tables
alter table `tbl_flights` add constraint `fk_aircraft_id` foreign key ( `aircraft_id` ) references `my_database`.`tbl_aircraft` ( `aircraft_id` ) on delete restrict on update cascade ;
or must add foreign column in table tbl_aircraft
Comments
Post a Comment