MySQL Cannot Add Foreign Key Constraint -- MySQL Workbench - mysql

When forward Engineering in MySQL Workbench I get the following error:
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
-- -----------------------------------------------------
-- Table `sdosburn_guile`.`Employee`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sdosburn_guile`.`Employee` (
`EmployeeID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`IsManager` TINYINT(1) NOT NULL,
`PasswordHash` BLOB(256) NOT NULL,
`NameLast` VARCHAR(25) NOT NULL,
`NameFirst` VARCHAR(25) NOT NULL,
`DateofBirth` DATE NOT NULL,
`EmailAddress` VARCHAR(50) NOT NULL,
`PhoneNumber` VARCHAR(13) NOT NULL,
`Gender` TINYINT(1) NULL,
`HireDate` DATE NULL,
`_WarehouseID` INT UNSIGNED NOT NULL,
PRIMARY KEY (`EmployeeID`),
INDEX `fk_Employee_Warehouse1_idx` (`_WarehouseID` ASC),
CONSTRAINT `fk_Employee_Warehouse1`
FOREIGN KEY (`_WarehouseID`)
REFERENCES `sdosburn_guile`.`Warehouse` (`WarehouseID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 10 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Attached is my DB information. I've checked data types, size, and everything else I've looked up on the Web, please help!!!!!
-- 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 sdosburn_guile
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema sdosburn_guile
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `sdosburn_guile` DEFAULT CHARACTER SET utf8 ;
-- -----------------------------------------------------
-- Schema named
-- -----------------------------------------------------
USE `sdosburn_guile` ;
-- Table sdosburn_guile.Customer
CREATE TABLE IF NOT EXISTS `sdosburn_guile`.`Customer` (
`CustomerID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`PasswordHash` BLOB(256) NOT NULL,
`NameLast` VARCHAR(25) NULL,
`NameFirst` VARCHAR(25) NULL,
`DateofBirth` DATE NULL,
`EmailAddress` VARCHAR(50) NOT NULL,
`PhoneNumber` VARCHAR(13) NULL,
PRIMARY KEY (`CustomerID`))
ENGINE = InnoDB;
-- Table sdosburn_guile.Book
CREATE TABLE IF NOT EXISTS `sdosburn_guile`.`Book` (
`ItemID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Price` DOUBLE NOT NULL,
`Title` VARCHAR(50) NOT NULL,
`Author` VARCHAR(50) NOT NULL,
`Genre` VARCHAR(20) NULL,
`ReleaseDate` VARCHAR(10) NULL,
`Publisher` VARCHAR(50) NULL,
PRIMARY KEY (`ItemID`))
ENGINE = InnoDB;
-- Table sdosburn_guile.Music
CREATE TABLE IF NOT EXISTS `sdosburn_guile`.`Music` (
`ItemID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Price` DOUBLE NOT NULL,
`Title` VARCHAR(50) NOT NULL,
`Artist` VARCHAR(50) NOT NULL,
`Genre` VARCHAR(20) NULL,
`ReleaseDate` VARCHAR(10) NULL,
`Record Company` VARCHAR(50) NULL,
PRIMARY KEY (`ItemID`))
ENGINE = InnoDB;
-- Table sdosburn_guile.Movie
CREATE TABLE IF NOT EXISTS `sdosburn_guile`.`Movie` (
`ItemID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Price` DOUBLE NOT NULL,
`Title` VARCHAR(50) NOT NULL,
`Genre` VARCHAR(20) NULL,
`ReleaseDate` VARCHAR(10) NULL,
`Actor_1` VARCHAR(50) NULL,
`Actor_2` VARCHAR(50) NULL,
`Actor_3` VARCHAR(50) NULL,
`Director` VARCHAR(50) NULL,
`Production Company` VARCHAR(50) NULL,
PRIMARY KEY (`ItemID`))
ENGINE = InnoDB;
-- Table sdosburn_guile.Warehouse
CREATE TABLE IF NOT EXISTS `sdosburn_guile`.`Warehouse` (
`WarehouseID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`PhoneNumber` VARCHAR(13) NOT NULL,
`_ManageID` INT UNSIGNED NOT NULL,
PRIMARY KEY (`WarehouseID`),
INDEX `fk_Warehouse_Employee1_idx` (`_ManageID` ASC),
CONSTRAINT `fk_Warehouse_Employee1`
FOREIGN KEY (`_ManageID`)
REFERENCES `sdosburn_guile`.`Employee` (`EmployeeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table sdosburn_guile.Employee
CREATE TABLE IF NOT EXISTS `sdosburn_guile`.`Employee` (
`EmployeeID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`IsManager` TINYINT(1) NOT NULL,
`PasswordHash` BLOB(256) NOT NULL,
`NameLast` VARCHAR(25) NOT NULL,
`NameFirst` VARCHAR(25) NOT NULL,
`DateofBirth` DATE NOT NULL,
`EmailAddress` VARCHAR(50) NOT NULL,
`PhoneNumber` VARCHAR(13) NOT NULL,
`Gender` TINYINT(1) NULL,
`HireDate` DATE NULL,
`_WarehouseID` INT UNSIGNED NOT NULL,
PRIMARY KEY (`EmployeeID`),
INDEX `fk_Employee_Warehouse1_idx` (`_WarehouseID` ASC),
CONSTRAINT `fk_Employee_Warehouse1`
FOREIGN KEY (`_WarehouseID`)
REFERENCES `sdosburn_guile`.`Warehouse` (`WarehouseID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table sdosburn_guile.Address
CREATE TABLE IF NOT EXISTS `sdosburn_guile`.`Address` (
`AddressID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Number` VARCHAR(10) NOT NULL,
`Street` VARCHAR(35) NOT NULL,
`State` VARCHAR(2) NOT NULL,
`Secondary` VARCHAR(20) NULL,
`LocalCustomerID` INT NULL,
`LocalEmployeeID` INT NULL,
`LocalWarehouseID` INT NULL,
`Addresscol` VARCHAR(45) NULL,
PRIMARY KEY (`AddressID`),
INDEX `fk_Address_Customer_idx` (`LocalCustomerID` ASC),
INDEX `fk_Address_Employee1_idx` (`LocalEmployeeID` ASC),
INDEX `fk_Address_Warehouse1_idx` (`LocalWarehouseID` ASC),
UNIQUE INDEX `LocalCustomerID_UNIQUE` (`LocalCustomerID` ASC),
UNIQUE INDEX `LocalEmployeeID_UNIQUE` (`LocalEmployeeID` ASC),
UNIQUE INDEX `Addresscol_UNIQUE` (`Addresscol` ASC),
CONSTRAINT `fk_Address_Customer`
FOREIGN KEY (`LocalCustomerID`)
REFERENCES `sdosburn_guile`.`Customer` (`CustomerID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Address_Employee1`
FOREIGN KEY (`LocalEmployeeID`)
REFERENCES `sdosburn_guile`.`Employee` (`EmployeeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Address_Warehouse1`
FOREIGN KEY (`LocalWarehouseID`)
REFERENCES `sdosburn_guile`.`Warehouse` (`WarehouseID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table sdosburn_guile.Billing
CREATE TABLE IF NOT EXISTS `sdosburn_guile`.`Billing` (
`BillingID` INT NOT NULL AUTO_INCREMENT COMMENT 'Remove datatype and have key be the payment name?',
`_CustomerID` INT NOT NULL,
`PaymentType` TINYINT NOT NULL DEFAULT 0,
`PaymentName` VARCHAR(50) NOT NULL COMMENT 'User\'s name for payment method',
`AccountNumber` VARCHAR(19) NOT NULL COMMENT 'Can be card number or bank account number',
`Routing Number` VARCHAR(9) NULL,
`CVV` VARCHAR(3) NULL,
`_BillingAddressID` INT UNSIGNED NOT NULL,
PRIMARY KEY (`BillingID`),
INDEX `fk_Billing_Customer1_idx` (`_CustomerID` ASC),
UNIQUE INDEX `BillingID_UNIQUE` (`BillingID` ASC),
INDEX `fk_Billing_Address1_idx` (`_BillingAddressID` ASC),
CONSTRAINT `fk_Billing_Customer1`
FOREIGN KEY (`_CustomerID`)
REFERENCES `sdosburn_guile`.`Customer` (`CustomerID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Billing_Address1`
FOREIGN KEY (`_BillingAddressID`)
REFERENCES `sdosburn_guile`.`Address` (`AddressID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table sdosburn_guile.ShoppingCart
CREATE TABLE IF NOT EXISTS `sdosburn_guile`.`ShoppingCart` (
`CartID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`_CustomerID` INT NOT NULL,
INDEX `fk_ShoppingCart_Customer1_idx` (`_CustomerID` ASC),
PRIMARY KEY (`CartID`),
CONSTRAINT `fk_ShoppingCart_Customer1`
FOREIGN KEY (`_CustomerID`)
REFERENCES `sdosburn_guile`.`Customer` (`CustomerID`)
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;

Your Foreign Key _ManageID in the Warehouse table is not a primary key in another table, that's why you're running into an error. Once you fix that, you should be ok, but I haven't verified it.

For those that have this issue but everything looks to be perfectly fine. What worked for me is dropping all tables and running the forward engineer script again.

Related

MySQL Foreign-keys

I've attempted to create foreign keys in my Movie_Copies table which references the primary keys in the tables Movie_Details and Customer_Details but it doesn't seem to work for me and the syntax error that appears seems to be with the indexing.
-- -----------------------------------------------------
-- Table `Movie_Renting`.`Movie_Details`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Movie_Renting`.`Movie_Details` (
`Movie_ID` VARCHAR(20) NOT NULL,
`Movie_Title` VARCHAR(256) NOT NULL,
`Release_Date` DATE NOT NULL,
`Run_Time` VARCHAR(10) NOT NULL,
`Genre` VARCHAR(20) NOT NULL,
`Rating` INT NOT NULL,
`Number_of_Copies` INT NOT NULL,
PRIMARY KEY (`Movie_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `Movie_Renting`.`Customer_Details`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Movie_Renting`.`Customer_Details` (
`Customer_ID` VARCHAR(10) NOT NULL,
`Customer_Name` VARCHAR(800) NOT NULL,
`Address_Line_One` VARCHAR(100) NOT NULL,
`Address_Line_Two` VARCHAR(100) NULL,
`Town` VARCHAR(90) NOT NULL,
`Post_Code` VARCHAR(8) NOT NULL,
`Contact_Number` VARCHAR(12) NOT NULL,
`Email_Address` VARCHAR(345) NOT NULL,
PRIMARY KEY (`Customer_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `Movie_Renting`.`Movie_Copies`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Movie_Renting`.`Movie_Copies` (
`Copy_ID` VARCHAR(10) NOT NULL,
`Movie_ID` VARCHAR(10) NOT NULL,
`Platform` VARCHAR(20) NOT NULL,
`Customer_ID` VARCHAR(45) NOT NULL,
`Rent_Out` DATE NOT NULL,
`Duration_Weeks` INT NOT NULL,
`Return_Date` DATE NOT NULL,
`Customer_Details_Customer_ID` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Copy_ID`),
INDEX `fk_Movie_Copies_Movie_Details_idx` (`Movie_ID` ASC) VISIBLE,
INDEX `fk_Movie_Copies_Customer_Details1_idx` (`Customer_ID` ASC) VISIBLE,
CONSTRAINT `fk_Movie_Copies_Movie_Details`
FOREIGN KEY (`Movie_ID`)
REFERENCES `Movie_Renting`.`Movie_Details` (`Movie_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Movie_Copies_Customer_Details1`
FOREIGN KEY (`Customer_ID`)
REFERENCES `Movie_Renting`.`Customer_Details` (`Customer_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

Error Code 1215. Cannot add foreign key constraint

Creating a new database here for school and having difficulty in understanding what's wrong here.
For example, I want to create this table (automated SQL output):
-- -----------------------------------------------------
-- Table `jobsearch`.`Employer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `jobsearch`.`Employer`
(
`EmployerID` SMALLINT(5) NOT NULL AUTO_INCREMENT,
`IndustryID` SMALLINT(5) NOT NULL,
`Address` VARCHAR(45) NOT NULL,
`City` VARCHAR(45) NOT NULL,
`State` CHAR(2) NOT NULL,
`Zip` VARCHAR(5) NOT NULL,
`Phone` VARCHAR(10) NOT NULL,
PRIMARY KEY (`EmployerID`, `IndustryID`),
INDEX `fk_Employer_Industry1_idx` (`IndustryID` ASC),
CONSTRAINT `fk_Employer_Industry1`
FOREIGN KEY (`IndustryID`) REFERENCES `job search`.`Industry` (`IndustryID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I have this table that the foreign should be referencing (This table was created without any issue):
CREATE TABLE IF NOT EXISTS `jobsearch`.`Industry`
(
`IndustryID` INT NOT NULL AUTO_INCREMENT,
`IndustryName` VARCHAR(45) NOT NULL,
`Region` VARCHAR(45) NOT NULL,
PRIMARY KEY (`IndustryID`))
ENGINE = InnoDB;
The datatype for the referencing column must match exactly that of the referenced column. You've defined `Industry.IndustryID as
`IndustryID` INT ...
and Employer.IndustryID as
`IndustryID` SMALLINT(5) ...
Change Employer.IndustryID to INT and you should be shiny.

Error 1215: Cannot add foreign key constraint when forward engineering

when I try to forward engineer my new schema, I am getting this error. Anyone can offer your assistance to ?
-- -----------------------------------------------------
-- Table `SLIOP`.`schedule`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SLIOP`.`schedule` (
`scheduleID` INT NOT NULL AUTO_INCREMENT,
`lecturerID` INT NOT NULL,
`courseID` INT NOT NULL,
`type` VARCHAR(30) NOT NULL,
PRIMARY KEY (`scheduleID`),
INDEX `fk_schedule_academic_staff1_idx` (`lecturerID` ASC),
INDEX `fk_schedule_course1_idx` (`courseID` ASC),
CONSTRAINT `lecturerID`
FOREIGN KEY (`lecturerID`)
REFERENCES `SLIOP`.`academic_staff` (`lecturerID`)
CONSTRAINT `courseID`
FOREIGN KEY (`courseID`)
REFERENCES `SLIOP`.`course` (`courseID`)
ENGINE = InnoDB
Parent tables
-- -----------------------------------------------------
-- Table `SLIOP`.`course`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SLIOP`.`course` (
`courseID` INT NOT NULL AUTO_INCREMENT,
`course_code` VARCHAR(10) NOT NULL,
`course_name` VARCHAR(40) NOT NULL,
`lecturer_name` VARCHAR(40) NOT NULL,
`time` TIMESTAMP NOT NULL,
`fee` DECIMAL(10,2) NOT NULL,
`requirement` MEDIUMTEXT NOT NULL,
`lecturerID` INT NOT NULL,
PRIMARY KEY (`courseID`),
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SLIOP`.`academic_staff`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SLIOP`.`academic_staff` (
`lecturerID` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(60) NOT NULL,
`last_name` VARCHAR(60) NOT NULL,
`profile_image` BLOB NULL,
PRIMARY KEY (`lecturerID`))
ENGINE = InnoDB;
I have searched related posts here, but i couldn't find where is my error.
Main issue I believe is a missing comma before CONSTRAINT courseID
But you missed several closing braces as well.
So here are working statements:
http://sqlfiddle.com/#!9/2bb11
CREATE TABLE IF NOT EXISTS `course` (
`courseID` INT NOT NULL AUTO_INCREMENT,
`course_code` VARCHAR(10) NOT NULL,
`course_name` VARCHAR(40) NOT NULL,
`lecturer_name` VARCHAR(40) NOT NULL,
`time` TIMESTAMP NOT NULL,
`fee` DECIMAL(10,2) NOT NULL,
`requirement` MEDIUMTEXT NOT NULL,
`lecturerID` INT NOT NULL,
PRIMARY KEY (`courseID`)
)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `academic_staff` (
`lecturerID` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(60) NOT NULL,
`last_name` VARCHAR(60) NOT NULL,
`profile_image` BLOB NULL,
PRIMARY KEY (`lecturerID`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `schedule` (
`scheduleID` INT NOT NULL AUTO_INCREMENT,
`lecturerID` INT NOT NULL,
`courseID` INT NOT NULL,
`type` VARCHAR(30) NOT NULL,
PRIMARY KEY (`scheduleID`),
INDEX `fk_schedule_academic_staff1_idx` (`lecturerID` ASC),
INDEX `fk_schedule_course1_idx` (`courseID` ASC),
CONSTRAINT `lecturerID`
FOREIGN KEY (`lecturerID`)
REFERENCES `academic_staff` (`lecturerID`),
CONSTRAINT `courseID`
FOREIGN KEY (`courseID`)
REFERENCES `course` (`courseID`))
ENGINE = InnoDB
I have been able to create the tables without any issue, after correcting the syntax errors. Just make sure you are running the scripts in correct order. i.e.
Table SLIOP.schedule should be created last since it is referencing
SLIOP.academic_staff and SLIOP.course

Forward engineering mysql workbench

im stuck and have an error in forward engineering my model :( i dont know how to solve it :/ i had a similar issue before and it was just about changing the position of the primary key and then it worked but this time it doesnt for some reason :/
Executing SQL script in server
ERROR: Error 1005: Can't create table 'werkzeuge.flansch' (errno: 150)
SQL Code:
-- -----------------------------------------------------
-- Table `werkzeuge`.`flansch`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `werkzeuge`.`flansch` (
`typ` VARCHAR(45) NOT NULL,
`bestellt` INT NULL,
`fertigung` INT NULL,
`lieferant` VARCHAR(50) NULL,
`lager` INT NULL,
`lagerplatz` VARCHAR(45) NULL,
`gewicht` DECIMAL NULL,
`e_k_preis` DECIMAL NULL,
`v_k_preis` DECIMAL NULL,
PRIMARY KEY (`typ`),
INDEX `fk_flansch_werkzeugführungen1_idx` (`typ` ASC),
CONSTRAINT `fk_flansch_werkzeugführungen1`
FOREIGN KEY (`typ`)
REFERENCES `werkzeuge`.`werkzeugführungen` (`flansch`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 8 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
And this is my create statement for my tables for the database
CREATE SCHEMA IF NOT EXISTS `werkzeuge` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `werkzeuge` ;
-- -----------------------------------------------------
-- Table `werkzeuge`.`maschinen`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `werkzeuge`.`maschinen` (
`werkzeugsatz` VARCHAR(45) NOT NULL,
`typ` VARCHAR(50) NOT NULL,
`maschinenkörper` VARCHAR(45) NOT NULL,
`elektrik` VARCHAR(45) NOT NULL,
`pneumatic` VARCHAR(45) NOT NULL,
`hydraulik` VARCHAR(45) NOT NULL,
`kühlvorrichtung` VARCHAR(45) NOT NULL,
`vorschubeinheit` VARCHAR(45) NOT NULL,
PRIMARY KEY (`werkzeugsatz`, `typ`, `maschinenkörper`, `elektrik`, `pneumatic`, `hydraulik`, `kühlvorrichtung`, `vorschubeinheit`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `werkzeuge`.`werkzeugsätze_gierth`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `werkzeuge`.`werkzeugsätze_gierth` (
`werkzeugführung` VARCHAR(45) NOT NULL,
`messerhalter` VARCHAR(45) NOT NULL,
`zentrierkronen` VARCHAR(45) NOT NULL,
`spanvorichtung` VARCHAR(45) NOT NULL,
`werkzeugsatz` VARCHAR(45) NOT NULL,
PRIMARY KEY (`werkzeugführung`, `messerhalter`, `zentrierkronen`, `spanvorichtung`),
INDEX `fk_werkzeugsätze_gierth_maschinen_idx` (`werkzeugsatz` ASC),
CONSTRAINT `fk_werkzeugsätze_gierth_maschinen`
FOREIGN KEY (`werkzeugsatz`)
REFERENCES `werkzeuge`.`maschinen` (`werkzeugsatz`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `werkzeuge`.`werkzeugführungen`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `werkzeuge`.`werkzeugführungen` (
`oberteilführung` VARCHAR(45) NOT NULL,
`flansch` VARCHAR(45) NOT NULL,
`passfeder` VARCHAR(45) NOT NULL,
`flansch_mit_führung` VARCHAR(45) NOT NULL,
`länge` INT NULL,
`wf_komplett` VARCHAR(45) NULL,
`jahresbedarf` VARCHAR(45) NULL,
`typ` VARCHAR(45) NOT NULL,
PRIMARY KEY (`oberteilführung`, `flansch`, `passfeder`, `flansch_mit_führung`),
INDEX `fk_werkzeugführungen_werkzeugsätze_gierth1_idx` (`typ` ASC),
CONSTRAINT `fk_werkzeugführungen_werkzeugsätze_gierth1`
FOREIGN KEY (`typ`)
REFERENCES `werkzeuge`.`werkzeugsätze_gierth` (`werkzeugführung`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `werkzeuge`.`flansch`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `werkzeuge`.`flansch` (
`typ` VARCHAR(45) NOT NULL,
`bestellt` INT NULL,
`fertigung` INT NULL,
`lieferant` VARCHAR(50) NULL,
`lager` INT NULL,
`lagerplatz` VARCHAR(45) NULL,
`gewicht` DECIMAL NULL,
`e_k_preis` DECIMAL NULL,
`v_k_preis` DECIMAL NULL,
PRIMARY KEY (`typ`),
INDEX `fk_flansch_werkzeugführungen1_idx` (`typ` ASC),
CONSTRAINT `fk_flansch_werkzeugführungen1`
FOREIGN KEY (`typ`)
REFERENCES `werkzeuge`.`werkzeugführungen` (`flansch`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `werkzeuge`.`oberteilführung`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `werkzeuge`.`oberteilführung` (
`typ` VARCHAR(45) NOT NULL,
`bestellt` INT NULL,
`fertigung` VARCHAR(45) NULL,
`lieferant` VARCHAR(45) NULL,
`lager` VARCHAR(45) NULL,
`lagerplatz` VARCHAR(45) NULL,
`gewicht` VARCHAR(45) NULL,
`e_k_preis` VARCHAR(45) NULL,
`v_k_preis` VARCHAR(45) NULL,
PRIMARY KEY (`typ`),
INDEX `fk_oberteilführung_werkzeugführungen1_idx` (`typ` ASC),
CONSTRAINT `fk_oberteilführung_werkzeugführungen1`
FOREIGN KEY (`typ`)
REFERENCES `werkzeuge`.`werkzeugführungen` (`oberteilführung`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `werkzeuge`.`flansch_mit_führung`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `werkzeuge`.`flansch_mit_führung` (
`typ` VARCHAR(45) NOT NULL,
`bestellt` INT NULL,
`fertigung` VARCHAR(45) NULL,
`lieferant` VARCHAR(45) NULL,
`lager` VARCHAR(45) NULL,
`lagerplatz` VARCHAR(45) NULL,
`gewicht` VARCHAR(45) NULL,
`e_k_preis` VARCHAR(45) NULL,
`v_k_preis` VARCHAR(45) NULL,
INDEX `fk_flansch_mit_führung_werkzeugführungen1_idx` (`typ` ASC),
PRIMARY KEY (`typ`),
CONSTRAINT `fk_flansch_mit_führung_werkzeugführungen1`
FOREIGN KEY (`typ`)
REFERENCES `werkzeuge`.`werkzeugführungen` (`flansch_mit_führung`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `werkzeuge`.`passfeder`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `werkzeuge`.`passfeder` (
`typ` VARCHAR(45) NOT NULL,
`bestellt` INT NULL,
`fertigung` VARCHAR(45) NULL,
`lieferant` VARCHAR(45) NULL,
`lager` VARCHAR(45) NULL,
`lagerplatz` VARCHAR(45) NULL,
`gewicht` VARCHAR(45) NULL,
`e_k_preis` VARCHAR(45) NULL,
`v_k_preis` VARCHAR(45) NULL,
PRIMARY KEY (`typ`),
INDEX `fk_passfeder_werkzeugführungen1_idx` (`typ` ASC),
CONSTRAINT `fk_passfeder_werkzeugführungen1`
FOREIGN KEY (`typ`)
REFERENCES `werkzeuge`.`werkzeugführungen` (`passfeder`)
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;
When you're creating the table werkzeuge.werkzeugführungen
`flansch` VARCHAR(45) NOT NULL
must actually be
`flansch` VARCHAR(45) NOT NULL UNIQUE
The flansch column defines the primary key in werkzeugführungen and primary key must be unique.

ERROR: Error 1067: Invalid default value for ON UPDATE CURRENT_TIMESTAMP

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";