I can't add a foreign key constraint? Mysql - mysql

I cannot create the second table because Mysql prints out the message with Error Code 12 15, but i do not understand what is the problem in the script..
There are my two tables:
CREATE TABLE IF NOT EXISTS `tsmdb_centralized`.`customer_accounts` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`customerAccountName` VARCHAR(50) NOT NULL,
`customerAccountUser` VARCHAR(50) NOT NULL,
`customerAccountServer` VARCHAR(45) NOT NULL,
`password` VARCHAR(20) NOT NULL,
`status` TINYINT(50) NOT NULL,
PRIMARY KEY (`id`, `customerAccountServer`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `tsmdb_centralized`.`bugs_etl`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `tsmdb_centralized`.`bugs_etl` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`bug_title` VARCHAR(45) NOT NULL,
`bug_description` VARCHAR(500) NULL,
`customerAccountServer` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_bugs_etl_customer_accounts_idx` (`customerAccountServer` ASC),
CONSTRAINT `fk_bugs_etl_customer_accounts`
FOREIGN KEY (`customerAccountServer`)
REFERENCES `tsmdb_centralized`.`customer_accounts` (`customerAccountServer`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

i found the error, you want to get a foreign key 'CustomerAccountServer' varchar(50) and you can only have a foreign key referencing a unique field. Modify your customer_accounts table so that the customeraccountServer field is unique.
CREATE TABLE IF NOT EXISTS `tsmdb_centralized`.`customer_accounts` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`customerAccountName` VARCHAR(50) NOT NULL,
`customerAccountUser` VARCHAR(50) NOT NULL,
`customerAccountServer` VARCHAR(45) NOT NULL,
`password` VARCHAR(20) NOT NULL,
`status` TINYINT(50) NOT NULL,
PRIMARY KEY (`id`, `customerAccountServer`),
UNIQUE KEY `customerAccountServer_UNIQUE` (`customerAccountServer`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

I run this Script:
CREATE TABLE customer_accounts (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
customerAccountName VARCHAR(50) NOT NULL,
customerAccountUser VARCHAR(50) NOT NULL,
customerAccountServer VARCHAR(45) NOT NULL,
password VARCHAR(20) NOT NULL,
status TINYINT(50) NOT NULL,
UNIQUE KEY Cat_customerAccountServer (customerAccountServer)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE bugs_etl (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
bug_title VARCHAR(45) NOT NULL,
bug_description VARCHAR(500) NULL,
customerAccountServer VARCHAR(45) NOT NULL,
INDEX fk_bugs_etl_customer_accounts_idx(customerAccountServer ASC),
FOREIGN KEY fk_cat(customerAccountServer)
REFERENCES customer_accounts(customerAccountServer)
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
insert into customer_accounts (customerAccountName,customerAccountUser,
customerAccountServer,password,status)
values('nuevo','nuevo','nuevo','1234',1);
insert into customer_accounts (customerAccountName,customerAccountUser,
customerAccountServer,password,status)
values('nuevo','nuevo','nuevo2','1234',1);
insert into bugs_etl (bug_title,bug_description,
customerAccountServer)
values('nuevo','nuevo','nuevo2');
then i can get:
select * from customer_accounts
join bugs_etl
on customer_accounts.customerAccountServer = bugs_etl.customerAccountServer

Related

MySQL Cannot Add Foreign Key Constraint -- MySQL Workbench

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.

Forward engineering mysql Foreign key

im trying to forward engineer my EER diagram in mysql workbench but i seem to be not able to do it
ive got a couple one to many relationships and also changed the names of the foreign keys but to no avail :(
this is from the error log
Executing SQL script in server
ERROR: Error 1005: Can't create table 'lagerprogram.werkzeugsätze_gierth' (errno: 150)
SQL Code:
-- -----------------------------------------------------
-- Table `lagerprogram`.`werkzeugsätze_gierth`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lagerprogram`.`werkzeugsätze_gierth` (
`werkzeugführung` VARCHAR(45) NOT NULL,
`messerhalter` VARCHAR(45) NOT NULL,
`zentrierkronen` VARCHAR(45) NOT NULL,
`spanvorichtung` VARCHAR(45) NOT NULL,
`werkzeugsatzQ` VARCHAR(45) NOT NULL,
PRIMARY KEY (`werkzeugführung`, `messerhalter`, `zentrierkronen`, `spanvorichtung`),
INDEX `fk_werkzeugsätze_gierth_maschinen_idx` (`werkzeugsatzQ` ASC),
CONSTRAINT `fk_werkzeugsätze_gierth_maschinen`
FOREIGN KEY (`werkzeugsatzQ`)
REFERENCES `lagerprogram`.`maschinen` (`werkzeugsatz`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 6 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
And this is my create statement
CREATE SCHEMA IF NOT EXISTS `lagerprogram` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `lagerprogram` ;
-- -----------------------------------------------------
-- Table `lagerprogram`.`maschinen`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lagerprogram`.`maschinen` (
`typ` VARCHAR(50) NOT NULL,
`werkzeugsatz` VARCHAR(45) 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 (`typ`, `werkzeugsatz`, `maschinenkörper`, `elektrik`, `pneumatic`, `hydraulik`, `kühlvorrichtung`, `vorschubeinheit`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `lagerprogram`.`werkzeugsätze_gierth`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lagerprogram`.`werkzeugsätze_gierth` (
`werkzeugführung` VARCHAR(45) NOT NULL,
`messerhalter` VARCHAR(45) NOT NULL,
`zentrierkronen` VARCHAR(45) NOT NULL,
`spanvorichtung` VARCHAR(45) NOT NULL,
`werkzeugsatzQ` VARCHAR(45) NOT NULL,
PRIMARY KEY (`werkzeugführung`, `messerhalter`, `zentrierkronen`, `spanvorichtung`),
INDEX `fk_werkzeugsätze_gierth_maschinen_idx` (`werkzeugsatzQ` ASC),
CONSTRAINT `fk_werkzeugsätze_gierth_maschinen`
FOREIGN KEY (`werkzeugsatzQ`)
REFERENCES `lagerprogram`.`maschinen` (`werkzeugsatz`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `lagerprogram`.`werkzeugführungen`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lagerprogram`.`werkzeugführungen` (
`länge` INT NULL,
`wf_komplett` VARCHAR(45) NULL,
`jahresbedarf` VARCHAR(45) NULL,
`flansch` VARCHAR(45) NOT NULL,
`oberteilführung` VARCHAR(45) NOT NULL,
`flansch_mit_führung` VARCHAR(45) NOT NULL,
`passfeder` VARCHAR(45) NOT NULL,
`typQ` VARCHAR(45) NOT NULL,
PRIMARY KEY (`flansch`, `oberteilführung`, `flansch_mit_führung`, `passfeder`),
INDEX `fk_werkzeugführungen_werkzeugsätze_gierth1_idx` (`typQ` ASC),
CONSTRAINT `fk_werkzeugführungen_werkzeugsätze_gierth1`
FOREIGN KEY (`typQ`)
REFERENCES `lagerprogram`.`werkzeugsätze_gierth` (`werkzeugführung`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `lagerprogram`.`flansch`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lagerprogram`.`flansch` (
`bestellt` INT NOT 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,
`flanschQ` VARCHAR(45) NOT NULL,
PRIMARY KEY (`bestellt`),
INDEX `fk_flansch_werkzeugführungen1_idx` (`flanschQ` ASC),
CONSTRAINT `fk_flansch_werkzeugführungen1`
FOREIGN KEY (`flanschQ`)
REFERENCES `lagerprogram`.`werkzeugführungen` (`flansch`)
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;
The column referenced in a foreign key needs to have an index on it. So you need to change the definition of the maschinen table to:
CREATE TABLE IF NOT EXISTS `lagerprogram`.`maschinen` (
`typ` VARCHAR(50) NOT NULL,
`werkzeugsatz` VARCHAR(45) 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 (`typ`, `werkzeugsatz`, `maschinenkörper`, `elektrik`, `pneumatic`, `hydraulik`, `kühlvorrichtung`, `vorschubeinheit`),
KEY (`werkzeugsatz`))
ENGINE = InnoDB;
Or you can change the order of the columns in the PRIMARY KEY so that werkzeugsatz is first:
PRIMARY KEY (`werkzeugsatz`, `typ`, `maschinenkörper`, `elektrik`, `pneumatic`, `hydraulik`, `kühlvorrichtung`, `vorschubeinheit`)

Creating a MySQL Table but get error

I get
errnno 150: InnoDB Documentation
Supports transactions, row-level locking, and foreign keys
Here is the SQL:
-- Table structure for table `Serving_Info`
--
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` varchar(128) NOT NULL,
Foreign key(`Oncall`) REFERENCES `employees`(`name`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
PRIMARY KEY (`Oncall`,`Food_name`, `Served_On`)
);
Any idea what is causing the error? I have tried making id part of the primary key but that isn't solving the problem either.
Here are the statements for the tables I am referencing:
CREATE TABLE IF NOT EXISTS `employees` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`email` varchar(64) NOT NULL,
`phone_number` varchar(16) NOT NULL,
`username` varchar(16) NOT NULL,
`password` varchar(32) NOT NULL,
`confirmcode` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id_user`)
);
--
-- Table structure for table `Foods`
--
CREATE TABLE IF NOT EXISTS `Foods` (
`Food_name` varchar(40) NOT NULL,
`CostPerRefill` double NOT NULL,
PRIMARY KEY (`Food_name`)
);
I just tried the below but I get the same error:
--
-- Table structure for table `Serving_Info`
--
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` varchar(128),
Foreign key(`Oncall`) REFERENCES `employees`(`name`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
PRIMARY KEY (`id`),
UNIQUE (`Oncall`,`Food_name`, `Served_On`)
);
Your problem is that the reference is to employees.name rather than employees.id_user. Try this:
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` int,
PRIMARY KEY (`id`),
Foreign key(`Oncall`) REFERENCES `employees`(`id_user`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
UNIQUE (`Oncall`,`Food_name`, `Served_On`)
);
Otherwise, change the employees table so name has an index:
CREATE TABLE IF NOT EXISTS `employees` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL ,
`email` varchar(64) NOT NULL,
`phone_number` varchar(16) NOT NULL,
`username` varchar(16) NOT NULL,
`password` varchar(32) NOT NULL,
`confirmcode` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id_user`),
KEY (name)
);
In practice, if you are going to use name this way, you should probably make that unique rather than just key.

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

ERROR 1215: Cannot add foreign key constraint when using ON DELETE SET NULL

I'm trying to create the following tables in MySQL. The first one:
CREATE TABLE IF NOT EXISTS address(
address_id INT NOT NULL AUTO_INCREMENT,
address_region VARCHAR(10) NOT NULL,
address_country VARCHAR(20) NOT NULL,
address_city VARCHAR(30) NOT NULL,
PRIMARY KEY(address_id))ENGINE = InnoDB;
I created it successfully,but when I try to create another table as following
CREATE TABLE IF NOT EXISTS spot(
spot_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
spot_address INT(11) NOT NULL,
spot_name VARCHAR(50) NOT NULL,
spot_desc VARCHAR(500) DEFAULT ' ',
spot_speadd VARCHAR(100) NOT NULL,
spot_viewtime INT DEFAULT 0,
FOREIGN KEY(spot_address)
REFERENCES address(address_id)
ON DELETE SET NULL
ON UPDATE SET NULL);
I get an error:
ERROR 1215 (HY000): Cannot add foreign key constraint
Why is this create table statement failing?
You have NOT NULL constraint on spot_address in the spot table and then try to set it to NULL on delete or update in the parent table address.
You can try this:
CREATE TABLE IF NOT EXISTS address
(
address_id INT NOT NULL AUTO_INCREMENT,
address_region VARCHAR(10) NOT NULL,
address_country VARCHAR(20) NOT NULL,
address_city VARCHAR(30) NOT NULL,
PRIMARY KEY(address_id)
)ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS `spot`
(
`spot_id` INT(11) NOT NULL AUTO_INCREMENT,
`spot_address` INT(11) NOT NULL,
`spot_name` VARCHAR(50) NOT NULL,
`spot_desc` VARCHAR(500) DEFAULT ' ',
`spot_speadd` VARCHAR(100) NOT NULL,
`spot_viewtime` INT(11) DEFAULT '0',
PRIMARY KEY (`spot_id`),
KEY `spot_address` (`spot_address`),
CONSTRAINT `spot_ibfk_1` FOREIGN KEY (`spot_address`) REFERENCES `address` (`address_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8