目錄
Welcome to relational databases in MySQL! ;)
So, how do I fix this annoying error?
首頁 資料庫 mysql教程 DuplicatekeyerrorinMySQL(Duplicatekeyname'&a

DuplicatekeyerrorinMySQL(Duplicatekeyname'&a

Jun 07, 2016 pm 03:54 PM

The below query is resulting in an error. I created this query in MySQL Workbench ErrorSQL query:-- ------------------------------------------------------- Table `smsdb`.`IntSupervisor`-- ---------------------------------------------------

The below query is resulting in an error. I created this query in MySQL Workbench

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Error

 

SQL query:

 

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

-- Table `smsdb`.`IntSupervisor`

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

  CREATE TABLE IF NOT EXISTS `smsdb`.`IntSupervisor` (

   `int_supr_id` VARCHAR( 32 ) NOT NULL ,

   `cent_id` INT NOT NULL ,

   INDEX `fk_IntSupervisor_Person1_idx` ( `int_supr_id` ASC ) ,

   INDEX `fk_IntSupervisor_Center1_idx` ( `cent_id` ASC ) ,

   PRIMARY KEY ( `int_supr_id` ) ,

   CONSTRAINT `fk_parent_id` FOREIGN KEY ( `int_supr_id` )

   REFERENCES `smsdb`.`Staff` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT ,

   CONSTRAINT `fk_center_id` FOREIGN KEY ( `cent_id` )

   REFERENCES  `smsdb`.`Center` (`cent_id`

 ) ON DELETE CASCADE ON UPDATE RESTRICT

 ) ENGINE = InnoDB;

登入後複製

I got an error message on execution:

1

2

MySQL said: Documentation 

    #1022 - Can't write; duplicate key in table 'intsupervisor'

登入後複製

If anyone has any ideas on how I can resolve this issue, please guide me in the right direction. Thanks!

Welcome to relational databases in MySQL! ;)


You can't have two foreign keys named the same thing across the whole query.

1

PRIMARY KEY ( `int_supr_id` ) ,

登入後複製

1

CONSTRAINT `fk_parent_id` FOREIGN KEY ( `int_supr_id` )

登入後複製

In the above statement, you can't redefine the index type on a single column.

So, how do I fix this annoying error?


Based on the structure of the database, you need to remove one of the two lines above. I'm guessing your linking to another table from the one you are creating, so I recommend replacing ...

1

2

PRIMARY KEY ( `int_supr_id` ) ,

       CONSTRAINT `fk_parent_id` FOREIGN KEY ( `int_supr_id` )

登入後複製

With the following:

1

CONSTRAINT `fk_parent_id` FOREIGN KEY ( `int_supr_id` )

登入後複製

(if the above doesn't work, you likely need to specify a table name for the foreign key)


2.--
-- Table structure for table `payment`
--


DROP TABLE IF EXISTS `payment`;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `payment` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`entry_ID` int(11) NOT NULL,
`account_ID` int(11) NOT NULL,
`amount` double NOT NULL,
`pmt_form` varchar(20) NOT NULL,
`reference` varchar(120) DEFAULT NULL,
`COID` int(11) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID` (`ID`),
KEY `FK_PAYMENT_ACCOUNT` (`account_ID`),
KEY `FK_PAYMENT_ENTRY` (`entry_ID`),
CONSTRAINT `FK_PAYMENT_ACCOUNT` FOREIGN KEY (`account_ID`) REFERENCES `account` (`ID`),
CONSTRAINT `FK_PAYMENT_ENTRY` FOREIGN KEY (`entry_ID`)REFERENCES `register_entry` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
SET character_set_client = @saved_cs_client;

一开始一个是entry_ID 一个是 entry_id 导致报错。后来改为一样 导入数据通过

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

兩個點博物館:邦格荒地地點指南
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

兩個點博物館:邦格荒地地點指南
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

減少在Docker中使用MySQL內存的使用 減少在Docker中使用MySQL內存的使用 Mar 04, 2025 pm 03:52 PM

減少在Docker中使用MySQL內存的使用

如何使用Alter Table語句在MySQL中更改表? 如何使用Alter Table語句在MySQL中更改表? Mar 19, 2025 pm 03:51 PM

如何使用Alter Table語句在MySQL中更改表?

mysql無法打開共享庫怎麼解決 mysql無法打開共享庫怎麼解決 Mar 04, 2025 pm 04:01 PM

mysql無法打開共享庫怎麼解決

在 Linux 中運行 MySQl(有/沒有帶有 phpmyadmin 的 podman 容器) 在 Linux 中運行 MySQl(有/沒有帶有 phpmyadmin 的 podman 容器) Mar 04, 2025 pm 03:54 PM

在 Linux 中運行 MySQl(有/沒有帶有 phpmyadmin 的 podman 容器)

什麼是 SQLite?全面概述 什麼是 SQLite?全面概述 Mar 04, 2025 pm 03:55 PM

什麼是 SQLite?全面概述

在MacOS上運行多個MySQL版本:逐步指南 在MacOS上運行多個MySQL版本:逐步指南 Mar 04, 2025 pm 03:49 PM

在MacOS上運行多個MySQL版本:逐步指南

如何為MySQL連接配置SSL/TLS加密? 如何為MySQL連接配置SSL/TLS加密? Mar 18, 2025 pm 12:01 PM

如何為MySQL連接配置SSL/TLS加密?

如何保護MySQL免受常見漏洞(SQL注入,蠻力攻擊)? 如何保護MySQL免受常見漏洞(SQL注入,蠻力攻擊)? Mar 18, 2025 pm 12:00 PM

如何保護MySQL免受常見漏洞(SQL注入,蠻力攻擊)?

See all articles