mysql – 选定的列没有兼容的类型,即使它具有相同的类型

我正在使用groupName从Table2在Table1(propType)上创建FOREIGN KEY.但我面临以下错误.
选定的列没有兼容的类型,即使它在mysql中具有相同的类型

表格1 :

    CREATE TABLE `configuration_master` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `insertTimestamp` datetime DEFAULT NULL,
      `propName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
      `propValue` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `propType` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
      `IsCloudSupport` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
      `DisplayName` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
      `updateTimestamp` datetime DEFAULT NULL,
      `userId` bigint(20) DEFAULT NULL,
      `SYNCCOL1` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL3` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL4` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL6` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL7` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL8` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL9` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL10` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `prptyp` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `propName` (`propName`),
      KEY `FKB54491EB8E326E43` (`userId`),
      KEY `proptype_frn_idx` (`propType`),
      KEY `ASD_idx` (`prptyp`),
      CONSTRAINT `FKB54491EB8E326E43` FOREIGN KEY (`userId`) REFERENCES `user_master` (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=86 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

表2:

    CREATE TABLE `config_group` (
      `groupName` varchar(45) CHARACTER SET utf8 NOT NULL,
      `prop1` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
      `prop2` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
      `prop3` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
      `prop4` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
      `ADS` int(11) DEFAULT NULL,
      `config_groupcol` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
      PRIMARY KEY (`groupName`),
      UNIQUE KEY `config_groupcol_UNIQUE` (`config_groupcol`),
      UNIQUE KEY `ADS_UNIQUE` (`ADS`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

我正在创建FOREIGN KEY,如下所示

    ALTER TABLE `konysyncconsoledb7214dev`.`configuration_master` 
    ADD CONSTRAINT `FK_configGrop`
      FOREIGN KEY (`propType`)
      REFERENCES `konysyncconsoledb7214dev`.`config_group` (`groupName`)

下图是错误消息.如果有人遇到这个问题,请告诉我我错过了什么.
error msg

从mysql手册页找到here

Corresponding columns in the foreign key and the referenced key must
have similar data types. The size and sign of integer types must be
the same. The length of string types need not be the same. For
nonbinary (character) string columns, the character set and collation
must be the same.

你的问题是整理.创建以下内容我没有任何问题.请注意,我不得不放弃部分数据库中的数据库名称.

哦,既然你没有提供第三个表,我不得不删除那个FK约束.但这不是问题.

注意,可空性不是问题.

您决定如何处理校对选择取决于您自己.但就是这样.

CREATE TABLE `configuration_master` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `insertTimestamp` datetime DEFAULT NULL,
  `propName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `propValue` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `propType` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `IsCloudSupport` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `DisplayName` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `updateTimestamp` datetime DEFAULT NULL,
  `userId` bigint(20) DEFAULT NULL,
  `SYNCCOL1` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL3` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL4` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL6` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL7` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL8` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL9` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL10` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `prptyp` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `propName` (`propName`),
  KEY `FKB54491EB8E326E43` (`userId`),
  KEY `proptype_frn_idx` (`propType`),
  KEY `ASD_idx` (`prptyp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



 CREATE TABLE `config_group` (
  `groupName` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
  `prop1` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  `prop2` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  `prop3` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  `prop4` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  `ADS` int(11) DEFAULT NULL,
  `config_groupcol` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`groupName`),
  UNIQUE KEY `config_groupcol_UNIQUE` (`config_groupcol`),
  UNIQUE KEY `ADS_UNIQUE` (`ADS`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;   

ALTER TABLE  `configuration_master` 
ADD CONSTRAINT `FK_configGrop`
  FOREIGN KEY (`propType`)
  REFERENCES `config_group` (`groupName`);

原始的更多细节

这解决了Barmar的评论.您的config_group创建表导致FK的匹配排序与ALTER TABLE成功的差异. SHOW FULL COLUMNS的手册页

从手册页面here

If CHARACTER SET X is specified without COLLATE, character set X and
its default collation are used.

06001

The character set is specified for the column, but the collation is
not. The column has character set utf8 and the default collation for
utf8, which is utf8_general_ci. To see the default collation for each
character set, use the SHOW COLLATION statement.

enter image description here

https://stackoverflow.com/questions/37595974/selected-columns-dont-have-compatiable-type-even-it-has-same-type

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:mysql – 选定的列没有兼容的类型,即使它具有相同的类型