mysql - ERROR 1005 (HY000): Can't create table? -
i have read through billion times , cannot figure out... generated in workbench should work creates of tables except sections... create if remove relationship between section , instructor need relationship work.... appreciated! code below... can me! thanks!
set @old_unique_checks=@@unique_checks, unique_checks=0; set @old_foreign_key_checks=@@foreign_key_checks, foreign_key_checks=0; set @old_sql_mode=@@sql_mode, sql_mode='traditional'; create schema if not exists `385_s11_turpinvp` default character set latin1 collate latin1_swedish_ci ; use `385_s11_turpinvp` ; -- ----------------------------------------------------- -- table `385_s11_turpinvp`.`tutors` -- ----------------------------------------------------- create table if not exists `385_s11_turpinvp`.`tutors` ( `name` varchar(45) not null , `banner_id` char(8) not null , `email` varchar(45) not null , `ssn` char(9) not null , `address` varchar(45) not null , `phone` int not null , primary key (`banner_id`) ) engine = innodb; create unique index `ssn_unique` on `385_s11_turpinvp`.`tutors` (`ssn` asc) ; create unique index `banner_id_unique` on `385_s11_turpinvp`.`tutors` (`banner_id` asc) ; -- ----------------------------------------------------- -- table `385_s11_turpinvp`.`students` - - ----------------------------------------------------- create table if not exists `385_s11_turpinvp`.`students` ( `name` varchar(45) not null , `banner_id` char(8) not null , `email` varchar(45) not null , `phone` int not null , primary key (`banner_id`) ) engine = innodb; create unique index `banner_id_unique` on `385_s11_turpinvp`.`students` (`banner_id` asc) ; -- ----------------------------------------------------- -- table `385_s11_turpinvp`.`instructors` -- ----------------------------------------------------- create table if not exists `385_s11_turpinvp`.`instructors` ( `name` varchar(45) not null , `banner_id` char(8) not null , `email` varchar(45) not null , primary key (`banner_id`, `name`) ) engine = innodb; create unique index `banner_id_unique` on `385_s11_turpinvp`.`instructors` (`banner_id` asc) ; -- ----------------------------------------------------- -- table `385_s11_turpinvp`.`courses` -- ----------------------------------------------------- create table if not exists `385_s11_turpinvp`.`courses` ( `course_id` varchar(45) not null , `course_name` varchar(45) not null , primary key (`course_id`) ) engine = innodb; -- ----------------------------------------------------- -- table `385_s11_turpinvp`.`appointments` -- ----------------------------------------------------- create table if not exists `385_s11_turpinvp`.`appointments` ( `courses_course_id` varchar(45) not null , `tutors_banner_id` char(8) not null , `students_banner_id` char(8) not null , `start_time` datetime not null , `end_time` datetime not null , primary key (`courses_course_id`, `tutors_banner_id`, `students_banner_id`, `start_time`, `end_time`) , constraint `fk_courses_has_tutors_courses1` foreign key (`courses_course_id` ) references `385_s11_turpinvp`.`courses` (`course_id` ) on delete cascade on update cascade, constraint `fk_courses_has_tutors_tutors1` foreign key (`tutors_banner_id` ) references `385_s11_turpinvp`.`tutors` (`banner_id` ) on delete cascade on update cascade, constraint `fk_appointments_students1` foreign key (`students_banner_id` ) references `385_s11_turpinvp`.`students` (`banner_id` ) on delete cascade on update cascade) engine = innodb; create index `fk_courses_has_tutors_tutors1` on `385_s11_turpinvp`.`appointments` (`tutors_banner_id` asc) ; create index `fk_courses_has_tutors_courses1` on `385_s11_turpinvp`.`appointments` (`courses_course_id` asc) ; create index `fk_appointments_students1` on `385_s11_turpinvp`.`appointments` (`students_banner_id` asc) ; -- ----------------------------------------------------- -- table `385_s11_turpinvp`.`qualified` -- ----------------------------------------------------- create table if not exists `385_s11_turpinvp`.`qualified` ( `courses_course_id` varchar(45) not null , `tutors_banner_id` char(8) not null , primary key (`courses_course_id`, `tutors_banner_id`) , constraint `fk_courses_has_tutors_courses2` foreign key (`courses_course_id` ) references `385_s11_turpinvp`.`courses` (`course_id` ) on delete cascade on update cascade, constraint `fk_courses_has_tutors_tutors2` foreign key (`tutors_banner_id` ) references `385_s11_turpinvp`.`tutors` (`banner_id` ) on delete cascade on update cascade) engine = innodb; create index `fk_courses_has_tutors_tutors2` on `385_s11_turpinvp`.`qualified` (`tutors_banner_id` asc) ; create index `fk_courses_has_tutors_courses2` on `385_s11_turpinvp`.`qualified` (`courses_course_id` asc) ; -- ----------------------------------------------------- -- table `385_s11_turpinvp`.`availability` -- ----------------------------------------------------- create table if not exists `385_s11_turpinvp`.`availability` ( `start_time` datetime not null , `end_time` datetime not null , `tutors_banner_id` char(8) not null , primary key (`tutors_banner_id`, `end_time`, `start_time`) , constraint `fk_availability_tutors1` foreign key (`tutors_banner_id` ) references `385_s11_turpinvp`.`tutors` (`banner_id` ) on delete cascade on update cascade) engine = innodb; -- ----------------------------------------------------- -- table `385_s11_turpinvp`.`sections` -- ----------------------------------------------------- create table if not exists `385_s11_turpinvp`.`sections` ( `courses_course_id` varchar(45) not null , `instructors_name` varchar(45) not null , `section_id` char(1) not null , primary key (`courses_course_id`, `instructors_name`, `section_id`) , constraint `fk_courses_has_enrolled_courses1` foreign key (`courses_course_id` ) references `385_s11_turpinvp`.`courses` (`course_id` ) on delete cascade on update cascade, constraint `fk_sections_instructors1` foreign key (`instructors_name` ) references `385_s11_turpinvp`.`instructors` (`name` ) on delete cascade on update cascade) engine = innodb; create index `fk_courses_has_enrolled_courses1` on `385_s11_turpinvp`.`sections` (`courses_course_id` asc) ; create index `fk_sections_instructors1` on `385_s11_turpinvp`.`sections` (`instructors_name` asc) ; -- ----------------------------------------------------- -- table `385_s11_turpinvp`.`enrolled` -- ----------------------------------------------------- create table if not exists `385_s11_turpinvp`.`enrolled` ( `students_banner_id` char(8) not null , `courses_course_id` varchar(45) not null , `sections_section_id` char(1) not null , primary key (`students_banner_id`, `courses_course_id`, `sections_section_id`) , constraint `fk_courses_has_students_students1` foreign key (`students_banner_id` ) references `385_s11_turpinvp`.`students` (`banner_id` ) on delete cascade on update cascade, constraint `fk_enrolled_courses1` foreign key (`courses_course_id` ) references `385_s11_turpinvp`.`courses` (`course_id` ) on delete cascade on update cascade, constraint `fk_enrolled_sections1` foreign key (`sections_section_id` ) references `385_s11_turpinvp`.`sections` (`section_id` ) on delete cascade on update cascade) engine = innodb; create index `fk_courses_has_students_students1` on `385_s11_turpinvp`.`enrolled` (`students_banner_id` asc) ; create index `fk_enrolled_courses1` on `385_s11_turpinvp`.`enrolled` (`courses_course_id` asc) ; c reate index `fk_enrolled_sections1` on `385_s11_turpinvp`.`enrolled` (`sections_section_id` asc) ; set sql_mode=@old_sql_mode; set foreign_key_checks=@old_foreign_key_checks; set unique_checks=@old_unique_checks;
to details on foreign key error, run show engine innodb status\g , @ "latest foreign key error" section.
it should tell foreign key invalid because there no unique index or primary key index on instructors.name. primary key instructors.(banner_id, name) , have unique index on instructors.banner_id. either need use 1 of column combinations foreign key, or add unique key on instructors.name
Comments
Post a Comment