SQL : ERROR 1005: Can't create table 'obl2.itemsubjects' (errno: 121) - mysql

I have the following tables:
CREATE TABLE `OBL2`.`item` (
`itemID` INT NOT NULL AUTO_INCREMENT ,
`itemName` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`itemID`) ,
INDEX `itemName` (`itemName` ASC) );
CREATE TABLE `OBL2`.`subject` (
`subjectID` INT NOT NULL ,
`subjectName` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`subjectID`) );
Now since the connection is many to many, each item can have many subject and each subject can be related to many items - I'd like to set a connection table.
This is my code:
CREATE TABLE `OBL2`.`itemsubjects` (
`itemID` INT NOT NULL ,
`subjectID` INT NOT NULL ,
PRIMARY KEY (`itemID`, `subjectID`) ,
INDEX `itemID_idx` (`itemID` ASC) ,
INDEX `subjectID_idx` (`subjectID` ASC) ,
CONSTRAINT `itemID`
FOREIGN KEY (`itemID` )
REFERENCES `OBL2`.`item` (`itemID` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `subjectID`
FOREIGN KEY (`subjectID` )
REFERENCES `OBL2`.`subject` (`subjectID` )
ON DELETE CASCADE
ON UPDATE CASCADE);
but for some reason the code of the 3rd table is not being accepted.
I get an error message:
ERROR 1005: Can't create table 'obl2.itemsubjects' (errno: 121)
I've read about the error on the internet and it says it's a known issue of MYSQL yet there are no solutions.
Any thoughts?

The MySQL docs say in FOREIGN KEY Constraints (emphasis mine):
If the CONSTRAINT symbol clause is given, the symbol value must be unique in the database. If the clause is not given, InnoDB creates the name automatically.
So, the reason that the itemsubject table creation failed, was that you had another (foreign key) constraint, named itemID, or one named subjectID in some other table of the database.
It's good to have a naming conevntion that is standard across the database. Just as you have ColumnName_idx for indices, you can use ReferencedTable_ReferencingTable_FK for foreign key constraints:
CREATE TABLE OBL2.itemsubjects (
itemID INT NOT NULL ,
subjectID INT NOT NULL ,
PRIMARY KEY
(itemID, subjectID) ,
INDEX itemID_idx -- I like these
(itemID ASC) ,
INDEX subjectID_idx -- two
(subjectID ASC) ,
CONSTRAINT item_itemsubject_FK -- what I propose, here
FOREIGN KEY (itemID)
REFERENCES OBL2.item (itemID)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT subject_itemsubject_FK -- and here
FOREIGN KEY (subjectID)
REFERENCES OBL2.subject (subjectID)
ON DELETE CASCADE
ON UPDATE CASCADE
);

Related

mysql error 150 from referencing same foreign key column in two tables

I have looked through quite a few posts but havent found the solution for my problem. My suspicion is the error stems from me trying to use a single column to reference the same primary key column in two different tables. Specifically the bid table has the foreign key simulation_id that is also present in the bidder and item_round_status tables. the bid table references the foreign keys of both of these tables but I would like to use only one simulation_id column in the table. Is this the source of the Error 150 problem?
-- -----------------------------------------------------
-- Table `kffg_simulations`.`item_round_status`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `kffg_simulations`.`item_round_status` (
`simulation_id` INT NOT NULL ,
`round` INT NOT NULL ,
`clock_item_id` INT NOT NULL ,
`posted_price` BIGINT NOT NULL ,
`clock_price` BIGINT NOT NULL ,
PRIMARY KEY (`simulation_id`, `round`, `clock_item_id`) ,
INDEX `fk_item_round_status_clock_item1_idx` (`clock_item_id` ASC) ,
INDEX `fk_item_round_status_simulation1_idx` (`simulation_id` ASC) ,
CONSTRAINT `fk_item_round_status_clock_item1`
FOREIGN KEY (`clock_item_id`)
REFERENCES `kffg_simulations`.`clock_item` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_item_round_status_simulation1`
FOREIGN KEY (`simulation_id`)
REFERENCES `kffg_simulations`.`simulation` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `kffg_simulations`.`bidder`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `kffg_simulations`.`bidder` (
`simulation_id` INT NOT NULL ,
`idx` INT NOT NULL ,
`bidder_strategy_id` INT NOT NULL ,
`budget` BIGINT NOT NULL ,
PRIMARY KEY (`simulation_id`, `idx`) ,
INDEX `fk_bidder_simulation1_idx` (`simulation_id` ASC) ,
INDEX `fk_bidder_bidder_strategy1_idx` (`bidder_strategy_id` ASC) ,
CONSTRAINT `fk_bidder_simulation1`
FOREIGN KEY (`simulation_id`)
REFERENCES `kffg_simulations`.`simulation` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bidder_bidder_strategy1`
FOREIGN KEY (`bidder_strategy_id`)
REFERENCES `kffg_simulations`.`bidder_strategy` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `kffg_simulations`.`bid_type`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `kffg_simulations`.`bid_type` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `kffg_simulations`.`bid_status`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `kffg_simulations`.`bid_status` (
`id` INT NOT NULL AUTO_INCREMENT ,
`description` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `kffg_simulations`.`bid`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `kffg_simulations`.`bid` (
`simulation_id` INT NOT NULL ,
`item_round_status_round` INT NOT NULL ,
`clock_item_id` INT NOT NULL ,
`bidder_idx` INT NOT NULL ,
`quantity` INT NOT NULL ,
`price` INT NOT NULL ,
`bid_type_id` INT NOT NULL ,
`switch_to_pea_category_id` INT NOT NULL ,
`backstop` BIGINT NULL ,
`bid_status_id` INT NOT NULL ,
`processed_demand` INT NOT NULL ,
PRIMARY KEY (`simulation_id`, `item_round_status_round`, `clock_item_id`, `bidder_idx`, `quantity`) ,
INDEX `fk_bid_item_round_status1_idx` (`simulation_id` ASC, `item_round_status_round` ASC, `clock_item_id` ASC) ,
INDEX `fk_bid_bidder1_idx` (`simulation_id` ASC, `bidder_idx` ASC) ,
INDEX `fk_bid_bid_type1_idx` (`bid_type_id` ASC) ,
INDEX `fk_bid_pea_category1_idx` (`switch_to_pea_category_id` ASC) ,
INDEX `fk_bid_bid_status1_idx` (`bid_status_id` ASC) ,
CONSTRAINT `fk_bid_item_round_status1`
FOREIGN KEY (`simulation_id` , `item_round_status_round` , `clock_item_id`)
REFERENCES `kffg_simulations`.`item_round_status` (`simulation_id` , `round` , `clock_item_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bid_bidder1`
FOREIGN KEY (`bidder_idx` , `simulation_id`)
REFERENCES `kffg_simulations`.`bidder` (`idx` , `simulation_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bid_bid_type1`
FOREIGN KEY (`bid_type_id`)
REFERENCES `kffg_simulations`.`bid_type` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bid_pea_category1`
FOREIGN KEY (`switch_to_pea_category_id`)
REFERENCES `kffg_simulations`.`pea_category` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bid_bid_status1`
FOREIGN KEY (`bid_status_id`)
REFERENCES `kffg_simulations`.`bid_status` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Updated to show error message:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
170604 21:52:27 Error in foreign key constraint of table kffg_simulations/bid:
FOREIGN KEY (`simulation_id` , `item_round_status_round` , `clock_item_id`)
REFERENCES `kffg_simulations`.`item_round_status` (`simulation_id` , `round` , `clock_item_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bid_bidder1`
FOREIGN KEY (`bidder_idx` , `simulation_id`)
REFERENCES `kffg_simulations`.`bidder` (`idx` , `simulation_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bid_bid_type1`
FOREIGN KEY (`bid_type_id`)
REFERENCES `kffg_simulations`.`bid_type` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bid_pea_category1`
FOREIGN KEY (`switch_to_pea_category_id`)
REFERENCES `kffg_simulations`.`pea_category` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bid_bid_status1`
FOREIGN KEY (`bid_status_id`)
REFERENCES `kffg_simulations`.`bid_status` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
Also updated with uml diagram:
Foreign Key Usage and Error Information gives info on FKs (foreign keys).
you can obtain a detailed explanation of the most recent InnoDB foreign key error by checking the output of SHOW ENGINE INNODB STATUS.
InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.
In bid:
FOREIGN KEY (`bidder_idx` , `simulation_id`)
REFERENCES `kffg_simulations`.`bidder` (`idx` , `simulation_id`)
The "referenced table" here is bidder, the "referenced columns" list is (idx , simulation_id).
Cannot find an index in the referenced table where the
referenced columns appear as the first columns,
Sure enough, in bidder the closest we find is:
PRIMARY KEY (`simulation_id`, `idx`) ,
which implicitly declares a default unique not null index, but like all the other indexes doesn't start with the FK's column list.
Philipxy thank you for your help on this problem. You were correct in the comment that the foreign key for bidder was wrong. For some reason mysql workbench generated the columns in the code in the wrong order. The code provided by mysqlworkbench is below:
CONSTRAINT `fk_bid_bidder1`
FOREIGN KEY (`bidder_idx` , `simulation_id`)
REFERENCES `kffg_simulations`.`bidder` (`idx` , `simulation_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
And the following code works properly:
CONSTRAINT `fk_assignment_bidder1`
FOREIGN KEY (`bidder_simulation_id` , `bidder_idx`)
REFERENCES `kffg_simulations`.`bidder` (`simulation_id` , `idx`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
As far as I can tell I had the indexing setup properly but the code was generate in the wrong order.
(I used mysql workbench forward engineer to generate the sql script. I used the gui to create the relationships between the bid table, bidder, and item_round_status. Since both of those tables have simulationid as pk it duplicated that column. When I manually adjusted the foreign key for bidder to reference the simulationid column generated for item_round_status it changed the indices related to that foreign key. I manually changed them to the proper indices but it seems to ignore my changes when generating the script causing the error.)

Mysql composite key out of a foreign composite key

I'm trying to make a many-to-many relationship between two tables In Mysql WorkBench, and one of those 2 tables has a composite primary key ( parts are coming from 2 foreign keys). When I'm trying to generate the SQL I'm getting this error :
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
-- -----------------------------------------------------
-- Table `A_D_schema`.`Resources_has_OwnerGroups`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `A_D_schema`.`Resources_has_OwnerGroups` (
`Resources_id` INT NOT NULL,
`OwnerGroups_id` INT NOT NULL,
`OwnerGroups_Instances_has_Customers_Instances_idInstances` INT NOT NULL,
`OwnerGroups_Instances_has_Customers_Customers_idCustomers` INT NOT NULL,
PRIMARY KEY (`Resources_id`, `OwnerGroups_id`, `OwnerGroups_Instances_has_Customers_Instances_idInstances`, `OwnerGroups_Instances_has_Customers_Customers_idCustomers`),
INDEX `fk_Resources_has_OwnerGroups_OwnerGroups1_idx` (`OwnerGroups_id` ASC, `OwnerGroups_Instances_has_Customers_Instances_idInstances` ASC, `OwnerGroups_Instances_has_Customers_Customers_idCustomers` ASC),
INDEX `fk_Resources_has_OwnerGroups_Resources1_idx` (`Resources_id` ASC),
CONSTRAINT `fk_Resources_has_OwnerGroups_Resources1`
FOREIGN KEY (`Resources_id`)
REFERENCES `A_D_schema`.`Resources` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Resources_has_OwnerGroups_OwnerGroups1`
FOREIGN KEY (`OwnerGroups_id` , `OwnerGroups_Instances_has_Customers_Instances_idInstances` , `OwnerGroups_Instances_has_Customers_Customers_idCustomers`)
REFERENCES `A_D_schema`.`OwnerGroups` (`id` , `Instances_has_Customers_Instances_idInstances` , `Instances_has_Customers_Customers_idCustomers`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
From the SHOW ENGINE INNODB STATUS I can see this message :
Cannot resolve column name close to:
)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Resources_has_OwnerGroups_OwnerGroups1`
FOREIGN KEY (`OwnerGroups_id` , `OwnerGroups_Instances_has_Customers_Instances_idInstances` , `OwnerGroups_Instances_has_Customers_Customers_idCustomers`)
REFERENCES `A_D_schema`.`OwnerGroups` (`id` , `Instances_has_Customers_Instances_idInstances` , `Instances_has_Customers_Customers_idCustomers`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
The SHOW CREATE TABLE Resources and SHOW CREATE TABLE OwnerGroups :
CREATE TABLE `Resources` (
`idResources` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(45) DEFAULT NULL,
`role` int(11) DEFAULT NULL COMMENT 'role : 1 disptcher \n0 admin',
PRIMARY KEY (`idResources`),
UNIQUE KEY `idresources_UNIQUE` (`idResources`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `OwnerGroups` (
`idOwnerGroups` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`group` int(11) DEFAULT NULL,
PRIMARY KEY (`idOwnerGroups`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CONSTRAINT `fk_Resources_has_OwnerGroups_Resources1`
FOREIGN KEY (`Resources_id`)
REFERENCES `A_D_schema`.`Resources` (`id`)
Your Resources table doesn't have a column id. Its primary key is idResources.
CONSTRAINT `fk_Resources_has_OwnerGroups_OwnerGroups1`
FOREIGN KEY (`OwnerGroups_id` , `OwnerGroups_Instances_has_Customers_Instances_idInstances` , `OwnerGroups_Instances_has_Customers_Customers_idCustomers`)
REFERENCES `A_D_schema`.`OwnerGroups` (`id` , `Instances_has_Customers_Instances_idInstances` , `Instances_has_Customers_Customers_idCustomers`)
Your OwnerGroups table doesn't have a column id. Its primary key is idOwnerGroups. It doesn't have the other two columns you reference at all.
In general, when you declare a foreign key, first you name the columns in the child table:
CREATE TABLE Child (
childCol1 INT,
childCol2 INT,
...
FOREIGN KEY (childCol1, childCol2) ...
Then you reference columns in the parent table:
... REFERENCES Parent (parentCol1, parentCol2)
);
You must use the names of columns as they exist in the parent table.
The columns you reference in the parent table must together be the PRIMARY KEY or UNIQUE KEY of that table. In other words, given the example above, it would not work against this Parent table:
CREATE TABLE Parent (
parentCol1 INT,
parentCol2 INT,
PRIMARY KEY (parentCol1)
);
Because the PRIMARY KEY does not include parentCol2.
In your case, the following should work:
CREATE TABLE IF NOT EXISTS `A_D_schema`.`Resources_has_OwnerGroups` (
`Resources_id` INT NOT NULL,
`OwnerGroups_id` INT NOT NULL,
`OwnerGroups_Instances_has_Customers_Instances_idInstances` INT NOT NULL,
`OwnerGroups_Instances_has_Customers_Customers_idCustomers` INT NOT NULL,
PRIMARY KEY (`Resources_id`, `OwnerGroups_id`, `OwnerGroups_Instances_has_Customers_Instances_idInstances`, `OwnerGroups_Instances_has_Customers_Customers_idCustomers`),
CONSTRAINT `fk_Resources_has_OwnerGroups_Resources1`
FOREIGN KEY (`Resources_id`)
REFERENCES `A_D_schema`.`Resources` (`idResources`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Resources_has_OwnerGroups_OwnerGroups1`
FOREIGN KEY (`OwnerGroups_id`)
REFERENCES `A_D_schema`.`OwnerGroups` (`idOwnerGroups`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
) ENGINE = InnoDB
I took out a couple of INDEX definitions that are redundant. You don't need to index your PRIMARY KEY, it's already the clustered index of the table. You don't need to index the column you use in a foreign key declaration, MySQL will index that column automatically if it need to (though if an index already exists for that column, the FK constraint will use that index).
I'm not sure I understand what your other two columns OwnerGroups_Instances_has_Customers_Instances_idInstances and OwnerGroups_Instances_has_Customers_Customers_idCustomers are meant to do. Typically in a many-to-many table, you only need enough columns to reference the primary keys of the respective parent tables.
Re your comment:
You should try refreshing the view of the schema from time to time. There's a button with a pair of curvy arrows, to the right of "SCHEMAS".

Why can't I create my table? Why is this FK constraint malformed?

I'm getting the error below when I attempt to create a table with a couple foreign keys. It goes smoothly if I only include the userID foreign key, but complains when I try to get the other foreign key in there as well. What's the deal?
ERROR 1005: Can't create table 'ps5_lwilkins.PhoneNumber' (errno: 150)
SQL Statement:
CREATE TABLE `ps5_lwilkins`.`PhoneNumber` (
`userID` CHAR(25) NOT NULL ,
`resumeID` CHAR(30) NOT NULL ,
`number` CHAR(45) NOT NULL ,
PRIMARY KEY (`userID`, `resumeID`, `number`) ,
INDEX `user_phoneNumber_fk1` (`userID` ASC) ,
INDEX `resume_phoneNumber_fk3` (`resumeID` ASC) ,
CONSTRAINT `user_phoneNumber_fk1`
FOREIGN KEY (`userID` )
REFERENCES `ps5_lwilkins`.`User` (`userID` )
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `resume_phoneNumber_fk3`
FOREIGN KEY (`resumeID` )
REFERENCES `ps5_lwilkins`.`Resume` (`resumeID` )
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB
Here's the schema for User:
User(userid: CHAR(25), pass: VARCHAR(25)) userid is the PK
Here's the schema for Resume:
Resume(userID: CHAR(25), resumeID: CHAR(30)
few other none important attributes). PK is (userID, resumeID).
Any ideas? Need more information?
Looking at other problems similar to this, I think it's somehow a malformed FK on the resumeID... but I can't see where!
I've tried several different names for the FK constraint btw.
EDIT: I don't have the privilege to perform SHOW ENGINE INNODB STATUS
You're on the right track with your comment above. You need to change this:
INDEX `user_phoneNumber_fk1` (`userID` ASC) ,
INDEX `resume_phoneNumber_fk3` (`resumeID` ASC) ,
and this:
FOREIGN KEY (`resumeID` )
REFERENCES `ps5_lwilkins`.`Resume` (`resumeID` )
to this:
INDEX `phoneNumber_fks` (`userID` ASC, `resumeID` ASC) ,
and this:
FOREIGN KEY (`userID`, `resumeID` )
REFERENCES `ps5_lwilkins`.`Resume` (`userID`, `resumeID` )

MySQL create table causing error

I am having trouble creating a table with MySQL. I have narrowed the problem down to the enrolled table.
The error I get is: #1005 - Can't create table 'mcems.enrolled' (errno: 150)
http://pastebin.com/9cUkgs8p
CRN field should be the same type in both tables, MCEMS.courses and MCEMS.enrolled; otherwise, you cannot use it in FOREIGN KEY. You have
CREATE TABLE IF NOT EXISTS `MCEMS`.`courses` (
`CRN` INT NOT NULL , ...)
and
CREATE TABLE IF NOT EXISTS `MCEMS`.`enrolled` (
`user_id` INT NOT NULL ,
`CRN` VARCHAR(45) , -- must be INT (also NOT NULL because it's part of PK)!!!
PRIMARY KEY (`user_id`, `CRN`) ,
INDEX `user_id` (`user_id` ASC) ,
INDEX `CRN` (`CRN` ASC) ,
FOREIGN KEY (`user_id`)
REFERENCES `MCEMS`.`users` (`user_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (`CRN`)
REFERENCES `MCEMS`.`courses` (`CRN`)
ON DELETE CASCADE
ON UPDATE CASCADE)
Also, absolutely no need for INDEX user_id (user_id ASC)

SQL - error code 1005 with error number 121

I'm running the following MySQL query (trimmed down), generated automatically by MySQL Workbench and I get the following error:
Error Code: 1005
Can't create table 'regula.reservation' (errno: 121)
I'm not very proficient with databases and this error is not very informative.
What is the problem here?
-- -----------------------------------------------------
-- Table `regula`.`Users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `regula`.`Users` ;
CREATE TABLE IF NOT EXISTS `regula`.`Users` (
`idUsers` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` TEXT NOT NULL ,
`type` TEXT NOT NULL ,
`pwd` TEXT NOT NULL ,
PRIMARY KEY (`idUsers`) ,
UNIQUE INDEX `idUsers_UNIQUE` (`idUsers` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regula`.`Projects`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `regula`.`Projects` ;
CREATE TABLE IF NOT EXISTS `regula`.`Projects` (
`idProjects` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`ownerId` INT UNSIGNED NOT NULL ,
`name` TEXT NOT NULL ,
`date` DATE NOT NULL ,
`time` TIME NOT NULL ,
`place` TEXT NOT NULL ,
`itemType` INT NOT NULL ,
PRIMARY KEY (`idProjects`) ,
UNIQUE INDEX `idProjects_UNIQUE` (`idProjects` ASC) ,
INDEX `ownerId` (`ownerId` ASC) ,
CONSTRAINT `ownerId`
FOREIGN KEY (`ownerId` )
REFERENCES `regula`.`Users` (`idUsers` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regula`.`ItemTypes`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `regula`.`ItemTypes` ;
CREATE TABLE IF NOT EXISTS `regula`.`ItemTypes` (
`idItemTypes` INT UNSIGNED NOT NULL ,
`prjId` INT UNSIGNED NOT NULL ,
`parentId` INT UNSIGNED NULL DEFAULT NULL ,
`name` TEXT NOT NULL ,
PRIMARY KEY (`idItemTypes`) ,
INDEX `prjId` (`prjId` ASC) ,
INDEX `parentId` (`parentId` ASC) ,
CONSTRAINT `prjId`
FOREIGN KEY (`prjId` )
REFERENCES `regula`.`Projects` (`idProjects` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `parentId`
FOREIGN KEY (`parentId` )
REFERENCES `regula`.`ItemTypes` (`idItemTypes` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regula`.`Reservation`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `regula`.`Reservation` ;
CREATE TABLE IF NOT EXISTS `regula`.`Reservation` (
`idReservation` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`prjId` INT UNSIGNED NOT NULL ,
`itemTypeId` INT UNSIGNED NOT NULL ,
`userId` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`idReservation`) ,
INDEX `prjId` (`prjId` ASC) ,
INDEX `itemTypeId` (`itemTypeId` ASC) ,
INDEX `userId` (`userId` ASC) ,
CONSTRAINT `prjId`
FOREIGN KEY (`prjId` )
REFERENCES `regula`.`Projects` (`idProjects` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `itemTypeId`
FOREIGN KEY (`itemTypeId` )
REFERENCES `regula`.`ItemTypes` (`idItemTypes` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `userId`
FOREIGN KEY (`userId` )
REFERENCES `regula`.`Users` (`idUsers` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Error 121 means that there is a foreign key constraint error. Since you're using InnoDB, you can use SHOW ENGINE INNODB STATUS after running the failed query to get an explanation in the LATEST FOREIGN KEY ERROR section. Having run your SQL myself, I get this:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
101210 14:55:50 Error in foreign key constraint creation for table `regula`.`Reservation`.
A foreign key constraint of name `regula`.`prjId`
already exists. (Note that internally InnoDB adds 'databasename'
in front of the user-defined constraint name.)
Note that InnoDB's FOREIGN KEY system tables store
constraint names as case-insensitive, with the
MySQL standard latin1_swedish_ci collation. If you
create tables or databases whose names differ only in
the character case, then collisions in constraint
names can occur. Workaround: name your constraints
explicitly with unique names.
Basically, you need to give your prjId constraint name a unique name in the last table. Constraint/foreign key names are global to a database, so they cannot be reused in different tables. Just change the last
CONSTRAINT `prjId`
to
CONSTRAINT `prjId2`
The error code 121 comes when you try to map the foreign key.
Basically it comes when your foreign key name already exists in the database.
For example:
ALTER TABLE `photokiosk`.`kiosk_event`
ADD CONSTRAINT `event_booking_id`
FOREIGN KEY `event_booking_id` (`event_booking_id`)
REFERENCES `event_booking` (`event_booking_id`)
If foreign key with the name event_booking_id is already mapped with the other table.
To get rid of this issue, please change the foreign key name, not the column name.
You will get this error message if you try to use a constraint name which already exists in the table.
Here 'prjId' already exists in table regula.ItemTypes. So, you can't use the same constraint name on table 'regula.reservation' again.