Error 121 can't create table - mysql

I am trying to import a database created in mySql workbench into phpMyAdmin (WAMP server) , when I try to import it I get error :
MySQL said: Documentation
#1005 - Can't create table '.\mydb\team.frm' (errno: 121)
Below is the executed SQL script
-- MySQL Script generated by MySQL Workbench
-- 03/03/15 13:06:58
-- Model: New Model Version: 1.0
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';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Team`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Team` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`TeamName` VARCHAR(45) NOT NULL,
`GoverningBody` VARCHAR(45) NOT NULL,
`Country` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Competitor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Competitor` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`FirstName` VARCHAR(45) NOT NULL,
`Surname` VARCHAR(45) NOT NULL,
`Age` INT NOT NULL,
`Nationality` VARCHAR(45) NOT NULL,
`Title` VARCHAR(45) NOT NULL,
`ContactNumber` INT NOT NULL,
`team_id` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `TeamID_idx` (`team_id` ASC),
CONSTRAINT `TeamID`
FOREIGN KEY (`team_id`)
REFERENCES `mydb`.`Team` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Card`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Card` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`IssueDate` DATE NOT NULL,
`ExpiryDate` DATE NOT NULL,
`Status` ENUM('Active','Expired','Lost') NOT NULL,
`competitor_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `CompetitorID_idx` (`competitor_id` ASC),
CONSTRAINT `CompetitorID`
FOREIGN KEY (`competitor_id`)
REFERENCES `mydb`.`Competitor` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Venue`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Venue` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`Stadium` VARCHAR(45) NOT NULL,
`Location` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Fixture`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Fixture` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`Time` TIME NOT NULL,
`Date` DATE NOT NULL,
`Type` VARCHAR(45) NOT NULL,
`venue_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `VenueID_idx` (`venue_id` ASC),
CONSTRAINT `VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `mydb`.`Venue` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Authorisation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Authorisation` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`fixture_id` INT NOT NULL,
`card_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `FixtureID_idx` (`fixture_id` ASC),
INDEX `CardID_idx` (`card_id` ASC),
CONSTRAINT `FixtureID`
FOREIGN KEY (`fixture_id`)
REFERENCES `mydb`.`Fixture` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CardID`
FOREIGN KEY (`card_id`)
REFERENCES `mydb`.`Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Log`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Log` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`card_id` INT NOT NULL,
`venue_id` INT NOT NULL,
`LogTime` TIME NOT NULL,
`LogDate` DATE NOT NULL,
PRIMARY KEY (`id`),
INDEX `CardID_idx` (`card_id` ASC),
INDEX `VenueID_idx` (`venue_id` ASC),
CONSTRAINT `CardID`
FOREIGN KEY (`card_id`)
REFERENCES `mydb`.`Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `mydb`.`Venue` (`id`)
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;

Making the changes in the comments above worked for me (the DATETIME to INT and making unique constraint and index names). As a safe practice, in our organization we always name our indexes/constraints as (table1Identifier)_(table2Identifier)_(constraintType)...so something like Log_Card_idx or Auth_Fixture_FK. Just a thought.
-- MySQL Script generated by MySQL Workbench
-- 03/03/15 13:06:58
-- Model: New Model Version: 1.0
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';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Team`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Team` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`TeamName` VARCHAR(45) NOT NULL,
`GoverningBody` VARCHAR(45) NOT NULL,
`Country` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Competitor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Competitor` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`FirstName` VARCHAR(45) NOT NULL,
`Surname` VARCHAR(45) NOT NULL,
`Age` INT NOT NULL,
`Nationality` VARCHAR(45) NOT NULL,
`Title` VARCHAR(45) NOT NULL,
`ContactNumber` INT NOT NULL,
`team_id` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `TeamID_idx` (`team_id` ASC),
CONSTRAINT `TeamID`
FOREIGN KEY (`team_id`)
REFERENCES `mydb`.`Team` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Card`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Card` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`IssueDate` DATE NOT NULL,
`ExpiryDate` DATE NOT NULL,
`Status` ENUM('Active','Expired','Lost') NOT NULL,
`competitor_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `CompetitorID_idx` (`competitor_id` ASC),
CONSTRAINT `CompetitorID`
FOREIGN KEY (`competitor_id`)
REFERENCES `mydb`.`Competitor` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Venue`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Venue` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`Stadium` VARCHAR(45) NOT NULL,
`Location` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Fixture`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Fixture` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`Time` TIME NOT NULL,
`Date` DATE NOT NULL,
`Type` VARCHAR(45) NOT NULL,
`venue_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `VenueID_idx` (`venue_id` ASC),
CONSTRAINT `VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `mydb`.`Venue` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Authorisation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Authorisation` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`fixture_id` INT NOT NULL,
`card_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `Auth_FixtureID_idx` (`fixture_id` ASC),
INDEX `Auth_CardID_idx` (`card_id` ASC),
CONSTRAINT `Auth_FixtureID`
FOREIGN KEY (`fixture_id`)
REFERENCES `Fixture` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Auth_CardID`
FOREIGN KEY (`card_id`)
REFERENCES `Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Log`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Log` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`card_id` INT NOT NULL,
`venue_id` INT NOT NULL,
`LogTime` TIME NOT NULL,
`LogDate` DATE NOT NULL,
PRIMARY KEY (`id`),
INDEX `Log_CardID_idx` (`card_id` ASC),
INDEX `Log_VenueID_idx` (`venue_id` ASC),
CONSTRAINT `Log_CardID`
FOREIGN KEY (`card_id`)
REFERENCES `mydb`.`Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Log_VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `Venue` (`id`)
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;

