我在创建表时遇到这些错误,它们在我取出外键时起作用。我对构建数据库相对较新,所以我可能会遗漏一些简单的
Create Table IF NOT EXISTS Classroom_Address_Mailing_Tbl (
ID int auto_increment primary key,
Street_Address varchar (25),
City varchar (25),
State_Abbr varchar (2),
Zipcode varchar (10)
);
Create Table IF NOT EXISTS Classroom_Address_Physical_Tbl (
ID int auto_increment primary key,
Street_Address varchar (25),
City varchar (25),
State_Abbr varchar (2),
Zipcode varchar (10)
);
Create Table IF NOT EXISTS Classroom_Info_Tbl (
ID int auto_increment primary key,
School_Name varchar (25),
Classroom_Name_Offical varchar (25),
Classroom_URL varchar (25),
Classroom_Address_Physical_ID int,
Classroom_Address_Mailing_ID int,
foreign key (Classroom_Address_Mailing_ID) references
Classroom_Address_Mailing_Tbl (ID) on delete cascade,
foreign key (Classroom_Address_Physical_ID) references
Classroom_Address_Physical_Tbl (ID) on delete cascade,
);
Create Table IF NOT EXISTS Contact_Info_Tbl (
Classroom_ID int,
Contact_Name varchar (20),
Contact_Title varchar (20),
Contact_Email varchar (20),
Contact_Phone varchar (20),
foreign key (Classroom_ID) references
Classroom_Info_Tbl (ID) on delete cascade
);
这些是我得到的错误:
错误1064(42000):您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第7行的')'附近使用正确的语法
ERROR 1005(HY000):无法创建表'neClassCertForm.Contact_Info_Tbl'(错误号:150)
答案 0 :(得分:0)
好的问题是创建表中的逗号
Classroom_Address_Physical_Tbl (ID) on delete cascade,
这打破了表创建。还要添加ENGINE = innodb以强制执行外键约束。
Create Table IF NOT EXISTS `Classroom_Address_Mailing_Tbl` (
ID int auto_increment primary key,
Street_Address varchar (25),
City varchar (25),
State_Abbr varchar (2),
Zipcode varchar (10)
)ENGINE=InnoDB;
Create Table IF NOT EXISTS `Classroom_Address_Physical_Tbl` (
ID int auto_increment primary key,
Street_Address varchar (25),
City varchar (25),
State_Abbr varchar (2),
Zipcode varchar (10)
)ENGINE=InnoDB;
Create Table IF NOT EXISTS `Classroom_Info_Tbl` (
ID int auto_increment primary key,
School_Name varchar (25),
Classroom_Name_Offical varchar (25),
Classroom_URL varchar (25),
Classroom_Address_Physical_ID int,
Classroom_Address_Mailing_ID int,
foreign key (Classroom_Address_Mailing_ID) references
Classroom_Address_Mailing_Tbl (ID) on delete cascade,
foreign key (Classroom_Address_Physical_ID) references
Classroom_Address_Physical_Tbl (ID) on delete cascade
)ENGINE=InnoDB;
Create Table IF NOT EXISTS `Contact_Info_Tbl` (
Classroom_ID int,
Contact_Name varchar (20),
Contact_Title varchar (20),
Contact_Email varchar (20),
Contact_Phone varchar (20),
index ci (Classroom_ID),
foreign key (Classroom_ID) references
Classroom_Info_Tbl (ID) on delete cascade
)ENGINE=InnoDB;