MySql and creating Foreign keys - mysql

-- MySQL Script generated by MySQL Workbench
-- Sun Apr 2 17:50:54 2017
-- Model: New Model Version: 1.0
-- 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 SintalDB
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema SintalDB
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `SintalDB` DEFAULT CHARACTER SET utf8 COLLATE utf8_slovenian_ci ;
USE `SintalDB` ;
-- -----------------------------------------------------
-- Table `SintalDB`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`User` (
`PK_User` INT NOT NULL,
`Name` VARCHAR(15) NOT NULL,
`Surname` VARCHAR(25) NOT NULL,
`Phone_Nr` VARCHAR(13) NOT NULL,
`Email` VARCHAR(90) NOT NULL,
`Car_Reg_Nr` VARCHAR(10) NULL,
`Username` VARCHAR(20) NOT NULL,
`Password` VARCHAR(20) NOT NULL,
`Servis_Input_Rights` TINYINT(1) NOT NULL,
`Servis_Output_Rights` TINYINT(1) NOT NULL,
`Servis_Delete_Rights` TINYINT(1) NOT NULL,
`Admin_Rights` TINYINT(1) NOT NULL,
PRIMARY KEY (`PK_User`))
ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
-- -----------------------------------------------------
-- Table `SintalDB`.`City`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`City` (
`PK_City` INT NOT NULL,
`Name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`PK_City`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
-- -----------------------------------------------------
-- Table `SintalDB`.`Company`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Company` (
`PK_Company` INT NOT NULL,
`Name` VARCHAR(25) NOT NULL,
`Address` VARCHAR(45) NOT NULL,
`Address_Nr` VARCHAR(4) NOT NULL,
`FK_City` INT NOT NULL,
PRIMARY KEY (`PK_Company`),
INDEX `FK_City_Company_fkn_idx` (`FK_City` ASC),
CONSTRAINT `FK_City_Company_fkn`
FOREIGN KEY (`FK_City`)
REFERENCES `SintalDB`.`City` (`PK_City`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
-- -----------------------------------------------------
-- Table `SintalDB`.`Facility`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Facility` (
`PK_Facility_AD` VARCHAR(6) NOT NULL,
`Name` VARCHAR(60) NOT NULL,
`Address` VARCHAR(45) NOT NULL,
`Address_Nr` VARCHAR(4) NOT NULL,
`FK_City` INT NOT NULL,
`FK_Company_Affiliation` INT NOT NULL,
PRIMARY KEY (`PK_Facility_AD`),
INDEX `FK_City_Facility_fkn_idx` (`FK_City` ASC),
INDEX `FK_Company_Affiliation_Facility_fkn_idx` (`FK_Company_Affiliation` ASC),
CONSTRAINT `FK_City_Facility_fkn`
FOREIGN KEY (`FK_City`)
REFERENCES `SintalDB`.`City` (`PK_City`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_Company_Affiliation_Facility_fkn`
FOREIGN KEY (`FK_Company_Affiliation`)
REFERENCES `SintalDB`.`Company` (`PK_Company`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
-- -----------------------------------------------------
-- Table `SintalDB`.`Servis`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Servis` (
`PK_Servis_Nr` VARCHAR(11) NOT NULL,
`FK_Facility_AD` VARCHAR(6) NOT NULL,
`Description` VARCHAR(255) NOT NULL,
`Date_Recived` DATETIME NOT NULL,
`FK_User_Commited` INT NULL,
`Date_Commited` DATETIME NULL,
`FK_User_Done` INT NULL,
`Date_Done` DATETIME NULL,
PRIMARY KEY (`PK_Servis_Nr`),
INDEX `FK_User_Servis_fkn_idx` (`FK_User_Commited` ASC, `FK_User_Done` ASC),
INDEX `FK_Facility_Servis_fkn_idx` (`FK_Facility_AD` ASC),
CONSTRAINT `FK_User_Servis_fkn`
FOREIGN KEY (`FK_User_Commited` , `FK_User_Done`)
REFERENCES `SintalDB`.`User` (`PK_User` , `PK_User`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_Facility_Servis_fkn`
FOREIGN KEY (`FK_Facility_AD`)
REFERENCES `SintalDB`.`Facility` (`PK_Facility_AD`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
-- -----------------------------------------------------
-- Table `SintalDB`.`ServisNote`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`ServisNote` (
`PK_ServisNote` INT NOT NULL,
`Note` VARCHAR(255) NOT NULL,
`FK_User_Note` INT NOT NULL,
`Date_Created` DATETIME NOT NULL,
`FK_Servis_Nr` VARCHAR(11) NOT NULL,
PRIMARY KEY (`PK_ServisNote`),
INDEX `FK_Servis_ServisNote_fkn_idx` (`FK_Servis_Nr` ASC),
INDEX `FK_User_ServisNote_fkn_idx` (`FK_User_Note` ASC),
CONSTRAINT `FK_Servis_ServisNote_fkn`
FOREIGN KEY (`FK_Servis_Nr`)
REFERENCES `SintalDB`.`Servis` (`PK_Servis_Nr`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_User_ServisNote_fkn`
FOREIGN KEY (`FK_User_Note`)
REFERENCES `SintalDB`.`User` (`PK_User`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
this is my sql file and I get error after i run sql file
Error Code: 1215. Cannot add foreign key constraint
17:52:08 CREATE TABLE IF NOT EXISTS SintalDB.Servis (
PK_Servis_Nr VARCHAR(11) NOT NULL, FK_Facility_AD VARCHAR(6) NOT
NULL, Description VARCHAR(255) NOT NULL, Date_Recived DATETIME
NOT NULL, FK_User_Commited INT NULL, Date_Commited DATETIME
NULL, FK_User_Done INT NULL, Date_Done DATETIME NULL,
PRIMARY KEY (PK_Servis_Nr), INDEX FK_User_Servis_fkn_idx
(FK_User_Commited ASC, FK_User_Done ASC), INDEX
FK_Facility_Servis_fkn_idx (FK_Facility_AD ASC), CONSTRAINT
FK_User_Servis_fkn FOREIGN KEY (FK_User_Commited ,
FK_User_Done) REFERENCES SintalDB.User (PK_User ,
PK_User) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT FK_Facility_Servis_fkn FOREIGN KEY (FK_Facility_AD)
REFERENCES SintalDB.Facility (PK_Facility_AD) ON DELETE NO
ACTION ON UPDATE NO ACTION) ENGINE = InnoDB DEFAULT CHARACTER SET
= utf8 COLLATE = utf8_slovenian_ci Error Code: 1215. Cannot add foreign key constraint 0.063 sec

Check this: Adding foreign key on multiple columns
I think you must add two foreign key for made reference to the same column.
You can replace your code for this and test.
-- -----------------------------------------------------
-- Table `SintalDB`.`Servis`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Servis` (
`PK_Servis_Nr` VARCHAR(11) NOT NULL,
`FK_Facility_AD` VARCHAR(6) NOT NULL,
`Description` VARCHAR(255) NOT NULL,
`Date_Recived` DATETIME NOT NULL,
`FK_User_Commited` INT NULL,
`Date_Commited` DATETIME NULL,
`FK_User_Done` INT NULL,
`Date_Done` DATETIME NULL,
PRIMARY KEY (`PK_Servis_Nr`),
INDEX `FK_User_Servis_fkn_idx` (`FK_User_Commited` ASC, `FK_User_Done` ASC),
INDEX `FK_Facility_Servis_fkn_idx` (`FK_Facility_AD` ASC),
CONSTRAINT `FK_User_Servis_fkn`
FOREIGN KEY (`FK_User_Commited`)
REFERENCES `SintalDB`.`User` (`PK_User` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_User_Servis_2_fkn`
FOREIGN KEY (`FK_User_Done`)
REFERENCES `SintalDB`.`User` (`PK_User`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_Facility_Servis_fkn`
FOREIGN KEY (`FK_Facility_AD`)
REFERENCES `SintalDB`.`Facility` (`PK_Facility_AD`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;

Related

MYSQL Cant not create table errno: 150 Foreign key constraint is incorrectly formed

A problem occurred in the foreign key while creating a table with mysql workbench.
When i create table with mysql workbench,
workbench give me this error :
Executing SQL script in server
ERROR: Error 1005: Can't create table kipit.unlike_game_library (errno: 150 "Foreign key constraint is incorrectly formed")
Executing SQL script in server
ERROR: Error 1005: Can't create table `kipit`.`unlike_game_library` (errno: 150 "Foreign key constraint is incorrectly formed")
SQL Code:
CREATE TABLE IF NOT EXISTS `kipit`.`unlike_game_library`
(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
`user_name` VARCHAR(100) NOT NULL,
`game_slug` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `user_name_UNIQUE` (`user_name` ASC),
CONSTRAINT `fk_rated_game_library_user`
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci
SQL script execution finished: statements: 11 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Here is my query:
-- 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 mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema kipit
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `kipit`;
-- -----------------------------------------------------
-- Schema kipit
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `kipit` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `kipit`;
-- -----------------------------------------------------
-- Table `kipit`.`user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `kipit`.`user`;
CREATE TABLE IF NOT EXISTS `kipit`.`user`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`role` VARCHAR(45) NOT NULL,
`name` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`picture` VARCHAR(100) NOT NULL,
`message` VARCHAR(255) NULL DEFAULT NULL,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
PRIMARY KEY (`id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC)
)
ENGINE = InnoDB
AUTO_INCREMENT = 34
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `kipit`.`game`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `kipit`.`game`;
CREATE TABLE IF NOT EXISTS `kipit`.`game`
(
`id` BIGINT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`slug` VARCHAR(255) NOT NULL,
`image` VARCHAR(255) NOT NULL,
`genre` VARCHAR(100) NULL,
`publisher` VARCHAR(255) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `slug_UNIQUE` (`slug` ASC),
UNIQUE INDEX `name_UNIQUE` (`name` ASC),
UNIQUE INDEX `id_UNIQUE` (`id` ASC)
)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `kipit`.`unlike_game_library`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `kipit`.`unlike_game_library`;
CREATE TABLE IF NOT EXISTS `kipit`.`unlike_game_library`
(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
`user_name` VARCHAR(100) NOT NULL,
`game_slug` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `user_name_UNIQUE` (`user_name` ASC),
UNIQUE INDEX `game_slug_UNIQUE` (`game_slug` ASC),
CONSTRAINT `fk_rated_game_library_user`
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
SET SQL_MODE = #OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS = #OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS = #OLD_UNIQUE_CHECKS;
i check same type and unicode setting and unique key, but it's not work
here is my db fiddle result (full query) :
https://www.db-fiddle.com/f/t2x2zvcVZZqxjsBsM7UXMF/3
+ INNO STATUS
2020-04-28 11:43:42 0x1d68 Error in foreign key constraint of table `kipit`.`if`:
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Please refer to https://mariadb.com/kb/en/library/foreign-keys/ for correct foreign key definition.
Create table `kipit`.`if` with foreign key constraint failed. Field type or character set for column 'user_name' does not mach referenced column 'name' near '
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci'.
Seems like 'DEFAULT CHAR SET' and 'COLLATION' doesn't match. How about making it same for all table being used.
DROP TABLE IF EXISTS `kipit`.`user`;
CREATE TABLE IF NOT EXISTS `kipit`.`user`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`role` VARCHAR(45) NOT NULL,
`name` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`picture` VARCHAR(100) NOT NULL,
`message` VARCHAR(255) NULL DEFAULT NULL,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
PRIMARY KEY (`id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC)
)
ENGINE = InnoDB
AUTO_INCREMENT = 34
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
DROP TABLE IF EXISTS `kipit`.`game`;
CREATE TABLE IF NOT EXISTS `kipit`.`game`
(
`id` BIGINT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`slug` VARCHAR(255) NOT NULL,
`image` VARCHAR(255) NOT NULL,
`genre` VARCHAR(100) NULL,
`publisher` VARCHAR(255) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `slug_UNIQUE` (`slug` ASC) ,
UNIQUE INDEX `name_UNIQUE` (`name` ASC) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
DROP TABLE IF EXISTS `kipit`.`unlike_game_library`;
CREATE TABLE IF NOT EXISTS `kipit`.`unlike_game_library`
(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
`user_name` VARCHAR(100) NOT NULL,
`game_slug` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `user_name_UNIQUE` (`user_name` ASC) ,
UNIQUE INDEX `game_slug_UNIQUE` (`game_slug` ASC) ,
CONSTRAINT `fk_rated_game_library_user`
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;

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;

Error 121 can't create table

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;

#1215 - Cannot add foreign key constraint

I have this SQL query:
-- MySQL Script generated by MySQL Workbench
-- 11/26/14 13:26:51
-- Model: Hashtigator database layout Version: 1.0
-- 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 local_sysDB
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `local_sysDB` ;
-- -----------------------------------------------------
-- Schema local_sysDB
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `local_sysDB` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
SHOW WARNINGS;
USE `local_sysDB` ;
-- -----------------------------------------------------
-- Table `local_sysDB`.`accounts`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `local_sysDB`.`accounts` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `local_sysDB`.`accounts` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`password` VARCHAR(255) NULL,
`email` VARCHAR(255) NULL COMMENT 'email must be unique',
`role` TINYINT(1) NULL DEFAULT 2 COMMENT '1 = admin, 2 = user\nDefault role is 2 (user)',
`active` TINYINT(1) NULL DEFAULT 0 COMMENT 'accounts should manually be activated',
`created` TIMESTAMP NULL DEFAULT NOW(),
`appId` VARCHAR(255) NULL DEFAULT NULL,
`appSecret` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `accounts_email` (`email` ASC),
UNIQUE INDEX `accounts_appKeys` (`appId` ASC, `appSecret` ASC))
ENGINE = InnoDB
AUTO_INCREMENT = 1
ROW_FORMAT = COMPRESSED;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `local_sysDB`.`hashtags`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `local_sysDB`.`hashtags` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `local_sysDB`.`hashtags` (
`id` INT NOT NULL AUTO_INCREMENT,
`hashtag` VARCHAR(255) NULL COMMENT 'hashtag must be unique. Must be saved without #',
`accountId` INT NULL,
`startTracking` DATETIME NULL COMMENT 'When tracking of the hashtag start',
`endTracking` DATETIME NULL COMMENT 'When tracking of the hashtag ends',
`trackingChannels` TINYTEXT NULL COMMENT 'JSON string with all channels that should be tracked',
`created` TIMESTAMP NULL DEFAULT NOW(),
PRIMARY KEY (`id`),
UNIQUE INDEX `hashtags_id` (`id` ASC),
UNIQUE INDEX `hashtags_hashtag` (`hashtag` ASC),
INDEX `hashtags_accountId_idx` (`accountId` ASC),
CONSTRAINT `hashtags_accountId`
FOREIGN KEY (`accountId`)
REFERENCES `local_sysDB`.`accounts` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `local_sysDB`.`apiKeys`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `local_sysDB`.`apiKeys` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `local_sysDB`.`apiKeys` (
`hashtagId` INT NOT NULL,
`accountId` INT NULL,
`clientId` VARCHAR(255) NULL,
`clientSecret` VARCHAR(255) NULL,
`subscriptionId` CHAR(8) NULL COMMENT 'used for instagram subscription',
`subsribtionStatus` TINYINT(1) NULL COMMENT '0 = unsubscribed, 1 = subscribed',
PRIMARY KEY (`hashtagId`),
UNIQUE INDEX `apiKeys_unique` (`accountId` ASC, `hashtagId` ASC),
CONSTRAINT `apiKeys_accountId`
FOREIGN KEY (`accountId`)
REFERENCES `local_sysDB`.`accounts` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `apiKeys_hashtagId`
FOREIGN KEY (`hashtagId`)
REFERENCES `local_sysDB`.`hashtags` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `local_sysDB`.`posts`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `local_sysDB`.`posts` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `local_sysDB`.`posts` (
`hashtagId` INT NOT NULL,
`postId` INT UNSIGNED NOT NULL,
`media` TINYINT(1) NULL,
`duplicate` TINYINT(1) NULL,
`sensitive` TINYINT(1) NULL,
`source` CHAR(10) NULL,
PRIMARY KEY (`hashtagId`, `postId`),
INDEX `posts_hashtagId` (`hashtagId` ASC, `postId` ASC))
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `local_sysDB`.`posts_instagram`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `local_sysDB`.`posts_instagram` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `local_sysDB`.`posts_instagram` (
`hashtagId` INT NOT NULL,
`type` CHAR(8) NULL,
`filter` VARCHAR(45) NULL,
`caption` TEXT NULL,
`userId` INT UNSIGNED NULL,
`created` INT UNSIGNED NULL,
`postId` INT UNSIGNED NOT NULL,
`urlToPost` VARCHAR(255) NULL,
`media` VARCHAR(255) NULL COMMENT 'First encountered media of COALESCE',
`media1` VARCHAR(255) NULL COMMENT 'Media can be image or video',
`media2` VARCHAR(255) NULL,
`media3` VARCHAR(255) NULL,
`media4` VARCHAR(255) NULL,
PRIMARY KEY (`hashtagId`, `postId`),
CONSTRAINT `posts_instagram_post`
FOREIGN KEY (`postId` , `hashtagId`)
REFERENCES `local_sysDB`.`posts` (`postId` , `hashtagId`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `local_sysDB`.`posts_twitter`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `local_sysDB`.`posts_twitter` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `local_sysDB`.`posts_twitter` (
`hashtagId` INT NOT NULL,
`coordinates` POINT NULL,
`created` INT UNSIGNED NULL,
`filterlevel` VARCHAR(45) NULL,
`postId` INT UNSIGNED NOT NULL,
`language` CHAR(11) NULL,
`sensitive` TINYINT(1) NULL,
`retweeted` TINYINT(1) NULL,
`text` CHAR(140) NULL,
`truncated` TINYINT(1) NULL,
`userId` INT UNSIGNED NULL,
`username` CHAR(15) NULL,
`userFullname` CHAR(20) NULL,
`media` VARCHAR(255) NULL COMMENT 'First encountered media of COALESCE',
`media1` VARCHAR(255) NULL,
`media2` VARCHAR(255) NULL,
`media3` VARCHAR(255) NULL,
`media4` VARCHAR(255) NULL,
PRIMARY KEY (`hashtagId`, `postId`),
INDEX `posts_coordinates` (`coordinates` ASC) COMMENT 'should be SPATIAL INDEX',
CONSTRAINT `posts_twitter_post`
FOREIGN KEY (`hashtagId` , `postId`)
REFERENCES `local_sysDB`.`posts` (`hashtagId` , `postId`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `local_sysDB`.`apiRequests`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `local_sysDB`.`apiRequests` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `local_sysDB`.`apiRequests` (
`id` INT NOT NULL,
`accountId` INT NULL,
`ip` INT UNSIGNED NULL,
`requestUrl` VARCHAR(255) NULL,
`test` TINYINT(1) NULL DEFAULT 0 COMMENT 'if API request was made during a test. 1 = true, 0 = false',
`created` TIMESTAMP NULL DEFAULT NOW(),
PRIMARY KEY (`id`),
INDEX `apiRequests_accountId_idx` (`accountId` ASC),
CONSTRAINT `apiRequests_accountId`
FOREIGN KEY (`accountId`)
REFERENCES `local_sysDB`.`accounts` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `local_sysDB`.`hashtagLimit`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `local_sysDB`.`hashtagLimit` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `local_sysDB`.`hashtagLimit` (
`accountId` INT NOT NULL,
`hashtagId` INT NOT NULL,
`postLimit` TINYINT NOT NULL,
PRIMARY KEY (`accountId`),
UNIQUE INDEX `hashtagLimit_unique` (`hashtagId` ASC, `accountId` ASC),
CONSTRAINT `hashtagLimit_accountId`
FOREIGN KEY (`accountId`)
REFERENCES `local_sysDB`.`accounts` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `hashtagLimit_hashtagId`
FOREIGN KEY (`hashtagId`)
REFERENCES `local_sysDB`.`hashtags` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `local_sysDB`.`profanity`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `local_sysDB`.`profanity` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `local_sysDB`.`profanity` (
`id` INT NOT NULL,
`filter` VARCHAR(255) NULL COMMENT 'Must be unique',
`active` TINYINT(1) NULL DEFAULT 1 COMMENT '1 = active, 0 not active',
PRIMARY KEY (`id`),
UNIQUE INDEX `profanity_filter` (`filter` ASC))
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `local_sysDB`.`accountProfanity`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `local_sysDB`.`accountProfanity` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `local_sysDB`.`accountProfanity` (
`id` INT NOT NULL AUTO_INCREMENT,
`accountId` INT NULL,
`filter` VARCHAR(255) NULL,
`created` TIMESTAMP NULL DEFAULT NOW(),
PRIMARY KEY (`id`),
INDEX `accountProfanity_id` (`accountId` ASC),
CONSTRAINT `accountProfanity_accountId`
FOREIGN KEY (`accountId`)
REFERENCES `local_sysDB`.`accounts` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SHOW WARNINGS;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
I get this error:
Error
SQL query:
CREATE TABLE IF NOT EXISTS `local_sysDB`.`posts_instagram` (
`hashtagId` INT NOT NULL,
`type` CHAR(8) NULL,
`filter` VARCHAR(45) NULL,
`caption` TEXT NULL,
`userId` INT UNSIGNED NULL,
`created` INT UNSIGNED NULL,
`postId` INT UNSIGNED NOT NULL,
`urlToPost` VARCHAR(255) NULL,
`media` VARCHAR(255) NULL COMMENT 'First encountered media of COALESCE',
`media1` VARCHAR(255) NULL COMMENT 'Media can be image or video',
`media2` VARCHAR(255) NULL,
`media3` VARCHAR(255) NULL,
`media4` VARCHAR(255) NULL,
PRIMARY KEY (`hashtagId`, `postId`),
CONSTRAINT `posts_instagram_post`
FOREIGN KEY (`postId` , `hashtagId`)
REFERENCES `local_sysDB`.`posts` (`postId` , `hashtagId`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
MySQL said: Documentation
#1215 - Cannot add foreign key constraint
I can't figure out what's wrong.
I'd like to automatically update posts_twitter or posts_instagram when something in posts changes.
I ued mysql workbench to create this query, but I don't get any errors in mysql workbench.
The SHOW WARNINGS statements in your SQL snippet show the following when pasting the snippet into the command line client:
| Warning | 150 | Create table 'local_sysDB/posts_instagram' with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.
| Error | 1005 | Can't create table `local_sysDB`.`posts_instagram` (errno: 150 "Foreign key constraint is incorrectly formed")
| Warning | 1215 | Cannot add foreign key constraint
The key part is "There is no index in the referenced table where the referenced columns appear as the first columns."
The foreign key definition has:
CONSTRAINT `posts_instagram_post`
FOREIGN KEY (`postId` , `hashtagId`)
REFERENCES `local_sysDB`.`posts` (`postId` , `hashtagId`)
while the indexes on the posts table are:
PRIMARY KEY (`hashtagId`, `postId`),
INDEX `posts_hashtagId` (`hashtagId` ASC, `postId` ASC))
which have columns hashtagId and postID in the opposite order
(the second INDEX is redundant btw. as it covers the same columns as the primary key with ASC being the default anyway)
You need to reverse column order on either the foreign key declaration or in the primary key of he posts table to make things work here.

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.