MySQL Workbench-具有外键约束的错误ERROR 1215(HY000)

时间:2018-11-10 11:06:10

标签: mysql sql mysql-workbench

我有以下两个表:

enter image description here

enter image description here

我需要添加UT作为Id_UT的外键,但是“被引用列”中的条目消失。

enter image description here

在我看来,键值的所有约束都已满足(数据类型,大小,唯一性)。这里有什么问题吗?谢谢。

这是一个最小的工作示例:

-- MySQL dump 10.13  Distrib 5.7.17, for Win32 (AMD64)
--
-- Host: 127.0.0.1    Database: newschema
-- ------------------------------------------------------
-- Server version	5.7.17

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `catalogo_reperti`
--

DROP TABLE IF EXISTS `catalogo_reperti`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `catalogo_reperti` (
  `UT` varchar(45) COLLATE utf16_unicode_ci NOT NULL,
  `Id_Reperto` varchar(45) COLLATE utf16_unicode_ci NOT NULL,
  `Documentazione` text COLLATE utf16_unicode_ci,
  PRIMARY KEY (`Id_Reperto`,`UT`),
  UNIQUE KEY `Id_Reperto_UNIQUE` (`Id_Reperto`)
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `catalogo_unita_topografiche`
--

DROP TABLE IF EXISTS `catalogo_unita_topografiche`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `catalogo_unita_topografiche` (
  `Id_UT` varchar(45) COLLATE utf16_unicode_ci NOT NULL,
  PRIMARY KEY (`Id_UT`),
  UNIQUE KEY `Id_UT_UNIQUE` (`Id_UT`)
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

1 个答案:

答案 0 :(得分:0)

强制声明字符集/排序规则(而不是保留默认的字符集,无论如何将其设置为相同)允许程序声明外键。