0

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

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:

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!

4

3 回答 3

1

You are adding an index to the column 'int_supr_id' and then also add a primary_index to it. You can only add 1 index to it (well, in normal cases)

于 2013-08-03T18:59:29.037 回答
1

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

PRIMARY KEY ( `int_supr_id` ) ,
       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 ...

PRIMARY KEY ( `int_supr_id` ) ,
       CONSTRAINT `fk_parent_id` FOREIGN KEY ( `int_supr_id` )

With the following:

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)

于 2013-08-03T19:03:18.900 回答
0

It probably throws an error because you making both primary and foreign keys on one column int_supr_id

于 2013-08-03T19:06:56.773 回答