我为此创建了一个模式:-
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`class_id` int(11) NOT NULL AUTO_INCREMENT,
`class_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`class_id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
/*Data for the table `class` */
insert into `class`(`class_id`,`class_name`) values (1,'A'),(2,'B'),(3,'C');
/*Table structure for table `info` */
DROP TABLE IF EXISTS `info`;
CREATE TABLE `info` (
`info_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
`class_id` int(11) DEFAULT NULL,
PRIMARY KEY (`info_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
/*Data for the table `info` */
insert into `info`(`info_id`,`user_id`,`parent_id`,`class_id`) values (1,3,2,1);
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) DEFAULT NULL,
`users_types_id` int(11) DEFAULT NULL,
`class_id` int(11) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
/*Data for the table `user` */
insert into `user`(`user_id`,`user_name`,`users_types_id`,`class_id`) values (1,'TeacherA',1,1),(2,'Parent',2,0),(3,'StudentA',3,1),(4,'TeacherB',1,2),(5,'TeacherC',1,3);
DROP TABLE IF EXISTS `users_types`;
CREATE TABLE `users_types` (
`users_types_id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(255) DEFAULT NULL,
PRIMARY KEY (`users_types_id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
/*Data for the table `users_types` */
insert into `users_types`(`users_types_id`,`type`) values (1,'Teacher'),(2,'Parent'),(3,'Student');
sqlfiddle在这里
同样在插入之前检查信息表中用户和父级的记录是否存在。