我已将项目从XAMP移至VM-> Vagrant-> ubuntu / trusty64->已安装(php5-mysql mysql-server mysql-client)
现在,当我尝试创建数据库和表时,我收到以下错误。
错误
无法创建表properties
,外键约束不正确。引用的表中没有索引,其中引用的列显示为第一列。
这是我的第一个脚本,所以我假设有一些错误但是当我使用XAMP时它都运行良好。我一直在阅读你使用的引擎和字符集,但我不太了解它或如何在流浪汉中编辑它。感谢所有帮助代码如下。
的MySQL
CREATE TABLE `usertypes`(
`id` INT NOT NULL AUTO_INCREMENT,
`type` CHAR(15),
`permissions` CHAR(15),
PRIMARY KEY (`id`)
);
INSERT INTO `usertypes`(`type`,`permissions`) VALUES ("Administrator", '{"Admin":1}' ), ("Staff", '{"Staff":1}'), ("Basic", '{"Basic":1}'), ("Pro", '{"Pro":1}'), ("Business", '{"Business":1}');
CREATE TABLE `users` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`username` VARCHAR (25) NOT NULL UNIQUE,
`password` VARCHAR (60) NOT NULL,
`usertype` INT NOT NULL DEFAULT 3,
`email` varchar(40) NOT NULL,
`authentication` varchar(32) NOT NULL,
`active` smallint(1) NOT NULL DEFAULT 0,
`newsletter` INT NOT NULL,
`banned` SMALLINT NOT NULL DEFAULT 0,
`user_since` DATE NOT NULL,
`listings` INT DEFAULT 0,
CONSTRAINT fk_usertype FOREIGN KEY (`usertype`) REFERENCES usertypes(`id`),
UNIQUE KEY `users` (`username`,`email`)
);
CREATE TABLE `personal` (
`id` INT PRIMARY KEY,
`first_name` VARCHAR (15) NULL,
`last_name` VARCHAR (15) NULL,
`mobile_phone` CHAR(10) NULL,
`city` CHAR(25) NULL,
`address` VARCHAR (100) NULL,
`postal_code` VARCHAR(6) NULL,
CONSTRAINT fk_personal FOREIGN KEY (`id`)
REFERENCES users(`id`)
);
/* Create Properties table */
CREATE TABLE `propertytypes`(
`id` INT NOT NULL AUTO_INCREMENT,
`type` CHAR(10),
PRIMARY KEY (`id`)
);
INSERT INTO `propertytypes`(`type`) VALUES ("house"), ("duplex"), ("apartment"), ("townhouse"), ("4 plex"),("6 plex"), ("room"), ("commercial"), ("gathering halls");
CREATE TABLE `utilities`(
`id` INT NOT NULL AUTO_INCREMENT,
`options` CHAR(10),
PRIMARY KEY (`id`)
);
INSERT INTO `utilities`(`options`) VALUES ("None"),("Heat"), ("Electricity"), ("Water"), ("TV"), ("Internet");
CREATE TABLE `features`(
`id` INT NOT NULL AUTO_INCREMENT,
`options` CHAR(10),
PRIMARY KEY (`id`)
);
INSERT INTO `features`(`options`) VALUES ("Coin Laundry"), ("Own Laundry"), ("Jetted Tub / Jacuzzi"), ("Gym"), ("Pool"), ("Security"), ("Balcony"), ("Elevator"), ("Hardwood Floors"), ("Fenced Backyard"), ("Dishwasher"), ("Air Conditioning"), ("Laminate Floors"), ("Fire Pit"), ("Fireplace"), ("Wheelchair Access"), ("Storage Lockers");
CREATE TABLE `properties` (
`id` INT NOT NULL AUTO_INCREMENT,
`owner` INT NOT NULL,
`propertytype` INT NOT NULL,
`address` varchar(255) NOT NULL,
`postalcode` varchar(8) NOT NULL,
`beds` INT(10),
`baths` INT(10),
`sqf` DECIMAL (6,2),
`price` DECIMAL (6,2),
`images` varchar(32),
`listed` smallint(1) DEFAULT 2,
`pets` smallint(1) DEFAULT 2,
`kids` smallint(1) DEFAULT 2,
`adults` smallint(1) DEFAULT 2,
`utilities` INT NOT NULL DEFAULT 0,
`features` INT NOT NULL DEFAULT 0,
`parking` smallint(1) DEFAULT 2,
`smokers` smallint(1) DEFAULT 2,
`deposit` smallint(1) DEFAULT 0,
`description` TEXT NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT fk_users FOREIGN KEY (`owner`)
REFERENCES users(`id`),
CONSTRAINT fk_propertytypes FOREIGN KEY (`propertytype`)
REFERENCES propertytypes(`id`),
CONSTRAINT fk_utilities FOREIGN KEY (`utilities`)
REFERENCES utilities(`options`),
CONSTRAINT fk_features FOREIGN KEY (`features`)
REFERENCES features(`options`)
);
答案 0 :(得分:1)
这一直贯穿
查找说改变
的行未正确指定列。另外,在插入过程中有2列有截断。
create schema ff; -- create a new db
use ff; -- now use that db
CREATE TABLE `usertypes`(
`id` INT NOT NULL AUTO_INCREMENT,
`type` CHAR(15),
`permissions` CHAR(15),
PRIMARY KEY (`id`)
);
INSERT INTO `usertypes`(`type`,`permissions`) VALUES ("Administrator", '{"Admin":1}' ), ("Staff", '{"Staff":1}'), ("Basic", '{"Basic":1}'), ("Pro", '{"Pro":1}'), ("Business", '{"Business":1}');
CREATE TABLE `users` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`username` VARCHAR (25) NOT NULL UNIQUE,
`password` VARCHAR (60) NOT NULL,
`usertype` INT NOT NULL DEFAULT 3,
`email` varchar(40) NOT NULL,
`authentication` varchar(32) NOT NULL,
`active` smallint(1) NOT NULL DEFAULT 0,
`newsletter` INT NOT NULL,
`banned` SMALLINT NOT NULL DEFAULT 0,
`user_since` DATE NOT NULL,
`listings` INT DEFAULT 0,
CONSTRAINT fk_usertype FOREIGN KEY (`usertype`) REFERENCES usertypes(`id`),
UNIQUE KEY `users` (`username`,`email`)
);
CREATE TABLE `personal` (
`id` INT PRIMARY KEY,
`first_name` VARCHAR (15) NULL,
`last_name` VARCHAR (15) NULL,
`mobile_phone` CHAR(10) NULL,
`city` CHAR(25) NULL,
`address` VARCHAR (100) NULL,
`postal_code` VARCHAR(6) NULL,
CONSTRAINT fk_personal FOREIGN KEY (`id`)
REFERENCES users(`id`)
);
/* Create Properties table */
-- drop table propertytypes;
CREATE TABLE `propertytypes`(
`id` INT NOT NULL AUTO_INCREMENT,
`type` CHAR(40), -- CHANGE MADE ************************
PRIMARY KEY (`id`)
);
INSERT INTO `propertytypes`(`type`) VALUES ("house"), ("duplex"), ("apartment"), ("townhouse"), ("4 plex"),("6 plex"), ("room"), ("commercial"), ("gathering halls");
-- drop table utilities;
CREATE TABLE `utilities`(
`id` INT NOT NULL AUTO_INCREMENT,
`options` CHAR(40), -- CHANGE MADE ************************
PRIMARY KEY (`id`)
);
INSERT INTO `utilities`(`options`) VALUES ("None"),("Heat"), ("Electricity"), ("Water"), ("TV"), ("Internet");
CREATE TABLE `features`(
`id` INT NOT NULL AUTO_INCREMENT,
`options` CHAR(40), -- CHANGE MADE ************************
PRIMARY KEY (`id`)
);
INSERT INTO `features`(`options`) VALUES ("Coin Laundry"), ("Own Laundry"), ("Jetted Tub / Jacuzzi"), ("Gym"), ("Pool"), ("Security"), ("Balcony"), ("Elevator"), ("Hardwood Floors"), ("Fenced Backyard"), ("Dishwasher"), ("Air Conditioning"), ("Laminate Floors"), ("Fire Pit"), ("Fireplace"), ("Wheelchair Access"), ("Storage Lockers");
CREATE TABLE `properties` (
`id` INT NOT NULL AUTO_INCREMENT,
`owner` INT NOT NULL,
`propertytype` INT NOT NULL,
`address` varchar(255) NOT NULL,
`postalcode` varchar(8) NOT NULL,
`beds` INT(10),
`baths` INT(10),
`sqf` DECIMAL (6,2),
`price` DECIMAL (6,2),
`images` varchar(32),
`listed` smallint(1) DEFAULT 2,
`pets` smallint(1) DEFAULT 2,
`kids` smallint(1) DEFAULT 2,
`adults` smallint(1) DEFAULT 2,
`utilities` INT NOT NULL DEFAULT 0,
`features` INT NOT NULL DEFAULT 0,
`parking` smallint(1) DEFAULT 2,
`smokers` smallint(1) DEFAULT 2,
`deposit` smallint(1) DEFAULT 0,
`description` TEXT NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT fk_users FOREIGN KEY (`owner`)
REFERENCES users(`id`),
CONSTRAINT fk_propertytypes FOREIGN KEY (`propertytype`)
REFERENCES propertytypes(`id`),
CONSTRAINT fk_utilities FOREIGN KEY (`utilities`)
REFERENCES utilities(`id`), -- CHANGE MADE ************************
CONSTRAINT fk_features FOREIGN KEY (`features`)
REFERENCES features(`id`) -- CHANGE MADE ************************
);
drop schema ff; -- drop test db (cleanup)
另外,如果DEFAULT 0
与从1开始向上的FK约束发生冲突,请考虑删除它。坦率地说,我没有看到它的重点。
来自名为Using FOREIGN KEY Constraints
的mysql手册页外键和引用键中的对应列必须 有类似的数据类型。整数类型的大小和符号必须是 相同。字符串类型的长度不必相同。对于 非二进制(字符)字符串列,字符集和排序规则 必须是一样的。
和
MySQL要求外键和引用键上的索引 外键检查可以很快,不需要表扫描。在里面 引用表时,必须有一个索引所在的外键 列以相同顺序列为第一列。这样的 如果不是,则会自动在引用表上创建索引 存在。如果您创建,可能会稍后以静默方式删除此索引 另一个可用于强制执行外键约束的索引。 如果给定,则使用index_name,如前所述。