我正在尝试创建一些非常简单的表,其中一个表与另一个表具有FK关系。但是MySQL无法识别存在的正确创建的表。我使用的是MacOS 10.14,并使用了DMG用于MySQLWorkBench和MySQL Community Server v8
已经采取的步骤: -通过MySQLWorkBench停止/重启MySQL 通过MacOS系统偏好设置停止/重启MySQL -下降表 -删除数据库 -运行mysqlcheck -四重检查所有拼写/格 -重新配置查询结构以逐步完成
/* Creating the building_t table */
CREATE TABLE building_t (
buildingkey VARCHAR(10),
buildingname VARCHAR(50),
buildingaddress VARCHAR(150),
buildingcity VARCHAR(50),
buildingstate VARCHAR(2),
buildingzipcode INT,
buildingmanagerphone INT,
PRIMARY KEY (buildingkey)
);
结果:
16:43:17创建表building_t(buildingkey VARCHAR(10),
建筑名称VARCHAR(50),建筑地址VARCHAR(150),
buildingcity VARCHAR(50),buildingstate VARCHAR(2),
buildingzipcode INT,buildingmanagerphone INT,PRIMARY KEY (buildingkey))的0行受到影响0.0083秒
下一步:
/* Creating the apartment_t table */
CREATE TABLE apartment_t (
apartmentkey VARCHAR(10),
apartmentrooms VARCHAR(50),
buildingkey VARCHAR(10),
PRIMARY KEY (apartmentkey),
FOREIGN KEY (buildingkey) REFERENCES building_t(buildingkey)
);
结果:
16:45:44创建表apartment_t((apartmentkey VARCHAR(10),
公寓房VARCHAR(50),buildingkey VARCHAR(10),PRIMARY KEY (公寓钥匙),外键(buildingkey)参考 building_t(buildingkey))错误代码:1824。无法打开 引用表'building_t'0.00069秒
下一步:
/* Change the apartment_t create statement to remove FK */
CREATE TABLE apartment_t (
apartmentkey VARCHAR(10),
apartmentrooms VARCHAR(50),
buildingkey VARCHAR(10),
PRIMARY KEY (apartmentkey)
);
结果:
16:47:13创建表apartment_t(apartmentkey VARCHAR(10),
公寓房VARCHAR(50),buildingkey VARCHAR(10),PRIMARY KEY (apartmentkey))的0行受到影响0.0077秒
下一步:
/*alter the apartment_t table to add the FK after the fact */
alter table apartment_t ADD FOREIGN KEY (buildingkey) REFERENCES building_t(buildingkey)
结果:
16:48:09更改表apartment_t添加外键(buildingkey) 参考building_t(buildingkey)错误代码:1146。表 'wildwood.apartment_t'不存在0.0011秒
所有格式均正确,这些问题均不应发生。请注意,当我尝试在CREATE TABLE语句中进行引用时,错误是“无法打开...”,但是在ALTER TABLE语句中,它表示不存在要更改的表。