Related

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.

My SQL Duplicated key

I'm creating a database with mysql and I have a problem with duplicated key, I don't find the error but I know which is the table error, I think the problem is with the FK's in "historiaLaboral" table the SP is this:
Im using mysql workbench I would like to you advising me how improve the model
-- 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='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema SGD
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema SGD
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `SGD` DEFAULT CHARACTER SET utf8 ;
USE `SGD` ;
-- -----------------------------------------------------
-- Table `SGD`.`tipoIdentificación`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`tipoIdentificación` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`empresa`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`empresa` (
`id` INT NOT NULL,
`razonSocial` VARCHAR(45) NOT NULL,
`NIT` VARCHAR(45) NOT NULL,
`telefono` VARCHAR(45) NOT NULL,
`direccion` VARCHAR(45) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`empleado`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`empleado` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombres` VARCHAR(45) NOT NULL,
`apellidos` VARCHAR(45) NOT NULL,
`id_tipoDocumento` INT NOT NULL,
`numeroDocumento` INT(20) NULL,
`aspiracionSalarial` DOUBLE NOT NULL,
`telefono` INT NOT NULL,
`direccion` VARCHAR(30) NOT NULL,
`id_empresa` INT NOT NULL,
`genero` ENUM('F', 'M') NOT NULL,
`aspirante` BIT(1) NULL,
PRIMARY KEY (`id`),
INDEX `id_tipoDocumento_idx` (`id_tipoDocumento` ASC),
INDEX `id_empresa_idx` (`id_empresa` ASC),
CONSTRAINT `id_tipoDocumento`
FOREIGN KEY (`id_tipoDocumento`)
REFERENCES `SGD`.`tipoIdentificación` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_empresa`
FOREIGN KEY (`id_empresa`)
REFERENCES `SGD`.`empresa` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`rol`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`rol` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`usuario`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`usuario` (
`id` INT NOT NULL AUTO_INCREMENT,
`usuario` VARCHAR(45) NOT NULL,
`contrasena` VARCHAR(45) NOT NULL,
`id_rol` INT NOT NULL,
`id_empleado` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_empleado_idx` (`id_empleado` ASC),
INDEX `id_rol_idx` (`id_rol` ASC),
CONSTRAINT `id_empleado`
FOREIGN KEY (`id_empleado`)
REFERENCES `SGD`.`empleado` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_rol`
FOREIGN KEY (`id_rol`)
REFERENCES `SGD`.`rol` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`historiaLaboral`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`historiaLaboral` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_empleado` INT NOT NULL,
`id_usuarioRegistra` INT NOT NULL,
`fechaIngreso` DATE NOT NULL,
`fechaModificacion` DATE NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_empleado_idx` (`id_empleado` ASC),
INDEX `id_usuarioRegistra_idx` (`id_usuarioRegistra` ASC),
CONSTRAINT `id_empleado`
FOREIGN KEY (`id_empleado`)
REFERENCES `SGD`.`empleado` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_usuarioRegistra`
FOREIGN KEY (`id_usuarioRegistra`)
REFERENCES `SGD`.`usuario` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`referencia`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`referencia` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombres` VARCHAR(45) NOT NULL,
`apellidos` VARCHAR(45) NOT NULL,
`relacion` VARCHAR(45) NOT NULL,
`telefono` VARCHAR(45) NOT NULL,
`telefono_2` VARCHAR(45) NULL,
`id_HLaboral` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_Empleado_idx` (`id_HLaboral` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`experienciaLaboral`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`experienciaLaboral` (
`id` INT NOT NULL AUTO_INCREMENT,
`empresa` VARCHAR(45) NOT NULL,
`fechaInicio` DATE NOT NULL,
`fechaFin` DATE NOT NULL,
`motivoRetiro` VARCHAR(45) NOT NULL,
`id_HLaboral` INT NOT NULL,
`certificado` BIT(1) NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_Empleado_idx` (`id_HLaboral` ASC),
CONSTRAINT `id_Empleado`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`estudios`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`estudios` (
`id` INT NOT NULL AUTO_INCREMENT,
`institución` VARCHAR(45) NOT NULL,
`fechaInicio` DATE NOT NULL,
`fechaFin` DATE NOT NULL,
`certificado` BIT(1) NOT NULL DEFAULT 0,
`id_HLaboral` INT NOT NULL,
`id_tipoEstudio` INT NULL,
PRIMARY KEY (`id`),
INDEX `id_HLaboral_idx` (`id_tipoEstudio` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_tipoEstudio`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`serie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`serie` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`soporte`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`soporte` (
`id` INT NOT NULL,
`papel` BIT(1) NOT NULL,
`digital` BIT(1) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`retencion`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`retencion` (
`id` INT NOT NULL AUTO_INCREMENT,
`archivoGestion` INT NOT NULL,
`archivoCentral` INT NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`dispFinal`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`dispFinal` (
`id` INT NOT NULL AUTO_INCREMENT,
`conservacionTotal` BIT(1) NULL,
`eliminacion` BIT(1) NULL,
`digitalizacion` BIT(1) NULL,
`seleccion` BIT(1) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`TRD`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`TRD` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_serie` INT NOT NULL,
`detalle` VARCHAR(2000) NOT NULL,
`id_soporte` INT NOT NULL,
`id_retencion` INT NOT NULL,
`id_dispFinal` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_serie_idx` (`id_serie` ASC),
INDEX `id_soporte_idx` (`id_soporte` ASC),
INDEX `id_retencion_idx` (`id_retencion` ASC),
INDEX `id_dispFinal_idx` (`id_dispFinal` ASC),
CONSTRAINT `id_serie`
FOREIGN KEY (`id_serie`)
REFERENCES `SGD`.`serie` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_soporte`
FOREIGN KEY (`id_soporte`)
REFERENCES `SGD`.`soporte` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_retencion`
FOREIGN KEY (`id_retencion`)
REFERENCES `SGD`.`retencion` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_dispFinal`
FOREIGN KEY (`id_dispFinal`)
REFERENCES `SGD`.`dispFinal` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`documento`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`documento` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_HLaboral` INT NOT NULL,
`id_TRD` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_HLaboral_idx` (`id_HLaboral` ASC),
INDEX `id_TRD_idx` (`id_TRD` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_TRD`
FOREIGN KEY (`id_TRD`)
REFERENCES `SGD`.`TRD` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`ubicacion`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`ubicacion` (
`id` INT NOT NULL AUTO_INCREMENT,
`analquel` VARCHAR(45) NOT NULL,
`estante` VARCHAR(45) NOT NULL,
`id_documento` INT NULL,
PRIMARY KEY (`id`),
INDEX `id_documento_idx` (`id_documento` ASC),
CONSTRAINT `id_documento`
FOREIGN KEY (`id_documento`)
REFERENCES `SGD`.`documento` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`subserie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`subserie` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_serie` INT NOT NULL,
`nombre` VARCHAR(45) NOT NULL,
`id_subSerie` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_serie_idx` (`id_subSerie` ASC),
CONSTRAINT `id_subSerie`
FOREIGN KEY (`id_subSerie`)
REFERENCES `SGD`.`serie` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`permisos`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`permisos` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
`id_rol` INT NOT NULL,
`acceso` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_rol_idx` (`id_rol` ASC),
CONSTRAINT `id_rol`
FOREIGN KEY (`id_rol`)
REFERENCES `SGD`.`rol` (`id`)
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;
I'm based on this model
Thanks for helping me!

ERROR: Error 1005: Can't create table 'airbnb.profile' (errno: 150)

I created a few tables with foreign keys in a workbench model that I wanted to forward engineer to a server connection although I am recieving this error;
ERROR: Error 1005: Can't create table 'airbnb.profile' (errno: 150)
I have had a look around on how to resolve this error but nothing has seemed to have worked. I tried ensuring all the default character sets were the same, that did not work.
I tried adding
SET FOREIGN_KEY_CHECKS=0;
This also did not work. I am new to mysql and database design in general so I am in a bit of a loss as to what exactly is wrong with my script.
Below is the SQL script, any help to resolve this error would be greatly appreciated.
-- 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='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema airbnb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema airbnb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `airbnb` DEFAULT CHARACTER SET latin1 ;
USE `airbnb` ;
-- -----------------------------------------------------
-- Table `airbnb`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`users` (
`user_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(50) CHARACTER SET 'utf8' NOT NULL,
`last_name` VARCHAR(50) CHARACTER SET 'utf8' NOT NULL,
`dob` DATETIME NOT NULL,
`email` VARCHAR(320) NOT NULL,
`newsletter` BIT(1) NOT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NULL DEFAULT NULL,
`credit` DECIMAL(10,2) NULL DEFAULT NULL,
PRIMARY KEY (`user_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
-- -----------------------------------------------------
-- Table `airbnb`.`language`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`language` (
`language_id` INT NOT NULL AUTO_INCREMENT,
`name` NVARCHAR(50) NOT NULL,
PRIMARY KEY (`language_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `airbnb`.`profile`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`profile` (
`profile_id` INT(11) NOT NULL,
`user_id` INT(10) NOT NULL,
`gender` ENUM('male', 'female', 'other') NULL DEFAULT NULL,
`phone_number` VARCHAR(45) NULL DEFAULT NULL,
`bio` VARCHAR(1000) CHARACTER SET 'utf8' NULL DEFAULT NULL,
`location` VARCHAR(300) CHARACTER SET 'utf8' NULL DEFAULT NULL,
`site_currency` VARCHAR(3) CHARACTER SET 'utf8' NOT NULL,
`language_id` INT NOT NULL,
PRIMARY KEY (`profile_id`),
UNIQUE INDEX `profile_id_UNIQUE` (`profile_id` ASC),
INDEX `fk_profile_users1_idx` (`user_id` ASC),
INDEX `fk_profile_language1_idx` (`language_id` ASC),
CONSTRAINT `fk_profile_users1`
FOREIGN KEY (`user_id`)
REFERENCES `airbnb`.`users` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_profile_language1`
FOREIGN KEY (`language_id`)
REFERENCES `airbnb`.`language` (`language_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
-- -----------------------------------------------------
-- Table `airbnb`.`profile_media`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`profile_media` (
`profile_media_id` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` INT(10) NOT NULL,
`photo` VARCHAR(100) NULL DEFAULT NULL,
`symbol` VARCHAR(100) NULL DEFAULT NULL,
`video` VARCHAR(100) NULL DEFAULT NULL,
PRIMARY KEY (`profile_media_id`),
INDEX `fk_profile_media_users1_idx` (`user_id` ASC),
CONSTRAINT `fk_profile_media_users1`
FOREIGN KEY (`user_id`)
REFERENCES `airbnb`.`users` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
-- -----------------------------------------------------
-- Table `airbnb`.`em_contact`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`em_contact` (
`em_contact_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NULL,
`phone` VARCHAR(45) NULL,
`email` NVARCHAR(320) NULL,
`relationship` VARCHAR(100) NULL,
PRIMARY KEY (`em_contact_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `airbnb`.`timezone`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`timezone` (
`timezone_id` INT NOT NULL AUTO_INCREMENT,
`name` NVARCHAR(50) NOT NULL,
PRIMARY KEY (`timezone_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `airbnb`.`shipping_address`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`shipping_address` (
`ship_id` INT NOT NULL AUTO_INCREMENT,
`add_1` NVARCHAR(50) NOT NULL,
`add_2` NVARCHAR(50) NULL,
`city` NVARCHAR(50) NOT NULL,
`state` NVARCHAR(50) NOT NULL,
`zip_code` NVARCHAR(20) NOT NULL,
PRIMARY KEY (`ship_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `airbnb`.`profile_opt`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`profile_opt` (
`prof_opt_id` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` INT(10) NOT NULL,
`school` VARCHAR(150) CHARACTER SET 'utf8' NULL,
`work` VARCHAR(100) CHARACTER SET 'utf8' NULL DEFAULT NULL,
`work_email_add` VARCHAR(320) NULL DEFAULT NULL,
`country_of_residence_id` INT(11) NULL,
`em_contact_id` INT NULL,
`timezone_id` INT NOT NULL,
`ship_id` INT NULL,
PRIMARY KEY (`prof_opt_id`),
UNIQUE INDEX `profile_optional_id_UNIQUE` (`prof_opt_id` ASC),
INDEX `fk_profile_optional_users1_idx` (`user_id` ASC),
INDEX `fk_profile_opt_em_contact1_idx` (`em_contact_id` ASC),
INDEX `fk_profile_opt_timezone1_idx` (`timezone_id` ASC),
INDEX `fk_profile_opt_shipping_address1_idx` (`ship_id` ASC),
CONSTRAINT `fk_profile_optional_users1`
FOREIGN KEY (`user_id`)
REFERENCES `airbnb`.`users` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_profile_opt_em_contact1`
FOREIGN KEY (`em_contact_id`)
REFERENCES `airbnb`.`em_contact` (`em_contact_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_profile_opt_timezone1`
FOREIGN KEY (`timezone_id`)
REFERENCES `airbnb`.`timezone` (`timezone_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_profile_opt_shipping_address1`
FOREIGN KEY (`ship_id`)
REFERENCES `airbnb`.`shipping_address` (`ship_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
-- -----------------------------------------------------
-- Table `airbnb`.`secure_login`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`secure_login` (
`secure_id` INT(11) NOT NULL AUTO_INCREMENT,
`encrypted_password` BINARY(32) NOT NULL,
`password_salt` BINARY(64) NOT NULL,
`login_count` INT(11) NOT NULL,
`last_login_request` DATETIME NULL DEFAULT NULL,
`last_login_at` DATETIME NULL DEFAULT NULL,
`last_login_ip` VARBINARY(16) NULL DEFAULT NULL,
`browser_id` INT(11) NULL DEFAULT NULL,
`device_id` INT(11) NULL DEFAULT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`secure_id`),
UNIQUE INDEX `id_UNIQUE` (`secure_id` ASC),
INDEX `fk_secure_login_users_idx` (`user_id` ASC),
CONSTRAINT `fk_secure_login_users`
FOREIGN KEY (`user_id`)
REFERENCES `airbnb`.`users` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
-- -----------------------------------------------------
-- Table `airbnb`.`hear_about_options`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`hear_about_options` (
`selection_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`selection_name` VARCHAR(100) NOT NULL,
PRIMARY KEY (`selection_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `airbnb`.`hear_about`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`hear_about` (
`hear_about_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(10) UNSIGNED NOT NULL,
`selection_id` INT NOT NULL,
PRIMARY KEY (`hear_about_id`),
INDEX `fk_hear_about_users1_idx` (`user_id` ASC),
INDEX `fk_hear_about_hear_about_options1_idx` (`selection_id` ASC),
CONSTRAINT `fk_hear_about_users1`
FOREIGN KEY (`user_id`)
REFERENCES `airbnb`.`users` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_hear_about_hear_about_options1`
FOREIGN KEY (`selection_id`)
REFERENCES `airbnb`.`hear_about_options` (`selection_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `airbnb`.`profile_language`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `airbnb`.`profile_language` (
`p_vs_l_id` INT NOT NULL AUTO_INCREMENT,
`prof_opt_id` INT(11) NOT NULL,
`language_id` INT NOT NULL,
PRIMARY KEY (`p_vs_l_id`),
INDEX `fk_profile_vs_language_profile_opt1_idx` (`prof_opt_id` ASC),
INDEX `fk_profile_language_language1_idx` (`language_id` ASC),
CONSTRAINT `fk_profile_vs_language_profile_opt1`
FOREIGN KEY (`prof_opt_id`)
REFERENCES `airbnb`.`profile_opt` (`prof_opt_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_profile_language_language1`
FOREIGN KEY (`language_id`)
REFERENCES `airbnb`.`language` (`language_id`)
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;

Cannot forward engineer this database from MySQL Workbench

Using MySQL Workbench I'm trying to forward engineer a schema, but I keep getting the message:
ERROR: Error 1022: Can't write; duplicate key in table 'tbl_banner_ad'
I've looked at similar problems but all the solutions say delete this or delete that but I don't want to delete something that then impacts how my database works.
From my SQL below can anyone tell me what I need to do that will get rid of this error but won't get rid of any referential integrity or anything like that:
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';
-- -----------------------------------------------------
-- Schema ibn_db
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `ibn_db` ;
CREATE SCHEMA IF NOT EXISTS `ibn_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `ibn_db` ;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_users` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_users` (
`_id` INT UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`name` VARCHAR(80) NOT NULL,
`email` VARCHAR(255) NOT NULL,
`address_building` VARCHAR(80) NULL,
`address_street` VARCHAR(80) NOT NULL,
`address_city` VARCHAR(80) NOT NULL,
`address_county` VARCHAR(80) NULL,
`address_postcode` VARCHAR(13) NOT NULL,
`address_country` VARCHAR(80) NULL,
`is_admin` TINYINT(1) NOT NULL DEFAULT 0,
`phone` VARCHAR(15) NOT NULL,
`company` VARCHAR(80) NULL,
`banned` TINYINT(1) NOT NULL DEFAULT 0,
`ban_reason` VARCHAR(160) NULL,
`ban_expiry_date` DATETIME NULL,
`password` VARCHAR(128) NOT NULL,
`salt` VARCHAR(128) NOT NULL,
`joined_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_modified_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`_id`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_unauthorized_users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_unauthorized_users` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_unauthorized_users` (
`_id` INT UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(128) NOT NULL,
`salt` VARCHAR(128) NOT NULL,
`activation_hash` VARCHAR(128) NOT NULL,
`created_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`_id`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC),
UNIQUE INDEX `activation_hash_UNIQUE` (`activation_hash` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_events`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_events` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_events` (
`_id` INT ZEROFILL UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(80) NOT NULL,
`description` MEDIUMTEXT NULL,
`start_datetime` DATETIME NOT NULL,
`end_datetime` DATETIME NOT NULL,
`created_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_modified_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`address_building` VARCHAR(80) NULL,
`address_street` VARCHAR(80) NOT NULL,
`address_city` VARCHAR(80) NOT NULL,
`address_county` VARCHAR(80) NULL,
`address_postcode` VARCHAR(13) NOT NULL,
`address_country` VARCHAR(80) NULL,
`image_url` VARCHAR(255) NULL,
PRIMARY KEY (`_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_payments`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_payments` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_payments` (
`_id` INT ZEROFILL UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_attendee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_attendee` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_attendee` (
`_id` INT ZEROFILL UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`event_id` INT UNSIGNED NOT NULL,
`payment_id` INT UNSIGNED NOT NULL,
`created_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`_id`),
INDEX `user_id_idx` (`user_id` ASC),
INDEX `event_id_idx` (`event_id` ASC),
INDEX `payment_id_idx` (`payment_id` ASC),
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `event_id`
FOREIGN KEY (`event_id`)
REFERENCES `ibn_db`.`tbl_events` (`_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `payment_id`
FOREIGN KEY (`payment_id`)
REFERENCES `ibn_db`.`tbl_payments` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_banner_ad`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_banner_ad` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_banner_ad` (
`_id` INT UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`payment_id` INT UNSIGNED NOT NULL,
`url` VARCHAR(255) NOT NULL,
`width` INT UNSIGNED NOT NULL,
`height` INT UNSIGNED NOT NULL,
`created_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`_id`),
INDEX `user_id_idx` (`user_id` ASC),
INDEX `payment_id_idx` (`payment_id` ASC),
CONSTRAINT `payment_id`
FOREIGN KEY (`payment_id`)
REFERENCES `ibn_db`.`tbl_payments` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_reviews`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_reviews` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_reviews` (
`_id` INT UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`event_id` INT UNSIGNED NOT NULL,
`comment` VARCHAR(255) NULL,
`stars` INT NULL,
PRIMARY KEY (`_id`),
INDEX `user_id_idx` (`user_id` ASC),
INDEX `event_id_idx` (`event_id` ASC),
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `event_id`
FOREIGN KEY (`event_id`)
REFERENCES `ibn_db`.`tbl_events` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_login_attempts`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_login_attempts` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_login_attempts` (
`_id` INT UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ip` VARCHAR(45) NOT NULL,
`success` TINYINT(1) NOT NULL,
`fail_reason` VARCHAR(160) NULL,
PRIMARY KEY (`_id`),
INDEX `user_id_idx` (`user_id` ASC),
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE CASCADE
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;
EDIT: I've tried deleting the INDEX statements from tbl_banner_ads but I can't delete it because the index is part of the foreign key.
Try and change the CREATE for tbl_banner_ad. Change this part:
CONSTRAINT `payment_id`
FOREIGN KEY (`payment_id`)
REFERENCES `ibn_db`.`tbl_payments` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
to:
CONSTRAINT `payment_id1`
FOREIGN KEY (`payment_id`)
REFERENCES `ibn_db`.`tbl_payments` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `user_id1`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
(Alternatively, you could name the constraints to reflect both the parent and child tablenames as well as the columnn-name. I.e. tbl_payment_tbl_banner_ad_payment_id.