Mysql 创建外键时出错(检查数据类型)
我试图在两个表之间创建一个关系。这是每个表的查询和外键创建Mysql 创建外键时出错(检查数据类型),mysql,Mysql,我试图在两个表之间创建一个关系。这是每个表的查询和外键创建 CREATE TABLE IF NOT EXISTS `quotes` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `quote` text COLLATE utf8_unicode_ci NOT NULL, `author` int(11) NOT NULL, `topic` int(11) NOT NULL, `language` varchar(255) C
CREATE TABLE IF NOT EXISTS `quotes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`quote` text COLLATE utf8_unicode_ci NOT NULL,
`author` int(11) NOT NULL,
`topic` int(11) NOT NULL,
`language` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `author` (`author`),
KEY `topic` (`topic`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `authors` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`author` int(11) NOT NULL,
`period` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`photo` text COLLATE utf8_unicode_ci NOT NULL,
`references` text COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `author` (`author`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
ALTER TABLE `quotes` ADD FOREIGN KEY ( `author` ) REFERENCES `mytestdb`.`authors` (
`id`
) ON DELETE RESTRICT ON UPDATE RESTRICT ;
但它抛出了以下错误
Error creating foreign key on author (check data types)
我不确定这个错误是怎么回事。在此方面的任何帮助都将不胜感激。数据类型和符号必须相同
int
与unsigned int
显示宽度不相关
author
是有符号整数
id
是一个无符号整数
从:
外键和引用键中的相应列必须
具有相似的数据类型。整数类型的大小和符号必须为
相同的。字符串类型的长度不必相同。对于
非二进制(字符)字符串列、字符集和排序规则
一定是一样的
它们的大小相同(int)。他们的星座不一样
10和11的显示宽度无关
演示
工作正常
CREATE TABLE IF NOT EXISTS `quotes` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`quote` text COLLATE utf8_unicode_ci NOT NULL,
`author` int(11) NOT NULL,
`topic` int(11) NOT NULL,
`language` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `author` (`author`),
KEY `topic` (`topic`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `authors` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`author` int(11) NOT NULL,
`period` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`photo` text COLLATE utf8_unicode_ci NOT NULL,
`references` text COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `author` (`author`),
CONSTRAINT fk_blah222 FOREIGN KEY (author) REFERENCES quotes(id)
) ENGINE=InnoDB;
工作正常数据类型和符号必须相同
CREATE TABLE IF NOT EXISTS `quotes` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`quote` text COLLATE utf8_unicode_ci NOT NULL,
`author` int(11) NOT NULL,
`topic` int(11) NOT NULL,
`language` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `author` (`author`),
KEY `topic` (`topic`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `authors` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`author` int(11) NOT NULL,
`period` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`photo` text COLLATE utf8_unicode_ci NOT NULL,
`references` text COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `author` (`author`),
CONSTRAINT fk_blah222 FOREIGN KEY (author) REFERENCES quotes(id)
) ENGINE=InnoDB;
int
与unsigned int
显示宽度不相关
author
是有符号整数
id
是一个无符号整数
从:
外键和引用键中的相应列必须
具有相似的数据类型。整数类型的大小和符号必须为
相同的。字符串类型的长度不必相同。对于
非二进制(字符)字符串列、字符集和排序规则
一定是一样的
它们的大小相同(int)。他们的星座不一样
10和11的显示宽度无关
演示
工作正常
CREATE TABLE IF NOT EXISTS `quotes` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`quote` text COLLATE utf8_unicode_ci NOT NULL,
`author` int(11) NOT NULL,
`topic` int(11) NOT NULL,
`language` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `author` (`author`),
KEY `topic` (`topic`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `authors` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`author` int(11) NOT NULL,
`period` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`photo` text COLLATE utf8_unicode_ci NOT NULL,
`references` text COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `author` (`author`),
CONSTRAINT fk_blah222 FOREIGN KEY (author) REFERENCES quotes(id)
) ENGINE=InnoDB;
可以正常工作错误消息很清楚:
mytestdb.authors.id
是无符号整数(10),而mytestdb.quotes.author
是有符号整数(11)与无符号整数(11)。Chg oneerror消息很清楚:mytestdb.authors.id
是无符号int(10),而mytestdb.quotes.author
是有符号int(11)与无符号int(11)。作者的大小应与引号id int size相同。不,它们的大小相同。它们的符号不同。慢慢读(答案)这里有三件事让人困惑。只有2个是相关的。两个相关的是尺寸和标志。第三个(不相关)是显示宽度10和11。作者的大小应与引号id int size相同。不,它们的大小相同。它们的符号不同。慢慢读(答案)这里有三件事让人困惑。只有2个是相关的。两个相关的是尺寸和标志。第三个(不相关)是显示宽度10和11。
CREATE TABLE IF NOT EXISTS `quotes` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`quote` text COLLATE utf8_unicode_ci NOT NULL,
`author` int(11) NOT NULL,
`topic` int(11) NOT NULL,
`language` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `author` (`author`),
KEY `topic` (`topic`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `authors` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`author` int(11) NOT NULL,
`period` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`photo` text COLLATE utf8_unicode_ci NOT NULL,
`references` text COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `author` (`author`),
CONSTRAINT fk_blah222 FOREIGN KEY (author) REFERENCES quotes(id)
) ENGINE=InnoDB;