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`)
Related
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;
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.
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
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.