Related
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.
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
I need a fresh pair of eyes to see what exactly I am doing wrong here.
CREATE TABLE IF NOT EXISTS `spring_normalize`.`users` (
`username` VARCHAR(60) NOT NULL,
`password` VARCHAR(80) NULL,
`authority` VARCHAR(45) NULL,
`name` VARCHAR(100) NULL,
`enabled` TINYINT(1) NULL,
`email` VARCHAR(60) NULL,
PRIMARY KEY (`username`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `spring_normalize`.`offers` (
`id` INT NOT NULL AUTO_INCREMENT,
`text` VARCHAR(100) NULL,
`users_username` VARCHAR(60) NOT NULL,
PRIMARY KEY (`id`, `users_username`),
INDEX `fk_offers_users_idx` (`users_username` ASC),
CONSTRAINT `fk_offers_users`
FOREIGN KEY (`users_username`)
REFERENCES `spring_normalize`.`users` (`username`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
From other people who has the same problem:
Is Db InnoDB? Yes
Are all tables InnoDB ? Yes
Is unique index present on referencing table ? Yes
Are referenced and referencing column exactly of the same type ? Yes
Question what do am I doing wrong? Thanks in advance!
As the others stated in comments, your queries are correct
Couple of things you can try :
Select your database first and remove it from your 2 create table
USE `spring_normalize`;
CREATE TABLE IF NOT EXISTS `users` (
`username` VARCHAR(60) NOT NULL,
`password` VARCHAR(80) NULL,
`authority` VARCHAR(45) NULL,
`name` VARCHAR(100) NULL,
`enabled` TINYINT(1) NULL,
`email` VARCHAR(60) NULL,
PRIMARY KEY (`username`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `offers` (
`id` INT NOT NULL AUTO_INCREMENT,
`text` VARCHAR(100) NULL,
`users_username` VARCHAR(60) NOT NULL,
PRIMARY KEY (`id`, `users_username`),
INDEX `fk_offers_users_idx` (`users_username` ASC),
CONSTRAINT `fk_offers_users`
FOREIGN KEY (`users_username`)
REFERENCES `users` (`username`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Try to specify this before the create tables in case the engine would (weirdly) try to create the table offers before users:
-- Do not check foreign key constraints
SET FOREIGN_KEY_CHECKS = 0;
and finally if nothing solves it, do this after having received the error, it will give you more info
SHOW ENGINE INNODB STATUS;
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`)
Been looking at this for two days and am at a loss as to what this means. I know it means "exactly what it says" but I don't know how to fix it. Can someone explain it to me in layman's terms and help me out?
ERROR: Error 1452: Cannot add or update a child row: a foreign key constraint fails
(`sls11n`.`dependent`, CONSTRAINT `fk_dependent_employee1` FOREIGN KEY (`emp_id`)
REFERENCES `employee` (`emp_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
INSERT INTO `sls11n`.`dependent` (`dep_id`, `emp_id`, `dep_ssn`, `dep_fname`, `dep_lname`,
`dep_street`, `dep_city`, `dep_state`, `dep_zip`, `dep_phone`, `dep_email`, `dep_notes`)
VALUES (NULL, 13, 123456789, 'Gary', 'Hart', 'West St', 'San Diego', 'CA', '23424',
'1234567890', 'garyhart#me.com', NULL)
Here's the script that I believe is relevant:
-- -----------------------------------------------------
-- Table `sls11n`.`employee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `sls11n`.`employee` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `sls11n`.`employee` (
`emp_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`emp_ssn` INT UNSIGNED NOT NULL,
`emp_fname` VARCHAR(15) NOT NULL,
`emp_lname` VARCHAR(20) NOT NULL,
`emp_street` VARCHAR(45) NOT NULL,
`emp_city` VARCHAR(45) NOT NULL,
`emp_state` CHAR(2) NOT NULL,
`emp_zip` CHAR(9) NOT NULL,
`emp_phone` CHAR(15) NOT NULL,
`emp_email` VARCHAR(100) NOT NULL,
`emp_doh` DATE NOT NULL,
`emp_is_inspect` ENUM('y', 'n') NOT NULL,
`emp_notes` VARCHAR(250) NULL,
PRIMARY KEY (`emp_id`))
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `sls11n`.`dependent`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `sls11n`.`dependent` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `sls11n`.`dependent` (
`dep_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`emp_id` TINYINT UNSIGNED NOT NULL,
`dep_ssn` INT UNSIGNED NOT NULL,
`dep_fname` VARCHAR(45) NOT NULL,
`dep_lname` VARCHAR(45) NOT NULL,
`dep_street` VARCHAR(45) NOT NULL,
`dep_city` VARCHAR(45) NOT NULL,
`dep_state` CHAR(2) NOT NULL,
`dep_zip` CHAR(9) NOT NULL,
`dep_phone` CHAR(10) NOT NULL,
`dep_email` VARCHAR(100) NOT NULL,
`dep_notes` VARCHAR(250) NULL,
PRIMARY KEY (`dep_id`),
INDEX `fk_dependent_employee1_idx` (`emp_id` ASC),
CONSTRAINT `fk_dependent_employee1`
FOREIGN KEY (`emp_id`)
REFERENCES `sls11n`.`employee` (`emp_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Any one have any ideas? Please use simple terms, I'm not kidding when I say I don't understand.
The foreign key constraint says that each dependent's emp_id must match an existing emp_id in the employee table. So before you add this row to the dependent table, you have to add a row to employee with emp_id = 13.