I need help to decide if the relationship between this 2 table is one to many or many to many. One student can sign up for many class and one class can have multiple student. This is my table below:
Thankyou
I need help to decide if the relationship between this 2 table is one to many or many to many. One student can sign up for many class and one class can have multiple student. This is my table below:
Thankyou
This is how it should be:
classes to students is in HABTM Relationship maped by classes_students. Below is schema:
CREATE TABLE `students` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `classes` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`subject` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `classes_students` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`class_id` INT(11) NOT NULL,
`student_id` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX `FK__classes` (`class_id`),
INDEX `FK__students` (`student_id`),
CONSTRAINT `FK__classes` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`),
CONSTRAINT `FK__students` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`)
)
COMMENT='table to support HABT classes to students'
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
This is a many-to-many relationship. You need to have Students and Classes into separate tables and connect them via a junction table which will contain a student_id and a class_id.