sql - Mysql foreign key constraint is incorrectly formed? -


i receiving error when attempting create tables in mysql foreign key

create table session ( code char(2) not null, date date, room varchar(30) null, constraint session_pk primary key (date), constraint session_fk foreign key (code)      references module(code));  create table module ( code char(2) not null, name varchar(30) not null, cost decimal(8,2) not null, credits tinyint not null, course_code char(3) not null, constraint module_pk primary key (code)); 

here 2 tables trying create, syntax i've used matches w3 schools , both data types same cannot see how incorrect, appreciated :)

you're trying create foreign key on table before creating referencing table.

interchanging order of query work :

create table module ( `code` char(2) not null, name varchar(30) not null, cost decimal(8,2) not null, credits tinyint not null, course_code char(3) not null, constraint module_pk primary key (`code`));  create table `session` ( `code` char(2) not null, `date` date, room varchar(30) null, constraint session_pk primary key (`date`), constraint session_fk foreign key (`code`)  references module(`code`)); 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -