mysql - Linking two tables in one through foreign keys -


i´m trying link 2 tables 1 reference table,so took primary keys of students , programs , placed them in table classstudent can relate student program, keep getting error:

cannot add or update child row:a foreign key constraint fails(crsdb.classstudent,constraint classstudent_ibfk_1 foreign key(programs_id)referenceprograms(id)on delete no action on update no action).

please suggestion highly appreciated.

create table if not exists `crsdb`.`students` (   `id` decimal(10,0) null default null,   `name` text null default null,    primary key (`id`)) engine = myisam default character set = latin1;  create table if not exists `crsdb`.`programs` (   `id` int null auto_increment,   `name` text null,   primary key (`id`)) engine = myisam default character set = latin1;  create table if not exists `crsdb`.`classstudent` (   `id` int not null auto_increment,   `class_id` int null,   `programs_id` int not null,   `students_id` decimal(10,0) not null,   primary key (`id`),   index `fk_classstudent_programs1_idx` (`programs_id` asc),   index `fk_classstudent_students1_idx` (`students_id` asc),   constraint `fk_classstudent_programs`     foreign key (`programs_id`)     references `crsdb`.`programs` (`id`)     on delete no action     on update no action,   constraint `fk_classstudent_students`     foreign key (`students_id`)     references `crsdb`.`students` (`id`)     on delete no action     on update no action) engine = innodb; 

in order add value classstudent table have have valid records in both students , programs table.

if students table looks this:

id     name ----  ---------- 1      george 2      samuel 

and programs table looks this

id     name ----  ---------- 1      history 2      science 

you can add values classstudent table match id's of foreign key fields:

id     class_id   programs_id   students_id ----  ---------- ------------- ------------- 1      ?          1             1 2      ?          1             2 3      ?          2             1 4      ?          2             3   <====will fail 

i don't know class_id field supposed be, don't see constraint on it.

since column values dependent on other tables values, cannot add value doesn't exist.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -