Foreign Key Constraint - (ERROR 1215). Please assist:) - mysql

I have looked at other questions on here and can't seem to find the answer I am looking for. I am attempting to make a SQL database with AUTO_INCREMENT set against the ID of each table. I have matched the data types from the foreign ID of a table to the primary key of a table. The errors occur on the following tables (only the ones that have foreign keys): NUMBERS, CUSTOMER, TRUNK, TRUNK_GROUP
The error received on these tables is:
ERROR 1215 (HY000): Cannot add foreign key constraint
Below is the code used. Wonder if anyone has any suggestions?
CREATE DATABASE IF NOT EXISTS `NOAS_DATABASE` DEFAULT CHARACTER SET utf8 ;
USE `NOAS_DATABASE` ;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`IP_ADDRESSES`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`IP_ADDRESSES` (
`IP_ID` INT(10) NOT NULL AUTO_INCREMENT,
`START_IP_RANGE` LONGBLOB NULL,
`END_IP_RANGE` LONGBLOB NULL,
PRIMARY KEY (`IP_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`NO_RANGE`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`NO_RANGE` (
`RANGE_ID` INT(10) NOT NULL AUTO_INCREMENTL,
`START_NO_RANGE` LONGBLOB NULL,
`END_NO_RANGE` LONGBLOB NULL,
PRIMARY KEY (`RANGE_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`NUMBERS`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`NUMBERS` (
`NUM_ID` INT(10) NOT NULL AUTO_INCREMENT,
`IP_ID` VARCHAR(20) NULL,
`RANGE_ID` VARCHAR(20) NULL,
`CALL_BARRING_STATUS` TEXT(10) NULL,
`ANONYMOUS_CALL_REJECT` TEXT(20) NULL,
`CALL_DIVERT` TEXT(20) NULL,
`CALL_DIVERT_DEST_NO` LONGBLOB NULL,
PRIMARY KEY (`NUM_ID`),
INDEX `IP_ID_idx` (`IP_ID` ASC) VISIBLE,
INDEX `RANGE_ID_idx` (`RANGE_ID` ASC) VISIBLE,
CONSTRAINT `IP_ID`
FOREIGN KEY (`IP_ID`)
REFERENCES `NOAS_DATABASE`.`IP_ADDRESSES` (`IP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `RANGE_ID`
FOREIGN KEY (`RANGE_ID`)
REFERENCES `NOAS_DATABASE`.`NO_RANGE` (`RANGE_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`SERVICE`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`SERVICE` (
`SERVICE_ID` INT(10) NOT NULL AUTO_INCREMENT,
`SERVICE_STATUS` VARCHAR(20) NULL,
`DOMAIN_NAME` LONGBLOB NULL,
PRIMARY KEY (`SERVICE_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`CUSTOMER`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`CUSTOMER` (
`CUSTOMER_ID` INT(10) NOT NULL AUTO_INCREMENT,
`CUST_NETWORK_SET` TEXT(20) NULL,
`BILLING_ID` TEXT(20) NULL,
`LOCATION` TEXT(20) NULL,
`SERVICE_ID` VARCHAR(20) NULL,
PRIMARY KEY (`CUSTOMER_ID`),
INDEX `SERVICE_ID_idx` (`SERVICE_ID` ASC) VISIBLE,
CONSTRAINT `SERVICE_ID`
FOREIGN KEY (`SERVICE_ID`)
REFERENCES `NOAS_DATABASE`.`SERVICE` (`SERVICE_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`NETWORK_SET`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`NETWORK_SET` (
`NETWORK_SET_OSS_ID` INT(10) NOT NULL AUTO_INCREMENT,
`PRIORITY_NOAS` TEXT(20) NULL,
`PRIORITY_SBC` TEXT(20) NULL,
PRIMARY KEY (`NETWORK_SET_OSS_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`TRUNK_GROUP`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`TRUNK_GROUP` (
`TRUNK_GROUP_ID` INT(10) NOT NULL AUTO_INCREMENT,
`CUSTOMER_ID` VARCHAR(20) NULL,
`TRUNK_ID` VARCHAR(20) NULL,
PRIMARY KEY (`TRUNK_GROUP_ID`),
INDEX `CUSTOMER_ID_idx` (`CUSTOMER_ID` ASC) VISIBLE,
INDEX `TRUNK_ID_idx` (`TRUNK_ID` ASC) VISIBLE,
CONSTRAINT `CUSTOMER_ID`
FOREIGN KEY (`CUSTOMER_ID`)
REFERENCES `NOAS_DATABASE`.`CUSTOMER` (`CUSTOMER_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `TRUNK_ID`
FOREIGN KEY (`TRUNK_ID`)
REFERENCES `NOAS_DATABASE`.`TRUNK` (`TRUNK_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`TRUNK`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`TRUNK` (
`TRUNK_ID` INT(10) NOT NULL AUTO_INCREMENT,
`TRUNK_GROUP_ID` VARCHAR(20) NULL,
`NETWORK_SET_OSS_ID` VARCHAR(20) NULL,
`NUM_ID` VARCHAR(20) NULL,
`TRUNK_SERVICE_STATUS` TEXT(20) NULL,
`TRUNK_GROUP_PRIORITY` TEXT(20) NULL,
`TRUNK_CAC_LIMIT` TEXT(20) NULL,
`HANDOVER_FORMAT` TEXT(20) NULL,
PRIMARY KEY (`TRUNK_ID`),
INDEX `NETWORK_SET_OSS_ID_idx` (`NETWORK_SET_OSS_ID` ASC) VISIBLE,
INDEX `NUM_ID_idx` (`NUM_ID` ASC) VISIBLE,
INDEX `TRUNK_GROUP_ID_idx` (`TRUNK_GROUP_ID` ASC) VISIBLE,
CONSTRAINT `NETWORK_SET_OSS_ID`
FOREIGN KEY (`NETWORK_SET_OSS_ID`)
REFERENCES `NOAS_DATABASE`.`NETWORK_SET` (`NETWORK_SET_OSS_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `NUM_ID`
FOREIGN KEY (`NUM_ID`)
REFERENCES `NOAS_DATABASE`.`NUMBERS` (`NUM_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `TRUNK_GROUP_ID`
FOREIGN KEY (`TRUNK_GROUP_ID`)
REFERENCES `NOAS_DATABASE`.`TRUNK_GROUP` (`TRUNK_GROUP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

There were minor issues in multiple tables. Here're some corrections:
Switch to the right database
USE `NOAS_DATABASE` ;
IP Address table is just fine
CREATE TABLE IF NOT EXISTS `IP_ADDRESSES` (
`IP_ID` INT(10) NOT NULL AUTO_INCREMENT,
`START_IP_RANGE` LONGBLOB NULL,
`END_IP_RANGE` LONGBLOB NULL,
PRIMARY KEY (`IP_ID`))
ENGINE = InnoDB;
No Range table
There was a type in AUTO_INCREMENT as commentor Dan mentioned. Below is the corrected version.
CREATE TABLE IF NOT EXISTS `NO_RANGE` (
`RANGE_ID` INT(10) NOT NULL AUTO_INCREMENT, -- fixed typo
`START_NO_RANGE` LONGBLOB NULL,
`END_NO_RANGE` LONGBLOB NULL,
PRIMARY KEY (`RANGE_ID`))
ENGINE = InnoDB;
Numbers had datatype issues
IP_ID and Range_ID should be INT. That's been corrected as follows.
CREATE TABLE IF NOT EXISTS `NUMBERS` (
`NUM_ID` INT(10) NOT NULL AUTO_INCREMENT,
`IP_ID` INT(10) NULL, -- fixed datatype
`RANGE_ID` INT(10) NULL, -- fixed datatype
`CALL_BARRING_STATUS` TEXT(10) NULL,
`ANONYMOUS_CALL_REJECT` TEXT(20) NULL,
`CALL_DIVERT` TEXT(20) NULL,
`CALL_DIVERT_DEST_NO` LONGBLOB NULL,
PRIMARY KEY (`NUM_ID`),
INDEX `IP_ID_idx` (`IP_ID` ASC),
INDEX `RANGE_ID_idx` (`RANGE_ID` ASC),
CONSTRAINT `IP_ID`
FOREIGN KEY (`IP_ID`)
REFERENCES `IP_ADDRESSES` (`IP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `RANGE_ID`
FOREIGN KEY (`RANGE_ID`)
REFERENCES `NO_RANGE` (`RANGE_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Service table is good
CREATE TABLE IF NOT EXISTS `SERVICE` (
`SERVICE_ID` INT(10) NOT NULL AUTO_INCREMENT,
`SERVICE_STATUS` VARCHAR(20) NULL,
`DOMAIN_NAME` LONGBLOB NULL,
PRIMARY KEY (`SERVICE_ID`))
ENGINE = InnoDB;
Customer table had datatype issue
Service_ID should be an INT. Corrected below.
CREATE TABLE IF NOT EXISTS `CUSTOMER` (
`CUSTOMER_ID` INT(10) NOT NULL AUTO_INCREMENT,
`CUST_NETWORK_SET` TEXT(20) NULL,
`BILLING_ID` TEXT(20) NULL,
`LOCATION` TEXT(20) NULL,
`SERVICE_ID` INT(10) NULL, -- fixed datatype
PRIMARY KEY (`CUSTOMER_ID`),
INDEX `SERVICE_ID_idx` (`SERVICE_ID` ASC) ,
CONSTRAINT `SERVICE_ID`
FOREIGN KEY (`SERVICE_ID`)
REFERENCES `SERVICE` (`SERVICE_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Network set is good
CREATE TABLE IF NOT EXISTS `NETWORK_SET` (
`NETWORK_SET_OSS_ID` INT(10) NOT NULL AUTO_INCREMENT,
`PRIORITY_NOAS` TEXT(20) NULL,
`PRIORITY_SBC` TEXT(20) NULL,
PRIMARY KEY (`NETWORK_SET_OSS_ID`))
ENGINE = InnoDB;
Trunk group had reference to a table that wasn't yet created
Trunk table was not created yet and Trunk_Group was referring to it. Removed that reference. Consider the possibility of removing trunk_id from trunk_group field.
CREATE TABLE IF NOT EXISTS `TRUNK_GROUP` (
`TRUNK_GROUP_ID` INT(10) NOT NULL AUTO_INCREMENT,
`CUSTOMER_ID` INT(10) NULL,
`TRUNK_ID` INT(10) NULL, -- consider removing this field
PRIMARY KEY (`TRUNK_GROUP_ID`),
INDEX `CUSTOMER_ID_idx` (`CUSTOMER_ID` ASC) ,
INDEX `TRUNK_ID_idx` (`TRUNK_ID` ASC) ,
CONSTRAINT `CUSTOMER_ID`
FOREIGN KEY (`CUSTOMER_ID`)
REFERENCES `CUSTOMER` (`CUSTOMER_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
-- removed trunk_id reference to trunk
)
ENGINE = InnoDB;
Trunk table's datatypes were fixed
Trunk_Group_ID and Network_Set_OSS_ID were switched to INT.
CREATE TABLE IF NOT EXISTS `TRUNK` (
`TRUNK_ID` INT(10) NOT NULL AUTO_INCREMENT,
`TRUNK_GROUP_ID` INT(10) NULL, -- fixed datatype
`NETWORK_SET_OSS_ID` INT(10) NULL, -- fixed datatype
`NUM_ID` INT(10) NULL,
`TRUNK_SERVICE_STATUS` TEXT(20) NULL,
`TRUNK_GROUP_PRIORITY` TEXT(20) NULL,
`TRUNK_CAC_LIMIT` TEXT(20) NULL,
`HANDOVER_FORMAT` TEXT(20) NULL,
PRIMARY KEY (`TRUNK_ID`),
INDEX `NETWORK_SET_OSS_ID_idx` (`NETWORK_SET_OSS_ID` ASC) ,
INDEX `NUM_ID_idx` (`NUM_ID` ASC) ,
INDEX `TRUNK_GROUP_ID_idx` (`TRUNK_GROUP_ID` ASC),
CONSTRAINT `NETWORK_SET_OSS_ID`
FOREIGN KEY (`NETWORK_SET_OSS_ID`)
REFERENCES `NETWORK_SET` (`NETWORK_SET_OSS_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `NUM_ID`
FOREIGN KEY (`NUM_ID`)
REFERENCES `NUMBERS` (`NUM_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `TRUNK_GROUP_ID`
FOREIGN KEY (`TRUNK_GROUP_ID`)
REFERENCES `TRUNK_GROUP` (`TRUNK_GROUP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Avoid circular references
Trunk group and trunk were referring to each other. I'd recommend that you have the relationship for Trunk such that Trunk belongs to a Trunk group and just keep it at that. I also removed the visible keyword.
I ran this on MySQL 5.7 and it worked well.

Related

Error 1005 when Forward Engineering EER diagram

I have created an EER diagram (as shown in the image bellow) in MySQL Workbench and wanted to Forward Engineer to build the DB.
After configuring the model options to the Target MySQL Version and removing the word "Visible" from all the Indexes in the SQL Code (the code goes in the bottom of this post), as it was triggering an error, I have came across a 1005 Error:
Executing SQL script in server
ERROR: Error 1005: Can't create table `books`.`books` (errno: 150 "Foreign key constraint is incorrectly formed")
SQL Code:
-- -----------------------------------------------------
-- Table `books`.`books`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`books` (
`bookID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`price` DECIMAL(10,2) NULL,
`book_prices_book_priceID` INT(11) NOT NULL,
`book_types_book_typeID` INT(11) NOT NULL,
`transactions_transactionID` INT(11) NOT NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NOT NULL,
`ISBN` VARCHAR(13) NULL,
PRIMARY KEY (`bookID`),
INDEX `fk_books_book_prices1_idx` (`book_prices_book_priceID` ASC),
INDEX `fk_books_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_books_book_prices1`
FOREIGN KEY (`book_prices_book_priceID`)
REFERENCES `books`.`book_prices` (`book_priceID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_books_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 8 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Any guidance on how to solve it is appreciated.
The SQL Code that I am using is the following:
-- MySQL Workbench Forward Engineering
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema books
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema books
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `books` DEFAULT CHARACTER SET utf8 ;
USE `books` ;
-- -----------------------------------------------------
-- Table `books`.`book_prices`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`book_prices` (
`book_priceID` INT(11) NOT NULL AUTO_INCREMENT,
`bookID` INT(11) NULL,
`price` DECIMAL(10,2) NULL,
`currency` CHAR(2) NULL,
`date_start` DATETIME NULL,
`date_end` DATETIME NULL,
PRIMARY KEY (`book_priceID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`transaction_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`transaction_types` (
`transaction_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`transactionID` INT(11) NULL,
PRIMARY KEY (`transaction_typeID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`transactions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`transactions` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`transaction_types_transaction_typeID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_transactions_transaction_types1_idx` (`transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_transactions_transaction_types1`
FOREIGN KEY (`transaction_types_transaction_typeID`)
REFERENCES `books`.`transaction_types` (`transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`books`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`books` (
`bookID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`price` DECIMAL(10,2) NULL,
`book_prices_book_priceID` INT(11) NOT NULL,
`book_types_book_typeID` INT(11) NOT NULL,
`transactions_transactionID` INT(11) NOT NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NOT NULL,
`ISBN` VARCHAR(13) NULL,
PRIMARY KEY (`bookID`),
INDEX `fk_books_book_prices1_idx` (`book_prices_book_priceID` ASC),
INDEX `fk_books_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_books_book_prices1`
FOREIGN KEY (`book_prices_book_priceID`)
REFERENCES `books`.`book_prices` (`book_priceID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_books_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`batch_transaction`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`batch_transaction` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`batchID` INT(11) NOT NULL,
`transactions_transactionID` INT(11) NOT NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_batch_transaction_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_batch_transaction_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`batches`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`batches` (
`batchID` INT(11) NOT NULL AUTO_INCREMENT,
`batch_transaction_transactionID` INT(11) NULL,
`book_typeID` INT(11) NOT NULL,
`price` DECIMAL(10,2) NULL,
`supplierID` INT(11) NULL,
PRIMARY KEY (`batchID`),
INDEX `fk_batches_batch_transaction1_idx` (`batch_transaction_transactionID` ASC),
CONSTRAINT `fk_batches_batch_transaction1`
FOREIGN KEY (`batch_transaction_transactionID`)
REFERENCES `books`.`batch_transaction` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`book_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`book_types` (
`book_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`books_bookID` INT(11) NOT NULL,
`books_book_prices_book_priceID` INT(11) NOT NULL,
`books_book_types_book_typeID` INT(11) NOT NULL,
`batches_batchID` INT(11) NOT NULL,
PRIMARY KEY (`book_typeID`),
INDEX `fk_product_types_products1_idx` (`books_bookID` ASC, `books_book_prices_book_priceID` ASC, `books_book_types_book_typeID` ASC),
INDEX `fk_product_types_batches1_idx` (`batches_batchID` ASC),
CONSTRAINT `fk_book_types_books1`
FOREIGN KEY (`books_bookID` , `books_book_prices_book_priceID` , `books_book_types_book_typeID`)
REFERENCES `books`.`books` (`bookID` , `book_prices_book_priceID` , `book_types_book_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_book_types_batches1`
FOREIGN KEY (`batches_batchID`)
REFERENCES `books`.`batches` (`batchID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`suppliers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`suppliers` (
`supplierID` INT(11) NOT NULL AUTO_INCREMENT,
`batches_batchID` INT(11) NOT NULL,
`name` VARCHAR(255) NULL,
PRIMARY KEY (`supplierID`),
INDEX `fk_suppliers_batches1_idx` (`batches_batchID` ASC),
CONSTRAINT `fk_suppliers_batches1`
FOREIGN KEY (`batches_batchID`)
REFERENCES `books`.`batches` (`batchID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`customer_transaction`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`customer_transaction` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`customerID` INT(11) NULL,
`transactions_transactionID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_client_transaction_transactions1_idx` (`transactions_transactionID` ASC),
CONSTRAINT `fk_customer_transaction_transactions1`
FOREIGN KEY (`transactions_transactionID`)
REFERENCES `books`.`transactions` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`customers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`customers` (
`customerID` INT(11) NOT NULL AUTO_INCREMENT,
`books_bookID` INT(11) NULL,
`books_book_prices_book_priceID` INT(11) NOT NULL,
`books_book_types_book_typeID` INT(11) NOT NULL,
`customer_transaction_transactionID` INT(11) NOT NULL,
PRIMARY KEY (`customerID`),
INDEX `fk_clients_products1_idx` (`books_bookID` ASC, `books_book_prices_book_priceID` ASC, `books_book_types_book_typeID` ASC),
INDEX `fk_clients_client_transaction1_idx` (`customer_transaction_transactionID` ASC),
CONSTRAINT `fk_customrs_products1`
FOREIGN KEY (`books_bookID` , `books_book_prices_book_priceID` , `books_book_types_book_typeID`)
REFERENCES `books`.`books` (`bookID` , `book_prices_book_priceID` , `book_types_book_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_customers_customer_transaction1`
FOREIGN KEY (`customer_transaction_transactionID`)
REFERENCES `books`.`customer_transaction` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`discounts`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`discounts` (
`discountID` INT(11) NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NOT NULL,
`transactions_transactionID` INT(11) NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NULL,
PRIMARY KEY (`discountID`),
INDEX `fk_discounts_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_discounts_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`discount_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`discount_types` (
`discount_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`Type` VARCHAR(255) NULL,
`discounts_discountID` INT(11) NOT NULL,
PRIMARY KEY (`discount_typeID`),
INDEX `fk_discount_types_discounts1_idx` (`discounts_discountID` ASC),
CONSTRAINT `fk_discount_types_discounts1`
FOREIGN KEY (`discounts_discountID`)
REFERENCES `books`.`discounts` (`discountID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`storagedistribution`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`storagedistribution` (
`storagedistributionID` INT(11) NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NULL,
`transactions_transactionID` INT(11) NOT NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NOT NULL,
PRIMARY KEY (`storagedistributionID`),
INDEX `fk_storagedistribution_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_storagedistribution_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
Edit 1:
Removed transaction_typeID from both books batch_transaction tables and ran the following SQL Script:
-- MySQL Workbench Forward Engineering
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema books
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema books
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `books` DEFAULT CHARACTER SET utf8 ;
USE `books` ;
-- -----------------------------------------------------
-- Table `books`.`book_prices`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`book_prices` (
`book_priceID` INT(11) NOT NULL AUTO_INCREMENT,
`bookID` INT(11) NULL,
`price` DECIMAL(10,2) NULL,
`currency` CHAR(2) NULL,
`date_start` DATETIME NULL,
`date_end` DATETIME NULL,
PRIMARY KEY (`book_priceID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`transaction_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`transaction_types` (
`transaction_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`transactionID` INT(11) NULL,
PRIMARY KEY (`transaction_typeID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`transactions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`transactions` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`transaction_types_transaction_typeID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_transactions_transaction_types1_idx` (`transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_transactions_transaction_types1`
FOREIGN KEY (`transaction_types_transaction_typeID`)
REFERENCES `books`.`transaction_types` (`transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`books`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`books` (
`bookID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`price` DECIMAL(10,2) NULL,
`book_prices_book_priceID` INT(11) NOT NULL,
`book_types_book_typeID` INT(11) NOT NULL,
`transactions_transactionID` INT(11) NOT NULL,
`ISBN` VARCHAR(13) NULL,
PRIMARY KEY (`bookID`),
INDEX `fk_books_book_prices1_idx` (`book_prices_book_priceID` ASC),
INDEX `fk_books_transactions1_idx` (`transactions_transactionID` ASC),
CONSTRAINT `fk_books_book_prices1`
FOREIGN KEY (`book_prices_book_priceID`)
REFERENCES `books`.`book_prices` (`book_priceID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_books_transactions1`
FOREIGN KEY (`transactions_transactionID`)
REFERENCES `books`.`transactions` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`batch_transaction`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`batch_transaction` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`batchID` INT(11) NOT NULL,
`transactions_transactionID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_batch_transaction_transactions1_idx` (`transactions_transactionID` ASC),
CONSTRAINT `fk_batch_transaction_transactions1`
FOREIGN KEY (`transactions_transactionID`)
REFERENCES `books`.`transactions` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`batches`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`batches` (
`batchID` INT(11) NOT NULL AUTO_INCREMENT,
`batch_transaction_transactionID` INT(11) NULL,
`book_typeID` INT(11) NOT NULL,
`price` DECIMAL(10,2) NULL,
`supplierID` INT(11) NULL,
PRIMARY KEY (`batchID`),
INDEX `fk_batches_batch_transaction1_idx` (`batch_transaction_transactionID` ASC),
CONSTRAINT `fk_batches_batch_transaction1`
FOREIGN KEY (`batch_transaction_transactionID`)
REFERENCES `books`.`batch_transaction` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`book_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`book_types` (
`book_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`books_bookID` INT(11) NOT NULL,
`books_book_prices_book_priceID` INT(11) NOT NULL,
`books_book_types_book_typeID` INT(11) NOT NULL,
`batches_batchID` INT(11) NOT NULL,
PRIMARY KEY (`book_typeID`),
INDEX `fk_product_types_products1_idx` (`books_bookID` ASC, `books_book_prices_book_priceID` ASC, `books_book_types_book_typeID` ASC),
INDEX `fk_product_types_batches1_idx` (`batches_batchID` ASC),
CONSTRAINT `fk_book_types_books1`
FOREIGN KEY (`books_bookID` , `books_book_prices_book_priceID` , `books_book_types_book_typeID`)
REFERENCES `books`.`books` (`bookID` , `book_prices_book_priceID` , `book_types_book_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_book_types_batches1`
FOREIGN KEY (`batches_batchID`)
REFERENCES `books`.`batches` (`batchID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`suppliers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`suppliers` (
`supplierID` INT(11) NOT NULL AUTO_INCREMENT,
`batches_batchID` INT(11) NOT NULL,
`name` VARCHAR(255) NULL,
PRIMARY KEY (`supplierID`),
INDEX `fk_suppliers_batches1_idx` (`batches_batchID` ASC),
CONSTRAINT `fk_suppliers_batches1`
FOREIGN KEY (`batches_batchID`)
REFERENCES `books`.`batches` (`batchID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`customer_transaction`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`customer_transaction` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`customerID` INT(11) NULL,
`transactions_transactionID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_client_transaction_transactions1_idx` (`transactions_transactionID` ASC),
CONSTRAINT `fk_customer_transaction_transactions1`
FOREIGN KEY (`transactions_transactionID`)
REFERENCES `books`.`transactions` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`customers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`customers` (
`customerID` INT(11) NOT NULL AUTO_INCREMENT,
`books_bookID` INT(11) NULL,
`books_book_prices_book_priceID` INT(11) NOT NULL,
`books_book_types_book_typeID` INT(11) NOT NULL,
`customer_transaction_transactionID` INT(11) NOT NULL,
PRIMARY KEY (`customerID`),
INDEX `fk_clients_products1_idx` (`books_bookID` ASC, `books_book_prices_book_priceID` ASC, `books_book_types_book_typeID` ASC),
INDEX `fk_clients_client_transaction1_idx` (`customer_transaction_transactionID` ASC),
CONSTRAINT `fk_customrs_products1`
FOREIGN KEY (`books_bookID` , `books_book_prices_book_priceID` , `books_book_types_book_typeID`)
REFERENCES `books`.`books` (`bookID` , `book_prices_book_priceID` , `book_types_book_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_customers_customer_transaction1`
FOREIGN KEY (`customer_transaction_transactionID`)
REFERENCES `books`.`customer_transaction` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`discounts`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`discounts` (
`discountID` INT(11) NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NOT NULL,
`transactions_transactionID` INT(11) NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NULL,
PRIMARY KEY (`discountID`),
INDEX `fk_discounts_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_discounts_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`discount_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`discount_types` (
`discount_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`Type` VARCHAR(255) NULL,
`discounts_discountID` INT(11) NOT NULL,
PRIMARY KEY (`discount_typeID`),
INDEX `fk_discount_types_discounts1_idx` (`discounts_discountID` ASC),
CONSTRAINT `fk_discount_types_discounts1`
FOREIGN KEY (`discounts_discountID`)
REFERENCES `books`.`discounts` (`discountID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`storagedistribution`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`storagedistribution` (
`storagedistributionID` INT(11) NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NULL,
`transactions_transactionID` INT(11) NOT NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NOT NULL,
PRIMARY KEY (`storagedistributionID`),
INDEX `fk_storagedistribution_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_storagedistribution_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
And now I got the same error in the table book_types:
ERROR: Error 1005: Can't create table `books`.`book_types` (errno: 150 "Foreign key constraint is incorrectly formed")
SQL Code:
-- -----------------------------------------------------
-- Table `books`.`book_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`book_types` (
`book_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`books_bookID` INT(11) NOT NULL,
`books_book_prices_book_priceID` INT(11) NOT NULL,
`books_book_types_book_typeID` INT(11) NOT NULL,
`batches_batchID` INT(11) NOT NULL,
PRIMARY KEY (`book_typeID`),
INDEX `fk_product_types_products1_idx` (`books_bookID` ASC, `books_book_prices_book_priceID` ASC, `books_book_types_book_typeID` ASC),
INDEX `fk_product_types_batches1_idx` (`batches_batchID` ASC),
CONSTRAINT `fk_book_types_books1`
FOREIGN KEY (`books_bookID` , `books_book_prices_book_priceID` , `books_book_types_book_typeID`)
REFERENCES `books`.`books` (`bookID` , `book_prices_book_priceID` , `book_types_book_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_book_types_batches1`
FOREIGN KEY (`batches_batchID`)
REFERENCES `books`.`batches` (`batchID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 11 succeeded, 1 failed
FOREIGN KEY (`transactions_transactionID`, `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID`, `transaction_types_transaction_typeID`)
Assuming you have good reasons to store transaction_typeID in the books table, though it's functionally dependend on transactionID. Any foreign key constraint needs an index in both tables (referencing/child and referenced/parent) which can support the constraint check.
If no supporting index is found in the child table (books), the engine will create one. That's why you have the following index:
INDEX `fk_books_transactions1_idx` (
`transactions_transactionID` ASC,
`transactions_transaction_types_transaction_typeID` ASC
)
But the engine will not create an index in the parent table (transactions). Indexes you already have are:
PRIMARY KEY (`transactionID`),
INDEX `fk_transactions_transaction_types1_idx` (`transaction_types_transaction_typeID` ASC),
Neither of them can support the FK constraint. So you need to define a new index in the transactions table:
INDEX (`transactionID`, `transaction_types_transaction_typeID`)
However - That is a denormalized design, which isn't something bad, if you have good reasons for it, since the consistency is enforced by the FK constraint. But if you know no good reasons for storing transaction_typeID in the books table, you should just remove it and change the FK constraint to
FOREIGN KEY (`transactions_transactionID`) REFERENCES `books`.`transactions` (`transactionID`)
Now it's referencing the primary key of the parent table. This is a basic design for foreign keys, and the constraint is supported by the PRIMARY KEY (index).
Problem solved by updating the relationships in the diagram.
The first diagram was denormalized, which isn't necessarily bad if there are justifications for it (the consistency was enforced by the FK constraint).
I ended up referencing only the PK of the "parent" table, being the _types table a parent one.
Final diagram:

Failed to add the foreign key constraint. Missing column 'gym_id' for constraint 'fk_gyms_instructors_gyms' in the referenced table 'gyms'

I'm forward engineering a database from an ERD that I created in MySqlWorkbench. I have a very basic many to many relationship between the "gyms" table and the "instructors" table, but I'm getting the error above. To me, the error reads that MySql can't find the "gym_id" column in the "gyms" table when it tries to create the many-to-many table.
I don't know why that would be the case.
I've went through the tables to make sure there were no typos and that the data types matched. I also went through the execution script to make sure the "gyms" table was being created before the many-to-many. It was. I can't sniff out what is causing the error. So any help or suggestion is appreciated
This is the error:
Executing SQL script in server
ERROR: Error 3734: Failed to add the foreign key constraint. Missing column 'gym_id' for constraint 'fk_gyms_instructors_gyms' in the referenced table 'gyms'
SQL Code:
-- -----------------------------------------------------
-- Table `fitness`.`gyms_instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms_instructors` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_gyms_instructors_gyms_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_gyms_instructors_instructors_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_gyms_instructors_gyms`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_gyms_instructors_instructors`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB
SQL script execution finished: statements: 10 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
The other potentially useful information is:
1. On the many to many table, in the foreign keys section both gym_id and instructor_id are SMALLINTS and NOT NULL,
2. On their respective table the gym_id and instructor_id are autoincrementing primary keys, NOT NULL
3. I pasted the full script below if you want to look.
-- MySQL Workbench Forward Engineering
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema fitness
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema fitness
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `fitness` DEFAULT CHARACTER SET utf8 ;
USE `fitness` ;
-- -----------------------------------------------------
-- Table `fitness`.`fitness_classes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`fitness_classes` (
`fitness_class_id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) NOT NULL,
`price` DECIMAL(2,2) NOT NULL,
`description` VARCHAR(500) NOT NULL,
`vacancies` INT NOT NULL,
`start_time` DATETIME NOT NULL,
PRIMARY KEY (`fitness_class_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`categories`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`categories` (
`category_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`description` TEXT(1200) NULL,
PRIMARY KEY (`category_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`tags`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`tags` (
`id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`category_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_tags_fitness_classes_idx` (`fitness_class_id` ASC) VISIBLE,
INDEX `fk_tags_categories_idx` (`category_id` ASC) VISIBLE,
CONSTRAINT `fk_tags_fitness_classes`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tags_categories`
FOREIGN KEY (`category_id`)
REFERENCES `fitness`.`categories` (`category_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`gyms`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms` (
`gym_id` SMALLINT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(55) NOT NULL,
`address` VARCHAR(55) NOT NULL,
`city` VARCHAR(25) NOT NULL,
`state` VARCHAR(25) NOT NULL,
`phone` VARCHAR(10) NOT NULL,
`latitude` DECIMAL(10,8) NULL,
`longitude` DECIMAL(10,8) NULL,
`neighborhood` VARCHAR(45) NOT NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`gym_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`instructors` (
`instructor_id` SMALLINT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(55) NOT NULL,
`last_name` VARCHAR(55) NOT NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`instructor_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`gyms_instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms_instructors` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_gyms_instructors_gyms_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_gyms_instructors_instructors_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_gyms_instructors_gyms`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_gyms_instructors_instructors`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`listings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`listings` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`fitness_class_id` INT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_listings_gym_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_listings_fitness_class_idx` (`fitness_class_id` ASC) VISIBLE,
INDEX `fk_listings_instructor_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_listings_gym`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_listings_fitness_class`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_listings_instructor`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`users` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(32) NOT NULL,
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`username` VARCHAR(16) NOT NULL,
PRIMARY KEY (`user_id`));
-- -----------------------------------------------------
-- Table `fitness`.`reviews`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`reviews` (
`review_id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`rating` TINYINT(1) NOT NULL,
`review_text` TEXT(1200) NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`review_id`),
INDEX `fk_reviews_classes_idx` (`fitness_class_id` ASC) VISIBLE,
CONSTRAINT `fk_reviews_classes`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`bookings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`bookings` (
`booking_id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`user_id` INT NOT NULL,
`price` DECIMAL(2,2) NOT NULL,
`class_start_time` DATETIME NOT NULL,
`purchase_date` DATETIME NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`booking_id`),
CONSTRAINT `user_id`
FOREIGN KEY ()
REFERENCES `fitness`.`users` ()
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fitness_class_id`
FOREIGN KEY ()
REFERENCES `fitness`.`fitness_classes` ()
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;

Error code 1215 with same datatype? [duplicate]

This question already has answers here:
MySQL Creating tables with Foreign Keys giving errno: 150
(20 answers)
Closed 4 years ago.
CREATE SCHEMA IF NOT EXISTS `cap` DEFAULT CHARACTER SET utf8 ;
USE `cap` ;
-- -----------------------------------------------------
-- Table `capstone`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`users` (
`username` VARCHAR(45) NOT NULL,
`password` VARCHAR(100) NOT NULL,
`roleid` INT NOT NULL,
`fullname` VARCHAR(45) NOT NULL,
`email` VARCHAR(45) NULL,
`phone` VARCHAR(45) NULL,
`department` VARCHAR(45) NULL,
PRIMARY KEY (`username`, `roleid`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`capstone`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`capstone` (
`username` VARCHAR(45) NOT NULL,
`capstoneid` INT NOT NULL,
`typeid` INT NOT NULL,
`title` VARCHAR(45) NULL,
`abstract` MEDIUMTEXT NULL,
`plagerismscore` VARCHAR(45) NULL,
`grade` VARCHAR(45) NULL,
PRIMARY KEY (`username`, `capstoneid`, `typeid`),
INDEX `fk_capstone_users_idx` (`username` ASC),
CONSTRAINT `fk_capstone_users`
FOREIGN KEY (`username`)
REFERENCES `cap`.`users` (`username`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`committee`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`committee` (
`capstoneid` INT NOT NULL,
`username` VARCHAR(45) NOT NULL,
`has_accepted` TINYINT NULL,
`has_declined` TINYINT NULL,
`positionid` INT NOT NULL,
`tracking` TINYINT NULL,
PRIMARY KEY (`capstoneid`, `username`, `positionid`),
CONSTRAINT `fk_committee_capstone`
FOREIGN KEY (`capstoneid`)
REFERENCES `cap`.`capstone` (`capstoneid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`studentdetails`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`studentdetails` (
`username` VARCHAR(45) NOT NULL,
`mastersstart` VARCHAR(45) NULL,
`capstonestart` VARCHAR(45) NULL,
PRIMARY KEY (`username`),
CONSTRAINT `fk_studentdetails_users`
FOREIGN KEY (`username`)
REFERENCES `cap`.`users` (`username`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`ritcalendar`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`ritcalendar` (
`term` INT NOT NULL,
`startdate` VARCHAR(45) NULL,
`adddropdeadline` VARCHAR(45) NULL,
`gradedeadline` VARCHAR(45) NULL,
`enddate` VARCHAR(45) NULL,
PRIMARY KEY (`term`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`statushistory`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`statushistory` (
`capstoneid` INT NOT NULL,
`statusid` INT NOT NULL,
`date` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`capstoneid`, `statusid`),
UNIQUE INDEX `capstoneid_UNIQUE` (`capstoneid` ASC),
CONSTRAINT `fk_statushistory_capstone`
FOREIGN KEY (`capstoneid`)
REFERENCES `cap`.`capstone` (`capstoneid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`status`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`status` (
`statusid` INT NOT NULL,
`name` VARCHAR(45) NULL,
`stepcode` VARCHAR(45) NULL,
`description` VARCHAR(255) NULL,
PRIMARY KEY (`statusid`),
CONSTRAINT `fk_status_statushistory`
FOREIGN KEY (`statusid`)
REFERENCES `cap`.`statushistory` (`statusid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`roles`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`roles` (
`roleid` INT NOT NULL,
`role` VARCHAR(10) NOT NULL,
PRIMARY KEY (`roleid`),
UNIQUE INDEX `role_UNIQUE` (`role` ASC),
CONSTRAINT `fk_roles_users`
FOREIGN KEY (`roleid`)
REFERENCES `cap`.`users` (`roleid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`types` (
`typeid` INT NOT NULL,
`type` VARCHAR(45) NOT NULL,
PRIMARY KEY (`typeid`),
CONSTRAINT `fk_types_capstone`
FOREIGN KEY (`typeid`)
REFERENCES `cap`.`capstone` (`typeid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`positions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`positions` (
`positionid` INT NOT NULL,
`position` VARCHAR(45) NULL,
PRIMARY KEY (`positionid`),
CONSTRAINT `fk_postions_committee`
FOREIGN KEY (`positionid`)
REFERENCES `cap`.`committee` (`positionid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `cap`.`committee` ( `capstoneid` INT NOT NULL, `username` VARCHAR(45) NOT NULL, `has_accepted` TINYINT NULL, `has_declined` TINYINT NULL, `positionid` INT NOT NULL, `tracking` TINYINT NULL, PRIMARY KEY (`capstoneid`, `username`, `positionid`), CONSTRAINT `fk_committee_capstone` FOREIGN KEY (`capstoneid`) REFERENCES `cap`.`capstone` (`capstoneid`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB Error Code: 1215. Cannot add foreign key constraint 0.031 sec
Can someone explain why I keep getting this error? They are the same datatype...
You should be more specific when you ask a question. You have eight foreign keys, you should at least say which one is causing an error.
Better would be to reduce your question to just the two tables involved in the error: capstone and committee
Here's what I see:
CREATE TABLE IF NOT EXISTS `cap`.`capstone` (
...
PRIMARY KEY (`username`, `capstoneid`, `typeid`),
CREATE TABLE IF NOT EXISTS `cap`.`committee` (
...
FOREIGN KEY (`capstoneid`)
REFERENCES `cap`.`capstone` (`capstoneid`)
You have a FK in committee that is referencing the middle column of the three-column primary key in capstone.
You should always make the FK have the same columns as the primary key it references.

MySQL Workbench Error 1005 cant create table

So I am very new to MySQL but I would like to think I that grasp a decent bit of it so far. I am struggling with the creation of the table PROJECT. I'm almost positive after my own research that it has something to do with my keys in the table. But I do not understand where the problem is or how to fix it.
The SQL queries below creates all tables for the project. I'm not sure of the problem because I only get the error code for the project for now. Any insight and advice on what causes this and how to rectify this in the code would be greatly appreciated.
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`employee` (
`ssn` VARCHAR(10) NOT NULL,
`fname` VARCHAR(45) NULL DEFAULT NULL,
`minit` VARCHAR(1) NULL DEFAULT NULL,
`lname` VARCHAR(45) NULL DEFAULT NULL,
`bdate` DATE NULL DEFAULT NULL,
`address` VARCHAR(45) NULL DEFAULT NULL,
`sex` VARCHAR(1) NULL DEFAULT NULL,
`salary` INT(11) NULL DEFAULT NULL,
`superssn` VARCHAR(10) NULL DEFAULT NULL,
`dno` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`ssn`),
INDEX `superssn_idx` (`superssn` ASC),
INDEX `dno_idx` (`dno` ASC),
CONSTRAINT `superssn`
FOREIGN KEY (`superssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `dno`
FOREIGN KEY (`dno`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`department`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`department` (
`dnumber` INT(11) NOT NULL,
`dname` VARCHAR(45) NULL DEFAULT NULL,
`mgrssn` VARCHAR(10) NULL DEFAULT NULL,
`mgrstartdate` DATE NULL DEFAULT NULL,
PRIMARY KEY (`dnumber`),
INDEX `mgrssn_idx` (`mgrssn` ASC),
CONSTRAINT `mgrssn`
FOREIGN KEY (`mgrssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`dept_locations`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`dept_locations` (
`dnumber` INT(11) NOT NULL,
`dlocation` VARCHAR(45) NOT NULL,
PRIMARY KEY (`dnumber`, `dlocation`),
CONSTRAINT `dnumber`
FOREIGN KEY (`dnumber`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`project`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`project` (
`pnumber` INT(11) NOT NULL,
`pname` VARCHAR(45) NULL,
`plocation` VARCHAR(45) NULL,
`dnum` INT(11) NOT NULL,
PRIMARY KEY (`pnumber`),
INDEX `dnum_idx` (`dnum` ASC),
INDEX `plocation_idx` (`plocation` ASC),
CONSTRAINT `dnum`
FOREIGN KEY (`dnum`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `plocation`
FOREIGN KEY (`plocation`)
REFERENCES `ebrasi1db`.`dept_locations` (`dlocation`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`works_on`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`works_on` (
`essn` VARCHAR(10) NOT NULL,
`pno` INT NOT NULL,
`hours` DECIMAL(5,2) NULL,
PRIMARY KEY (`essn`, `pno`),
INDEX `pno_idx` (`pno` ASC),
CONSTRAINT `works_on_essn`
FOREIGN KEY (`essn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `pno`
FOREIGN KEY (`pno`)
REFERENCES `ebrasi1db`.`project` (`pnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`dependent`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`dependent` (
`essn` VARCHAR(10) NOT NULL,
`dependent_name` VARCHAR(45) NOT NULL,
`sex` VARCHAR(1) NULL,
`bdate` DATE NULL,
`relation` VARCHAR(45) NULL,
PRIMARY KEY (`essn`, `dependent_name`),
CONSTRAINT `dependent_essn`
FOREIGN KEY (`essn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Your problem lies essentially in the fact that you are trying to create a constraint to a non key column (because the referenced table has a double key).
In your table project you have:
CONSTRAINT `plocation`
FOREIGN KEY (`plocation`)
REFERENCES `dept_locations` (`dlocation`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
dept_locations.dlocation is not a key ALONE (primary key), therefore the reason you are not able to create that constraint.
You need to make the constraint for both keys from the referenced table like this:
CONSTRAINT `plocation`
FOREIGN KEY (`pnumber`, `plocation`)
REFERENCES `dept_locations` (`dnumber`, `dlocation`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
In this case you won't need the first constraint dnum as it is already referenced in dept_locations table.
Also note that you tables department and employee will not be created because one reference the other so you need to create the tables first without the constraints then apply the constraints like:
ALTER TABLE `employee` add
CONSTRAINT `dno`
FOREIGN KEY (`dno`)
REFERENCES `department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Same for the department table and its constraint to employee
Normally you get the Error 1005 when it unable to create the foreign keys. When you are creating a foreign key constraint the parent table should be available in the database.
In your case 'employee' table has a FK to 'department' table but you are trying to create the 'employee' table before creating the 'department' table. Also the 'department' table has a FK back to 'employee' table. Therefore you are not able to create the 'department' table first. To solve this you may create the 'department' table first without FK to 'employee' table. Then create the 'employee' table and then alter the 'department' table with FK.
Here is the modified code for first two tables and follow the same pattern for rest if you get the same error.
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`department` (
`dnumber` INT(11) NOT NULL,
`dname` VARCHAR(45) NULL DEFAULT NULL,
`mgrssn` VARCHAR(10) NULL DEFAULT NULL,
`mgrstartdate` DATE NULL DEFAULT NULL,
PRIMARY KEY (`dnumber`),
INDEX `mgrssn_idx` (`mgrssn` ASC))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`employee` (
`ssn` VARCHAR(10) NOT NULL,
`fname` VARCHAR(45) NULL DEFAULT NULL,
`minit` VARCHAR(1) NULL DEFAULT NULL,
`lname` VARCHAR(45) NULL DEFAULT NULL,
`bdate` DATE NULL DEFAULT NULL,
`address` VARCHAR(45) NULL DEFAULT NULL,
`sex` VARCHAR(1) NULL DEFAULT NULL,
`salary` INT(11) NULL DEFAULT NULL,
`superssn` VARCHAR(10) NULL DEFAULT NULL,
`dno` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`ssn`),
INDEX `superssn_idx` (`superssn` ASC),
INDEX `dno_idx` (`dno` ASC),
CONSTRAINT `superssn`
FOREIGN KEY (`superssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `dno`
FOREIGN KEY (`dno`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
alter table department add
CONSTRAINT `mgrssn`
FOREIGN KEY (`mgrssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

Error "Cannot add foreign key constraint"

I am suffering the same "Cannot add foreign key constraint" that other folks around here.
The table client_partners contains a relationship between users. The error is raised at creating client_partners.
I have checked that users.id have the same type than client_partners.clientid and client_partners.partnerid: INT UNSIGNED NOT NULL.
The configuration options are:
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
Users table definition:
-- -----------------------------------------------------
-- Table `users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `users` ;
CREATE TABLE IF NOT EXISTS `users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`password` VARCHAR(200) NOT NULL,
`salt` VARCHAR(100) NULL,
`firstname` VARCHAR(50) NOT NULL,
`lastname` VARCHAR(50) NOT NULL,
`email` VARCHAR(50) NOT NULL,
`role` VARCHAR(10) NOT NULL DEFAULT 'supplier',
`destination` VARCHAR(10) NULL,
`supplierId` INT UNSIGNED NULL,
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE INDEX `username_UNIQUE` (`username` ASC),
INDEX `fk_users_suppliers_idx` (`supplierId` ASC),
INDEX `fk_users_roles_idx` (`role` ASC),
INDEX `fk_users_destinations1_idx` (`destination` ASC),
CONSTRAINT `fk_users_suppliers`
FOREIGN KEY (`supplierId`)
REFERENCES `suppliers` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_roles`
FOREIGN KEY (`role`)
REFERENCES `roles` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_destinations1`
FOREIGN KEY (`destination`)
REFERENCES `destinations` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
Client_partners table definition:
-- -----------------------------------------------------
-- Table `client_partners`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `client_partners` ;
CREATE TABLE IF NOT EXISTS `client_partners` (
`clientid` INT UNSIGNED NOT NULL,
`partnerid` INT UNSIGNED NOT NULL,
PRIMARY KEY (`clientid`, `partnerid`),
CONSTRAINT `fk_client_partners_1`
FOREIGN KEY (`clientid` , `partnerid`)
REFERENCES `users` (`id` , `id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
The datatypes (except autoincrement) are the same. The referencing table (users) is created first. What am I missing?
CONSTRAINT `fk_client_partners_1`
FOREIGN KEY (`clientid` , `partnerid`)
REFERENCES `users` (`id` , `id`)
here you are referencing to id of table users twice..it is not possible. try by removing one and create
If you want to reference to another foreign key you need to create it by using another name.