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.
Related
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.
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;
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;
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.
I run the script from Workbench. Here is the complete script:
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
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- Table mydb.categories
DROP TABLE IF EXISTS `mydb`.`categories` ;
CREATE TABLE IF NOT EXISTS `mydb`.`categories` (
`categories_id` INT(5) UNSIGNED NOT NULL,
`categories_name` VARCHAR(32) NOT NULL,
`categories_image` VARCHAR(64) NULL,
`parent_id` INT(5) UNSIGNED NOT NULL,
`sort_order` INT(3) NULL,
`date_added` TIMESTAMP NOT NULL DEFAULT 0,
`last_modified` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`categories_id`),
INDEX `fk_categories_categories1_idx` (`parent_id` ASC),
CONSTRAINT `fk_categories_categories1`
FOREIGN KEY (`parent_id`)
REFERENCES `mydb`.`categories` (`categories_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.manufacturers
DROP TABLE IF EXISTS `mydb`.`manufacturers` ;
CREATE TABLE IF NOT EXISTS `mydb`.`manufacturers` (
`manufacturers_id` INT(5) UNSIGNED NOT NULL,
`manufacturers_name` VARCHAR(32) NOT NULL,
`date_added` TIMESTAMP NOT NULL DEFAULT 0,
PRIMARY KEY (`manufacturers_id`))
ENGINE = InnoDB;
-- Table mydb.products
DROP TABLE IF EXISTS `mydb`.`products` ;
CREATE TABLE IF NOT EXISTS `mydb`.`products` (
`products_id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`products_model` VARCHAR(20) NULL,
`products_price` DECIMAL(10,2) UNSIGNED NULL,
`products_weight` DECIMAL(4,2) UNSIGNED NULL,
`manufacturers_id` INT(5) UNSIGNED NOT NULL,
PRIMARY KEY (`products_id`),
INDEX `fk_products_manufacturers1_idx` (`manufacturers_id` ASC),
CONSTRAINT `manufacturers_id`
FOREIGN KEY (`manufacturers_id`)
REFERENCES `mydb`.`manufacturers` (`manufacturers_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.categories_has_products
DROP TABLE IF EXISTS `mydb`.`categories_has_products` ;
CREATE TABLE IF NOT EXISTS `mydb`.`categories_has_products` (
`categories_id` INT(5) UNSIGNED NOT NULL,
`products_id` INT(5) UNSIGNED NOT NULL,
PRIMARY KEY (`categories_id`, `products_id`),
INDEX `fk_categories_has_products_products_idx` (`products_id` ASC),
INDEX `fk_categories_has_products_categories_idx` (`categories_id` ASC),
CONSTRAINT `categories_id`
FOREIGN KEY (`categories_id`)
REFERENCES `mydb`.`categories` (`categories_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `products_id`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.products_description
DROP TABLE IF EXISTS `mydb`.`products_description` ;
CREATE TABLE IF NOT EXISTS `mydb`.`products_description` (
`products_id` INT(5) UNSIGNED NOT NULL,
`products_name` VARCHAR(64) NOT NULL,
`products_description` TEXT NULL,
`products_url` VARCHAR(255) NULL,
`products_viewed` INT(5) UNSIGNED NULL,
PRIMARY KEY (`products_id`),
UNIQUE INDEX `products_name_UNIQUE` (`products_name` ASC),
CONSTRAINT `products_id`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.customers
DROP TABLE IF EXISTS `mydb`.`customers` ;
CREATE TABLE IF NOT EXISTS `mydb`.`customers` (
`customers_id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`customers_gender` CHAR(1) NULL,
`customers_firstname` VARCHAR(32) NOT NULL,
`customers_lastname` VARCHAR(32) NULL,
`customers_dob` DATE NULL,
`customers_email_address` VARCHAR(96) NULL,
`customers_default_address_id` INT(5) UNSIGNED NULL,
`customers_telephone` VARCHAR(32) NULL,
`customers_fax` VARCHAR(32) NULL,
`customers_password` VARCHAR(40) NULL,
`customers_newsletter` CHAR(1) NULL,
`customers_info_date_of_last_logon` DATETIME NOT NULL,
`customers_info_number_of_logons` INT(5) UNSIGNED NOT NULL,
`customers_info_date_account_created` TIMESTAMP NOT NULL DEFAULT 0,
`customers_info_date_account_last_modified` TIMESTAMP NOT NULL,
PRIMARY KEY (`customers_id`))
ENGINE = InnoDB;
-- Table mydb.reviews
DROP TABLE IF EXISTS `mydb`.`reviews` ;
CREATE TABLE IF NOT EXISTS `mydb`.`reviews` (
`reviews_id` INT(5) UNSIGNED NOT NULL,
`products_id` INT(5) UNSIGNED NOT NULL,
`customers_id` INT(5) UNSIGNED NOT NULL,
`customers_name` VARCHAR(64) NULL,
`reviews_rating` INT(1) UNSIGNED NULL,
`date_added` TIMESTAMP NOT NULL DEFAULT 0,
`last_modified` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`reviews_read` INT(5) UNSIGNED NULL,
`reviews_text` TEXT NULL,
PRIMARY KEY (`reviews_id`),
INDEX `fk_reviews_products1_idx` (`products_id` ASC),
INDEX `fk_reviews_customers1_idx` (`customers_id` ASC),
CONSTRAINT `fk_reviews_products1`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_reviews_customers1`
FOREIGN KEY (`customers_id`)
REFERENCES `mydb`.`customers` (`customers_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.specials
DROP TABLE IF EXISTS `mydb`.`specials` ;
CREATE TABLE IF NOT EXISTS `mydb`.`specials` (
`specials_id` INT(5) UNSIGNED NOT NULL,
`products_id` INT(5) UNSIGNED NOT NULL,
`specials_new_products_price` DECIMAL(10,2) UNSIGNED NULL,
`specials_date_added` TIMESTAMP NOT NULL DEFAULT 0,
`specials_last_modified` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`specials_id`),
INDEX `fk_specials_products1_idx` (`products_id` ASC),
CONSTRAINT `fk_specials_products1`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.orders
DROP TABLE IF EXISTS `mydb`.`orders` ;
CREATE TABLE IF NOT EXISTS `mydb`.`orders` (
`orders_id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`customers_id` INT(5) UNSIGNED NOT NULL,
`customers_street_address` VARCHAR(64) NOT NULL,
`customers_suburb` VARCHAR(32) NULL,
`customers_city` VARCHAR(32) NOT NULL,
`customers_postcode` VARCHAR(10) NULL,
`customers_state` VARCHAR(32) NULL,
`customers_country` VARCHAR(32) NULL,
`customers_telephone` VARCHAR(32) NULL,
`customers_email_address` VARCHAR(96) NULL,
`delivery_name` VARCHAR(64) NULL,
`delivery_street_address` VARCHAR(64) NULL,
`delivery_suburb` VARCHAR(32) NULL,
`delivery_city` VARCHAR(32) NULL,
`delivery_postcode` VARCHAR(10) NULL,
`delivery_state` VARCHAR(32) NULL,
`delivery_country` VARCHAR(32) NULL,
`payment_method` VARCHAR(12) NULL,
`cc_type` VARCHAR(20) NULL,
`cc_owner` VARCHAR(64) NULL,
`cc_number` VARCHAR(32) NULL,
`cc_expires` VARCHAR(4) NULL,
`last_modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`date_purchased` TIMESTAMP NOT NULL DEFAULT 0,
`shipping_cost` DECIMAL(10,2) UNSIGNED NULL,
`shipping_method` VARCHAR(32) NULL,
`orders_status` VARCHAR(10) NULL,
`orders_date_finished` DATETIME NULL,
`comments` TEXT NULL,
`currency` VARCHAR(3) NULL,
`currency_value` DECIMAL(16,6) NULL,
PRIMARY KEY (`orders_id`),
INDEX `fk_orders_customers1_idx` (`customers_id` ASC),
CONSTRAINT `fk_orders_customers1`
FOREIGN KEY (`customers_id`)
REFERENCES `mydb`.`customers` (`customers_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.ordered_products
DROP TABLE IF EXISTS `mydb`.`ordered_products` ;
CREATE TABLE IF NOT EXISTS `mydb`.`ordered_products` (
`orders_id` INT(5) UNSIGNED NOT NULL,
`products_id` INT(5) UNSIGNED NOT NULL,
`products_size_id` TINYINT UNSIGNED NOT NULL,
`products_color_id` TINYINT UNSIGNED NOT NULL,
`products_price` DECIMAL(10,2) UNSIGNED NOT NULL,
`quantity` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`orders_id`, `products_id`, `products_size_id`, `products_color_id`),
INDEX `fk_orders_has_products_products1_idx` (`products_id` ASC),
INDEX `fk_orders_has_products_orders1_idx` (`orders_id` ASC),
CONSTRAINT `fk_orders_has_products_orders1`
FOREIGN KEY (`orders_id`)
REFERENCES `mydb`.`orders` (`orders_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_orders_has_products_products1`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.ordered_products
DROP TABLE IF EXISTS `mydb`.`ordered_products` ;
CREATE TABLE IF NOT EXISTS `mydb`.`ordered_products` (
`orders_id` INT(5) UNSIGNED NOT NULL,
`products_id` INT(5) UNSIGNED NOT NULL,
`products_size_id` TINYINT UNSIGNED NOT NULL,
`products_color_id` TINYINT UNSIGNED NOT NULL,
`products_price` DECIMAL(10,2) UNSIGNED NOT NULL,
`quantity` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`orders_id`, `products_id`, `products_size_id`, `products_color_id`),
INDEX `fk_orders_has_products_products1_idx` (`products_id` ASC),
INDEX `fk_orders_has_products_orders1_idx` (`orders_id` ASC),
CONSTRAINT `fk_orders_has_products_orders1`
FOREIGN KEY (`orders_id`)
REFERENCES `mydb`.`orders` (`orders_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_orders_has_products_products1`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.products_size
DROP TABLE IF EXISTS `mydb`.`products_size` ;
CREATE TABLE IF NOT EXISTS `mydb`.`products_size` (
`products_size_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`products_size_name` VARCHAR(15) NOT NULL,
PRIMARY KEY (`products_size_id`))
ENGINE = InnoDB;
-- Table mydb.products_color
DROP TABLE IF EXISTS `mydb`.`products_color` ;
CREATE TABLE IF NOT EXISTS `mydb`.`products_color` (
`products_color_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`products_color_name` VARCHAR(20) NOT NULL,
PRIMARY KEY (`products_color_id`))
ENGINE = InnoDB;
-- Table mydb.products_attributes
DROP TABLE IF EXISTS `mydb`.`products_attributes` ;
CREATE TABLE IF NOT EXISTS `mydb`.`products_attributes` (
`products_id` INT(5) UNSIGNED NOT NULL,
`products_size_id` TINYINT UNSIGNED NOT NULL,
`products_color_id` TINYINT UNSIGNED NOT NULL,
`products_quantity` INT(4) UNSIGNED NOT NULL,
`products_image` VARCHAR(64) NULL,
`products_date_added` TIMESTAMP NOT NULL DEFAULT 0,
`products_last_modified` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`products_date_available` DATETIME NULL,
`products_status` TINYINT UNSIGNED NULL,
PRIMARY KEY (`products_id`, `products_size_id`, `products_color_id`),
INDEX `fk_products_attributes_products_sizes1_idx` (`products_size_id` ASC),
INDEX `fk_products_attributes_products_colors1_idx` (`products_color_id` ASC),
CONSTRAINT `fk_products_attributes_products1`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_products_attributes_products_sizes1`
FOREIGN KEY (`products_size_id`)
REFERENCES `mydb`.`products_size` (`products_size_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_products_attributes_products_colors1`
FOREIGN KEY (`products_color_id`)
REFERENCES `mydb`.`products_color` (`products_color_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;
And here is the complete error:
Executing SQL script in server
ERROR: Error 1067: Invalid default value for 'last_modified'
SQL Code:
CREATE TABLE IF NOT EXISTS `mydb`.`categories` (
`categories_id` INT(5) UNSIGNED NOT NULL,
`categories_name` VARCHAR(32) NOT NULL,
`categories_image` VARCHAR(64) NULL,
`parent_id` INT(5) UNSIGNED NOT NULL,
`sort_order` INT(3) NULL,
`date_added` TIMESTAMP NOT NULL DEFAULT 0,
`last_modified` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`categories_id`),
INDEX `fk_categories_categories1_idx` (`parent_id` ASC),
CONSTRAINT `fk_categories_categories1`
FOREIGN KEY (`parent_id`)
REFERENCES `mydb`.`categories` (`categories_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 7 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
It is because of the SQL_MODE you are setting.
TRADITIONAL and ALLOW_INVALID_DATES restricts the TIMESTAMP type columns for not being set with a default value.
By defining any of the following should work on TIMESTAMP type columns.
DEFAULT 0
DEFAULT CURRENT_TIMESTAMP
Alternately, by just setting the SQL_MODE to ALLOW_INVALID_DATES would need no changes to your script.
Others:
Constraint names MUST be unique. Table products_description defines Constraint products_id but the same name was already used in table categories_has_products.
Maintain unique constraint names.
Refer to:
Important SQL Modes
TRADITIONAL
ALLOW_INVALID_DATES
Change the SQL_MODE like below:
SET GLOBAL sql_mode = 'ALLOW_INVALID_DATES’;
SET SESSION sql_mode = 'ALLOW_INVALID_DATES';
Since last_modified cannot be null by your definition, you have to provide a default value as well:
`last_modified` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW()
I had the same problem and too many dates to change. I set this at the top of my .sql script and all was well.
SET sql_mode = 'ALLOW_INVALID_DATES';
I`m not sure if its correct but I set on "my sql settings -> sql-mode as none or something like this (the other beyond "sql-mode -> user-mode") solved my problem
So in my case ubuntu 16.04 instance with lemp
i had to change the mysql time stamp so log into mysql and execute below commands
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";