答案 0 :(得分:1)
应该是这样的:
学生的课程是由班级学生编写的HABTM关系。 下面是架构:
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
;
答案 1 :(得分:0)
这是many-to-many relationship。您需要将学生和班级分成不同的表格,并通过junction table连接它们,其中包含student_id和class_id。