繁体   English   中英

由于外键约束,在ubuntu / trusty64 php5-mysql上无法创建表

[英]Can't create a table due to Foreign key constraint, on ubuntu/trusty64 php5-mysql

我已经将项目从XAMP移至VM-> Vagrant-> ubuntu / trusty64-> Installed(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`)
);

这一直贯穿

寻找写有CHANGE MADE的字样

未正确指定列。 另外,有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)

另外,如果它们与从1开始及更高的FK约束相冲突,请考虑删除DEFAULT 0 坦率地说,我不明白这一点。

从名为“ 使用外键约束 ”的mysql手册页中

外键和引用键中的对应列必须具有相似的数据类型。 整数类型的大小和符号必须相同。 字符串类型的长度不必相同。 对于非二进制(字符)字符串列,字符集和排序规则必须相同。

MySQL需要在外键和引用键上建立索引,以便外键检查可以快速进行,而无需进行表扫描。 在引用表中,必须有一个索引,其中外键列以相同的顺序列为第一列。 如果这样的索引不存在,则会在引用表上自动创建。 如果您创建另一个可用于强制外键约束的索引,则以后可能会静默删除该索引。 如果给定,则使用index_name(如前所述)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM