gpt4 book ai didi

MySQL 错误 150 : two VARCHAR(255) attributes

转载 作者:行者123 更新时间:2023-11-29 15:44:16 25 4
gpt4 key购买 nike

我正在为存档创建新表,并且同时设置外键。

我尝试了不同类型的 varchar 大小,检查了 InnoDB 等,并检查了表名称中是否存在拼写错误。

CREATE TABLE `VKST_Project`
(
`id` int PRIMARY KEY,
`brand` varchar(255),
`city` varchar(255),
`street` varchar(255),
`plz` varchar(255),
`center_name` varchar(255),
`nation` varchar(255),
`shop_type` varchar(255),
`state` varchar(255),
`sales_area` varchar(255),
`open_date` varchar(255),
`construction_date` varchar(255),
`re_open_date` varchar(255),
`local_manager` varchar(255),
`window_length` double,
`object_front_length` double,
`security_alarm` boolean,
`security_goods` boolean,
`fire_sprinklers` boolean,
`fire_smoke_alarm` boolean,
`fire_RWA` boolean,
`advert_modules` varchar(255),
`tech_equipment` varchar(255),
`ventilation` varchar(255),
`heating` varchar(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;;

CREATE TABLE `Floor`
(
`id` int PRIMARY KEY AUTO_INCREMENT,
`floor_id` varchar(255),
`object_id` int,
`floor_space_total` double,
`floor_space_sales` double,
`floor_space_store` double,
`floor_space_others` double,
`floor_capacity_total` double,
`floor_capacity_sales` double,
`floor_capacity_store` double,
`floor_capacity_others` double
) ENGINE=InnoDB DEFAULT CHARSET=utf8;;

CREATE TABLE `Room`
(
`id` int PRIMARY KEY AUTO_INCREMENT,
`room_id` varchar(255),
`floor_id` varchar(255),
`object_id` int,
`name` varchar(255),
`space` double,
`height` double,
`sap` varchar(255),
`cad_file_name` varchar(255),
`DIN_Type` varchar(255),
`GIF_Type` varchar(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;;

CREATE TABLE `Ceiling`
(
`ceiling_id` int PRIMARY KEY AUTO_INCREMENT,
`room_id` varchar(255),
`material` varchar(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;;

CREATE TABLE `Flooring`
(
`flooring_id` int PRIMARY KEY AUTO_INCREMENT,
`room_id` varchar(255),
`material` varchar(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;;

ALTER TABLE `Floor` ADD FOREIGN KEY (`object_id`) REFERENCES `VKST_Project` (`id`);

ALTER TABLE `Room` ADD FOREIGN KEY (`floor_id`) REFERENCES `Floor` (`floor_id`);

ALTER TABLE `Room` ADD FOREIGN KEY (`object_id`) REFERENCES `VKST_Project` (`id`);

ALTER TABLE `Ceiling` ADD FOREIGN KEY (`room_id`) REFERENCES `Room` (`room_id`);

ALTER TABLE `Flooring` ADD FOREIGN KEY (`room_id`) REFERENCES `Room` (`room_id`);

错误发生在这里:ALTER TABLE Room ADD FOREIGN KEY (floor_id) REFERENCES Floor (floor_id) ;

错误:150“外键约束格式不正确”

最佳答案

'MySQL 需要外键和引用键上的索引' mysql 将在引用表中生成索引,但您必须在引用表(楼层、房间)中生成它们,作为定义的一部分或使用更改表。 https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

drop table if exists flooring;
drop table if exists ceiling;
drop table if exists room;
drop table if exists floor;
drop table if exists vkst_project;

CREATE TABLE `VKST_Project`
(
`id` int PRIMARY KEY,
`brand` varchar(255),
`city` varchar(255),
`street` varchar(255),
`plz` varchar(255),
`center_name` varchar(255),
`nation` varchar(255),
`shop_type` varchar(255),
`state` varchar(255),
`sales_area` varchar(255),
`open_date` varchar(255),
`construction_date` varchar(255),
`re_open_date` varchar(255),
`local_manager` varchar(255),
`window_length` double,
`object_front_length` double,
`security_alarm` boolean,
`security_goods` boolean,
`fire_sprinklers` boolean,
`fire_smoke_alarm` boolean,
`fire_RWA` boolean,
`advert_modules` varchar(255),
`tech_equipment` varchar(255),
`ventilation` varchar(255),
`heating` varchar(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `Floor`
(
`id` int PRIMARY KEY AUTO_INCREMENT,
`floor_id` varchar(255),
`object_id` int,
`floor_space_total` double,
`floor_space_sales` double,
`floor_space_store` double,
`floor_space_others` double,
`floor_capacity_total` double,
`floor_capacity_sales` double,
`floor_capacity_store` double,
`floor_capacity_others` double,
key flk1(floor_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `Room`
(
`id` int PRIMARY KEY AUTO_INCREMENT,
`room_id` varchar(255),
`floor_id` varchar(255),
`object_id` int,
`name` varchar(255),
`space` double,
`height` double,
`sap` varchar(255),
`cad_file_name` varchar(255),
`DIN_Type` varchar(255),
`GIF_Type` varchar(255),
key rki(room_id),
key rk2(floor_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `Ceiling`
(
`ceiling_id` int PRIMARY KEY AUTO_INCREMENT,
`room_id` varchar(255),
`material` varchar(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `Flooring`
(
`flooring_id` int PRIMARY KEY AUTO_INCREMENT,
`room_id` varchar(255),
`material` varchar(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `Floor` ADD FOREIGN KEY fk1(`object_id`) REFERENCES `VKST_Project` (`id`);

ALTER TABLE `Room` ADD FOREIGN KEY fk2(`floor_id`) REFERENCES `Floor` (`floor_id`);

ALTER TABLE `Room` ADD FOREIGN KEY fk3(`object_id`) REFERENCES `VKST_Project` (`id`);

ALTER TABLE `Ceiling` ADD FOREIGN KEY fk4(`room_id`) REFERENCES `Room` (`room_id`);

ALTER TABLE `Flooring` ADD FOREIGN KEY fk5(`room_id`) REFERENCES `Room` (`room_id`);

关于MySQL 错误 150 : two VARCHAR(255) attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57249228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com