MYSQL Error - Duplicate key on write or update - mysql

CREATE TABLE IF NOT EXISTS `demare`.`shop` (
`shop_id` INT NOT NULL AUTO_INCREMENT,
`shop_image` VARCHAR(255) NOT NULL,
`shop_price` FLOAT(6,2) NOT NULL,
`cart_id` INT NOT NULL,
PRIMARY KEY (`shop_id`),
INDEX `cart_id_idx` (`cart_id` ASC),
CONSTRAINT `cart_id`
FOREIGN KEY (`cart_id`)
REFERENCES `demare`.`shopping cart` (`cart_id`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I have an error. My 'shop' table cannot be created as I have error:
121 Duplicate key on write or update.
I have provided the codes. Can anyone tell me what the problem is?

Problem exist because the foriegn key exist in memory, if possible drop the database and start again.

Related

Duplicate Key issue after lost connection even thou constraint does not exist

I was doing a migration on laravel for all my DB's (several clients). All DB are the same structure and one of them lost connection and failed before completion. I tried to migrate again and I ran into an issue with a duplicate foreign key.
After looking at the constraints
SELECT * FROM information_schema.KEY_COLUMN_USAGE ke where constraint_name = 'rt_eqmtfixedcycle_excav_id_foreign'
SQL result for schema
All other databases successfully inserted 'rt_eqmtfixedcycle_excav_id_foreign' key. But the one that failed 'smv_Forestalleonera' database did not appear, but.
If I try to insert again:
use smv_forestalleonera;
alter table `rt_eqmtfixedcycle` add constraint `rt_eqmtfixedcycle_excav_id_foreign` foreign key (`excav_id`) references `rt_truck` (`id`) on delete restrict on update cascade
I get the following error:
12:33:45 alter table `rt_eqmtfixedcycle`
add constraint `rt_eqmtfixedcycle_excav_id_foreign`
foreign key (`excav_id`)
references `rt_truck` (`id`)
on delete restrict
on update cascade Error Code: 1005. Can't create table `smv_forestalleonera`.`#sql-7f4d_a8f7` (errno: 121 "Duplicate key on write or update") 0.234 sec
I'm using MariaDB 10.2.7.
I've already removed the table and restarted the engine without any results.
(from comment)
CREATE TABLE rt_eqmtfixedcycle (
id smallint(5) unsigned NOT NULL,
excav_id smallint(5) unsigned DEFAULT NULL,
locload_id int(10) unsigned DEFAULT NULL,
locdump_id int(10) unsigned DEFAULT NULL,
updated_at datetime DEFAULT NULL,
updated_by int(10) unsigned DEFAULT NULL,
KEY rt_eqmtfixedcycle_id_foreign (id),
CONSTRAINT rt_eqmtfixedcycle_id_foreign
FOREIGN KEY (id) REFERENCES rt_truck (id) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Can't write; duplicate key in table 'UserSessions' Even pk name changed

I want to create two table AdminSessions and UserSessions to store the client session data. Both of them have a pk named sessionKey with the properties of PK, NN, UQ. When I try to do a forward engineering, I encounter Can't write; duplicate key in table 'UserSessions'. After reading this solution, I try to change the name of the pks to sessionKeya and sessionKeyb but it does not work.
How to solve this error?
Error Message
ERROR: Error 1022: Can't write; duplicate key in table 'UserSessions'
SQL
CREATE TABLE IF NOT EXISTS `CodeSpace`.`UserSessions` (
`sessionKey` VARCHAR(128) NOT NULL,
`userId` INT UNSIGNED NOT NULL,
`lastAccessTime` DATETIME NULL,
`ip` VARCHAR(45) NULL,
`expiryTime` DATETIME NULL,
PRIMARY KEY (`sessionKey`),
UNIQUE INDEX `userId_UNIQUE` (`userId` ASC),
UNIQUE INDEX `id_UNIQUE` (`sessionKey` ASC),
CONSTRAINT `fk_UserSession_1`
FOREIGN KEY (`userId`)
REFERENCES `CodeSpace`.`Users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB

duplicate key in table for foreign key in sql

I am using mysql_workbench to create database I have products_types, categories and subcategories table. category_id and subcategory_id are the foreign keys referencing to categories and subcategories table respectively. When when I forward engineer this model from workbench to mysql database on phpMyAdmin it gives error as
Can't write; duplicate key in table 'product_types'
This is the exact code along with error.
Executing SQL script in server
ERROR: Error 1022: Can't write; duplicate key in table 'product_types'
SQL Code:
-- -----------------------------------------------------
-- Table `argo_project_01_2`.`product_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `argo_project_01_2`.`product_types` (
`id` INT NOT NULL,
`category_id` INT NULL,
`subcategory_id` INT NULL,
`title` VARCHAR(100) NULL,
`description` TEXT NULL,
`status` INT NULL DEFAULT 0,
`created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`modified` TIMESTAMP NULL,
PRIMARY KEY (`id`),
INDEX `category_id_idx` (`category_id` ASC),
INDEX `subcategory_id_idx` (`subcategory_id` ASC),
CONSTRAINT `category_id`
FOREIGN KEY (`category_id`)
REFERENCES `argo_project_01_2`.`categories` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `subcategory_id`
FOREIGN KEY (`subcategory_id`)
REFERENCES `argo_project_01_2`.`subcategories` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 9 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
What is wrong in the query ?
I think the problem may be solved by Changing constraint names "category_id" and "subcategory_id" to for example "fk_category_id" and "fk_subcategory_id".
Edit :
Also you can remove "category_id_idx" and "subcategory_id_idx" indexes because the foreign key automatically generates an index.

While creating database: Error Code: 1215. Cannot add foreign key constraint

When I am creating a database, I'm getting a little bit of a difficulty with creating the "main" table for this database.
I thought there was a type difference between the foreign key and the primary key, but there wasn't.
So can anyone help me out? There must be something that I'm overlooking, but I can't find the problem.
[code]
-- -----------------------------------------------------
-- Table `boat`.`terms_and_conditions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `boat`.`terms_and_conditions` (
`Terms_And_Conditions_Id` INT NOT NULL,
`Terms_And_Conditions_Doc` VARCHAR(255) NULL DEFAULT NULL,
`Terms_And_Conditions_Date` DATE NULL DEFAULT NULL,
PRIMARY KEY (`Terms_And_Conditions_Id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `boat`.`player`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `boat`.`player` (
`Player_Id` INT NOT NULL,
`Player_Firstname` VARCHAR(45) NULL DEFAULT NULL,
`Player_Lastname` VARCHAR(45) NULL DEFAULT NULL,
`Player_Password` VARCHAR(45) NULL,
`Player_Budget` DOUBLE NULL,
`Terms_Id` INT NOT NULL,
PRIMARY KEY (`Player_Id`),
INDEX `fk_player_terms_and_conditions1_idx` (`Terms_Id` ASC),
CONSTRAINT `fk_player_player_result1`
FOREIGN KEY (`Player_Id`)
REFERENCES `boat`.`player_result` (`Player_Id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_player_supervisor_player1`
FOREIGN KEY (`Player_Id`)
REFERENCES `boat`.`supervisor_player` (`Player_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_player_terms_and_conditions1`
FOREIGN KEY (`Terms_Id`)
REFERENCES `boat`.`terms_and_conditions` (`Terms_And_Conditions_Id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Problem was in the Primary Key in the Player Table. It was configured to act like a foreign key for the other tables.

Mysql Query Error #1005 150

I have a query but its showing error #1005 and 150 in details. Some innodb
How to get it right?
CREATE TABLE `iars` ( `id` int(10) unsigned NOT NULL auto_increment,
`SessionId` int(10) unsigned NOT NULL,
`TeacherId` int(10) unsigned NOT NULL,
`courseid` int(4) unsigned NOT NULL,
`semester` int(4) unsigned NOT NULL,
`PaperId` int(4) unsigned NOT NULL,
`groupid` int(4) unsigned NOT NULL,
`Type` varchar(4) default 1,
PRIMARY KEY (`id`),
UNIQUE KEY `Constrint_Index`
(`SessionId`,`TeacherId`,`courseid`,`semester`,`PaperId`,`groupid`),
KEY `FK_tid` (`TeacherId`),
KEY `FK_gid` (`GroupId`),
KEY `FK_pid` (`PaperId`),
CONSTRAINT `FK_gid` FOREIGN KEY (`GroupId`) REFERENCES `groups` (`id`) ON DELETE
CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_pid` FOREIGN KEY (`PaperId`) REFERENCES `papers` (`p_id`) ON DELETE
CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_ssessionid` FOREIGN KEY (`SessionId`) REFERENCES `sessions` (`id`) ON
DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_tid` FOREIGN KEY (`TeacherId`) REFERENCES `teachers` (`id`) ON DELETE
CASCADE ON UPDATE CASCADE )
From the MySQL manual:
You cannot issue DROP TABLE for an InnoDB table that is referenced by a FOREIGN KEY constraint, unless you do SET foreign_key_checks = 0. When you drop a table, the constraints that were defined in its create statement are also dropped.
If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.
This means one of your other tables is probably referencing a column in iars which you are not re-creating. The solution, then, is to weed through all of your tables (via describe queries) and see where the reference is and either add the referenced column to your CREATE TABLE here, or remove the reference, as appropriate for your schema.
edit a very helpful gem from later on that referenced page:
SHOW ENGINE INNODB STATUS;
reveals loads of good info on the foreign-key error which just occurred. It should point you exactly where you need to look.