gpt4 book ai didi

mysql - 无法创建 mysql 表 : foreign key constraint issue

转载 作者:搜寻专家 更新时间:2023-10-30 23:27:33 25 4
gpt4 key购买 nike

我删除了一个 sql 表(没有键约束检查),现在我无法从 sql 查询重新生成或再次导入。

创建查询:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
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 */;

CREATE TABLE `customer_entity` (
`entity_id` int(10) UNSIGNED NOT NULL COMMENT 'Entity Id',
`website_id` smallint(5) UNSIGNED DEFAULT NULL COMMENT 'Website Id',
`email` varchar(255) DEFAULT NULL COMMENT 'Email',
`group_id` smallint(5) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Group Id',
`increment_id` varchar(50) DEFAULT NULL COMMENT 'Increment Id',
`store_id` smallint(5) UNSIGNED DEFAULT '0' COMMENT 'Store Id',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Created At',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Updated At',
`is_active` smallint(5) UNSIGNED NOT NULL DEFAULT '1' COMMENT 'Is Active',
`disable_auto_group_change` smallint(5) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Disable automatic group change based on VAT ID',
`created_in` varchar(255) DEFAULT NULL COMMENT 'Created From',
`prefix` varchar(40) DEFAULT NULL COMMENT 'Prefix',
`firstname` varchar(255) DEFAULT NULL COMMENT 'First Name',
`middlename` varchar(255) DEFAULT NULL COMMENT 'Middle Name/Initial',
`lastname` varchar(255) DEFAULT NULL COMMENT 'Last Name',
`suffix` varchar(40) DEFAULT NULL COMMENT 'Suffix',
`dob` date DEFAULT NULL COMMENT 'Date of Birth',
`password_hash` varchar(128) DEFAULT NULL COMMENT 'Password_hash',
`rp_token` varchar(128) DEFAULT NULL COMMENT 'Reset password token',
`rp_token_created_at` datetime DEFAULT NULL COMMENT 'Reset password token creation time',
`default_billing` int(10) UNSIGNED DEFAULT NULL COMMENT 'Default Billing Address',
`default_shipping` int(10) UNSIGNED DEFAULT NULL COMMENT 'Default Shipping Address',
`taxvat` varchar(50) DEFAULT NULL COMMENT 'Tax/VAT Number',
`confirmation` varchar(64) DEFAULT NULL COMMENT 'Is Confirmed',
`gender` smallint(5) UNSIGNED DEFAULT NULL COMMENT 'Gender',
`failures_num` smallint(6) DEFAULT '0' COMMENT 'Failure Number',
`first_failure` timestamp NULL DEFAULT NULL COMMENT 'First Failure',
`lock_expires` timestamp NULL DEFAULT NULL COMMENT 'Lock Expiration Date'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Customer Entity';

ALTER TABLE `customer_entity`
ADD PRIMARY KEY (`entity_id`),
ADD UNIQUE KEY `CUSTOMER_ENTITY_EMAIL_WEBSITE_ID` (`email`,`website_id`),
ADD KEY `CUSTOMER_ENTITY_STORE_ID` (`store_id`),
ADD KEY `CUSTOMER_ENTITY_WEBSITE_ID` (`website_id`),
ADD KEY `CUSTOMER_ENTITY_FIRSTNAME` (`firstname`),
ADD KEY `CUSTOMER_ENTITY_LASTNAME` (`lastname`);


ALTER TABLE `customer_entity`
MODIFY `entity_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Entity Id';


ALTER TABLE `customer_entity`
ADD CONSTRAINT `CUSTOMER_ENTITY_STORE_ID_STORE_STORE_ID` FOREIGN KEY (`store_id`) REFERENCES `store` (`store_id`) ON DELETE SET NULL,
ADD CONSTRAINT `CUSTOMER_ENTITY_WEBSITE_ID_STORE_WEBSITE_WEBSITE_ID` FOREIGN KEY (`website_id`) REFERENCES `store_website` (`website_id`) ON DELETE SET NULL;
COMMIT;

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

发现错误:

#1215 - 无法添加参照完整性约束(外部键约束)

我也尝试过使用 SET FOREIGN_KEY_CHECKS = 0; 但我没有成功。我尝试再次导入 sql 表但没有成功。在创建查询中没有关键的外部约束,所以我无法弄清楚为什么查询会产生错误。

该表在 Magento 2 数据库中称为“customer_entity.sql”。

最佳答案

外键约束是不是在另外一张表上?如果是这样,您可以尝试删除它,创建此表,然后重新添加约束。

关于mysql - 无法创建 mysql 表 : foreign key constraint issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54577248/

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