gpt4 book ai didi

mysql - 即使使用 SET FOREIGN_KEY_CHECKS=0 也无法创建表(错误 1215);

转载 作者:行者123 更新时间:2023-11-29 11:47:22 25 4
gpt4 key购买 nike

我想做的事情我已经做过无数次了,但现在从 MySql 5.6.26 升级到 5.6.27 失败了(在 5.7.x 中也失败了)。我有一个数据库,其中有一堆带有外键约束的表。我已经导出(使用 phpMyAdmin)替换表,指定以下导出选项:

Disable foreign key checks
Add DROP TABLE / VIEW / PROCEDURE / FUNCTION / EVENT / TRIGGER statement
Add CREATE TABLE statement

生成的导出文件部分包含:

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `application`;
CREATE TABLE `application` (
`PK_Application` int(11) NOT NULL,
`FK_Parent` int(11) DEFAULT NULL,
`ApplicationLevel` tinyint(4) NOT NULL,
`Description` varchar(35) NOT NULL,
`Sequence` tinyint(4) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;

. . .
. . .

DROP TABLE IF EXISTS `product_x_application`;
CREATE TABLE `product_x_application` (
`PK_Product_X_Application` int(11) NOT NULL,
`Product_PKID` varchar(36) NOT NULL,
`FK_Application` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

. . .
. . .

ALTER TABLE `product_x_application`
ADD CONSTRAINT `product_x_application_ibfk_2` FOREIGN KEY (`FK_Application`) REFERENCES `application` (`PK_Application`) ON DELETE CASCADE ON UPDATE CASCADE;

当表 applicationproduct_x_application 已存在时,将此文件输入到 mysql.exe 或 phpMyAdmin 导入函数时,表 application< 的 DROP 语句 成功,但后续 CREATE TABLE 语句失败并显示:

ERROR 1215 (HY000): Cannot add foreign key constraint

但是,如果我首先删除 product_x_application 表,该表具有引用表 application 的外键约束 product_x_application_ibfk_2,则 CREATE TABLE 语句成功。

此后我了解了更多信息。我转到我的生产机器,它运行带有 MySql 5.5 服务器和 phpMyAdmin 4.4.23 phpMyAdmin 的 Linux,并导出了两个有问题的表,并且能够将生成的文件导入到我的 MySql 5.6.27 服务器中。这是两个完整导出文件(仅结构)。首先来自有问题的 5.6.27 服务器:

-- phpMyAdmin SQL Dump
-- version 4.4.15
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 10, 2016 at 07:01 AM
-- Server version: 5.6.26-log
-- PHP Version: 5.6.12

SET FOREIGN_KEY_CHECKS=0;
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!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 utf8mb4 */;

--
-- Database: `spmorell_sami`
--

-- --------------------------------------------------------

--
-- Table structure for table `application`
--

DROP TABLE IF EXISTS `application`;
CREATE TABLE `application` (
`PK_Application` int(11) NOT NULL,
`FK_Parent` int(11) DEFAULT NULL,
`ApplicationLevel` tinyint(4) NOT NULL,
`Description` varchar(35) NOT NULL,
`Sequence` tinyint(4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `product_x_application`
--

DROP TABLE IF EXISTS `product_x_application`;
CREATE TABLE `product_x_application` (
`PK_Product_X_Application` int(11) NOT NULL,
`Product_PKID` varchar(36) NOT NULL,
`FK_Application` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `application`
--
ALTER TABLE `application`
ADD PRIMARY KEY (`PK_Application`);

--
-- Indexes for table `product_x_application`
--
ALTER TABLE `product_x_application`
ADD PRIMARY KEY (`PK_Product_X_Application`),
ADD KEY `Product_PKID` (`Product_PKID`),
ADD KEY `FK_Application` (`FK_Application`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `application`
--
ALTER TABLE `application`
MODIFY `PK_Application` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `product_x_application`
--
ALTER TABLE `product_x_application`
MODIFY `PK_Product_X_Application` int(11) NOT NULL AUTO_INCREMENT;
--
-- Constraints for dumped tables
--

--
-- Constraints for table `product_x_application`
--
ALTER TABLE `product_x_application`
ADD CONSTRAINT `product_x_application_ibfk_1` FOREIGN KEY (`Product_PKID`) REFERENCES `product` (`Product_PKID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `product_x_application_ibfk_2` FOREIGN KEY (`FK_Application`) REFERENCES `application` (`PK_Application`) ON DELETE CASCADE ON UPDATE CASCADE;
SET FOREIGN_KEY_CHECKS=1;

/*!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 */;

请注意,application 表的主键是在 ALTER TABLE 语句中定义的。这是从 5.5 服务器导出的文件:

-- phpMyAdmin SQL Dump
-- version 3.5.8.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 10, 2016 at 05:09 AM
-- Server version: 5.5.42-37.1-log
-- PHP Version: 5.4.23

SET FOREIGN_KEY_CHECKS=0;
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Database: `spmorell_sami`
--

-- --------------------------------------------------------

--
-- Table structure for table `application`
--

DROP TABLE IF EXISTS `application`;
CREATE TABLE `application` (
`PK_Application` int(11) NOT NULL AUTO_INCREMENT,
`FK_Parent` int(11) DEFAULT NULL,
`ApplicationLevel` tinyint(4) NOT NULL,
`Description` varchar(35) NOT NULL,
`Sequence` tinyint(4) NOT NULL,
PRIMARY KEY (`PK_Application`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

-- --------------------------------------------------------

--
-- Table structure for table `product_x_application`
--

DROP TABLE IF EXISTS `product_x_application`;
CREATE TABLE `product_x_application` (
`PK_Product_X_Application` int(11) NOT NULL AUTO_INCREMENT,
`Product_PKID` varchar(36) NOT NULL,
`FK_Application` int(11) NOT NULL,
PRIMARY KEY (`PK_Product_X_Application`),
KEY `Product_PKID` (`Product_PKID`),
KEY `FK_Application` (`FK_Application`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1633 ;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `product_x_application`
--
ALTER TABLE `product_x_application`
ADD CONSTRAINT `product_x_application_ibfk_2` FOREIGN KEY (`FK_Application`) REFERENCES `application` (`PK_Application`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `product_x_application_ibfk_1` FOREIGN KEY (`Product_PKID`) REFERENCES `product` (`Product_PKID`) ON DELETE CASCADE ON UPDATE CASCADE;
SET FOREIGN_KEY_CHECKS=1;

此处,application 表的主键在定义表时立即定义。我认为问题在于,当系统尝试重新创建 application 表时,product_x_application 表已经存在,该表对表具有外键约束,当重新创建的外键上没有所需的索引,因为尚未定义主键。

最佳答案

外键应该引用唯一的列(例如主键)。目前,PK_Application 不是这样的列。不过,从其名称来看,您似乎打算将其作为主键:

ALTER TABLE `application` 
ADD CONSTRAINT `application_pk` PRIMARY KEY (`PK_Application`);

关于mysql - 即使使用 SET FOREIGN_KEY_CHECKS=0 也无法创建表(错误 1215);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34697478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com