Hi all. I'm developing a quiz application consisting of multiple choice questions. Here are the tables that I've come up with.
CREATE TABLE IF NOT EXISTS `student` (
`s_id` int(10) NOT NULL,
`s_name` varchar(30) NOT NULL,
`s_email` varchar(30) NOT NULL,
PRIMARY KEY (`s_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `instructor` (
`i_id` int(6) NOT NULL,
`i_name` varchar(30) NOT NULL,
`i_email` varchar(30) NOT NULL,
PRIMARY KEY (`i_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `result` (
`r_id` int(6) NOT NULL AUTO_INCREMENT,
`s_id` int(10) NOT NULL,
`i_id` int(6) NOT NULL,
`r_status` text NOT NULL,
`r_score` decimal(6,0) NOT NULL,
PRIMARY KEY (`r_id`),
UNIQUE KEY `s_id` (`s_id`),
UNIQUE KEY `i_id` (`i_id`)
ADD CONSTRAINT `result_ibfk_1` FOREIGN KEY (`s_id`) REFERENCES `student` (`s_id`),
ADD CONSTRAINT `result_ibfk_2` FOREIGN KEY (`i_id`) REFERENCES `instructor` (`i_id`);
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `eval_set` (
`set_id` int(11) NOT NULL AUTO_INCREMENT,
`r_id` int(11) NOT NULL,
`q_id` int(11) NOT NULL,
`response` varchar(255) NOT NULL,
`created` date NOT NULL,
PRIMARY KEY (`set_id`),
UNIQUE KEY `r_id` (`r_id`,`q_id`),
KEY `q_id` (`q_id`)
ADD CONSTRAINT `eval_set_ibfk_1` FOREIGN KEY (`r_id`) REFERENCES `result` (`r_id`),
ADD CONSTRAINT `eval_set_ibfk_2` FOREIGN KEY (`q_id`) REFERENCES `question` (`q_id`);
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `question` (
`q_id` int(10) NOT NULL AUTO_INCREMENT,
`a_id` int(6) NOT NULL,
`cr_id` int(6) NOT NULL,
`q_qstn_no` int(11) NOT NULL,
`q_text` varchar(100) NOT NULL,
`q_chpt` int(11) NOT NULL,
PRIMARY KEY (`q_id`),
UNIQUE KEY `a_id` (`a_id`,`cr_id`),
KEY `cr_id` (`cr_id`)
ADD CONSTRAINT `question_ibfk_2` FOREIGN KEY (`cr_id`) REFERENCES `chapter_rel` (`cr_id`),
ADD CONSTRAINT `question_ibfk_1` FOREIGN KEY (`a_id`) REFERENCES `answer` (`a_id`);
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
CREATE TABLE IF NOT EXISTS `answer` (
`a_id` int(6) NOT NULL AUTO_INCREMENT,
`q_id` int(10) NOT NULL,
`a_text` varchar(255) NOT NULL,
`a_value` tinyint(1) NOT NULL,
PRIMARY KEY (`a_id`),
UNIQUE KEY `q_id` (`q_id`)
ADD CONSTRAINT `answer_ibfk_1` FOREIGN KEY (`q_id`) REFERENCES `question` (`q_id`);
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `chapter_rel` (
`cr_id` int(6) NOT NULL AUTO_INCREMENT,
`q_id` int(10) NOT NULL,
`chapter_no` int(6) NOT NULL,
PRIMARY KEY (`cr_id`),
UNIQUE KEY `q_id` (`q_id`)
ADD CONSTRAINT `chapter_rel_ibfk_1` FOREIGN KEY (`q_id`) REFERENCES `question` (`q_id`);
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
There foreign key error occurred when I want to add q_text in 'question'. I'm aware there are two FKs (a_id and cr_id) whose parent tables are not yet populated. How do I go about it? The way I see it, I need to insert the questions first, then the multiple choice of answers and related chapters (hence the separate tables).
Would omitting the two FKs in 'question' and just have the q_id in 'answer' and 'chapter_rel' work? I'm using phpmyadmin and all the constraints are set to ON DELETE RESTRICT and ON UPDATE RESTRICT.
Hope someone can point me in the right direction. Thank you.
Regards,
Atikah