I created a Database with Mysql Qorkbench and i tried to Forward Engineer to Database but it completed with errors and this is the Message Log:
> Executing SQL script in server
ERROR: Error 1005: Can't create table `mydb`.`cds` (errno: 121 "Duplicate key on write or update")
SQL Code:
-- -----------------------------------------------------
-- Table `mydb`.`CDs`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`CDs` (
`CDid` INT NOT NULL AUTO_INCREMENT,
`Titel` VARCHAR(45) NOT NULL,
`Autor` VARCHAR(45) NOT NULL,
`Erscheinungsjahr` VARCHAR(45) NOT NULL,
`Genre` VARCHAR(45) NOT NULL,
`Stockwerk` VARCHAR(45) NOT NULL,
`Regal` INT NOT NULL,
`Ausgeborgt` INT NULL,
`Rezensionen` VARCHAR(600) NULL,
`Kurzbeschreibung` VARCHAR(600) NOT NULL,
PRIMARY KEY (`CDid`),
UNIQUE INDEX `CDid_UNIQUE` (`CDid` ASC),
INDEX `Buchungsid_idx` (`Ausgeborgt` ASC),
CONSTRAINT `Buchungsid`
FOREIGN KEY (`Ausgeborgt`)
REFERENCES `mydb`.`Buchung` (`Buchungsid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 9 succeeded, 1 failed
Fetching back view definitions in final form.
Could not get definition for mydb.view1 from server
1 views were read back.
This is the EER Diagram of my Database
A primary key is essentially an indexed unique not null constraint. You don't need to add an additional unique constraint, and indeed can't, as the error specifies. Remove the extra unique index clause and you should be OK:
CREATE TABLE IF NOT EXISTS `mydb`.`CDs` (
`CDid` INT NOT NULL AUTO_INCREMENT,
`Titel` VARCHAR(45) NOT NULL,
`Autor` VARCHAR(45) NOT NULL,
`Erscheinungsjahr` VARCHAR(45) NOT NULL,
`Genre` VARCHAR(45) NOT NULL,
`Stockwerk` VARCHAR(45) NOT NULL,
`Regal` INT NOT NULL,
`Ausgeborgt` INT NULL,
`Rezensionen` VARCHAR(600) NULL,
`Kurzbeschreibung` VARCHAR(600) NOT NULL,
PRIMARY KEY (`CDid`), -- no need for an extra index
INDEX `Buchungsid_idx` (`Ausgeborgt` ASC),
CONSTRAINT `Buchungsid`
FOREIGN KEY (`Ausgeborgt`)
REFERENCES `mydb`.`Buchung` (`Buchungsid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
Related
Im workin in mySQL workbench 8.0CE, i've created two tables, one for person and another one for persons direction, i'm trying to export data, but it throws and error
ERROR 1064 (42000) at line 67: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '
CONSTRAINT `fk_PersonaDireccion`
FOREIGN KEY (`idPersona`)
REFERENCES' at line 8
Operation failed with exitcode 1
This is the sql code
CREATE TABLE IF NOT EXISTS `dinSchema`.`Personas` (
`nombre` VARCHAR(20) NOT NULL,
`apellidoP` VARCHAR(20) NOT NULL,
`apellidoM` VARCHAR(20) NOT NULL,
`foto` MEDIUMBLOB NULL,
`fechaCaptura` TIMESTAMP(6) NOT NULL,
`escolaridad` VARCHAR(25) NOT NULL,
`carrera` VARCHAR(25) NULL,
`telefono` VARCHAR(10) NULL,
`correo` VARCHAR(50) NOT NULL,
`sexo` VARCHAR(10) NOT NULL,
`rfc` VARCHAR(13) NOT NULL,
`curp` VARCHAR(18) NOT NULL,
`observaciones` MEDIUMTEXT NULL,
`idPersonas` INT NOT NULL,
PRIMARY KEY (`idPersonas`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `dinSchema`.`direccion`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `dinSchema`.`direccion` ;
CREATE TABLE IF NOT EXISTS `dinSchema`.`direccion` (
`pais` VARCHAR(6) NOT NULL DEFAULT 'México',
`estado` VARCHAR(20) NOT NULL,
`ciudad` VARCHAR(25) NOT NULL,
`direccion` VARCHAR(150) NOT NULL,
`cp` INT(8) NOT NULL,
`idPersona` INT NOT NULL,
INDEX `fk_PersonaDireccion_idx` (`idPersona` ASC) VISIBLE,
CONSTRAINT `fk_PersonaDireccion`
FOREIGN KEY (`idPersona`)
REFERENCES `dinSchema`.`Personas` (`idPersonas`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB;
next i append EER Diagram img
Note: "Personas" id field is at last, beacuse i deleted a foreign key connection.
Somehow i fixed it, unchecked user default global settings in model options, and
INDEX `fk_PersonaDireccion_idx` (`idPersona` ASC) VISIBLE
disapeared, now "direccion" table got like this
CREATE TABLE IF NOT EXISTS `dinSchema`.`direccion` (
`pais` VARCHAR(6) NOT NULL DEFAULT 'México',
`estado` VARCHAR(20) NOT NULL,
`ciudad` VARCHAR(25) NOT NULL,
`direccion` VARCHAR(150) NOT NULL,
`cp` INT(8) NOT NULL,
`idPersona` INT NOT NULL,
CONSTRAINT `fk_PersonaDireccion`
FOREIGN KEY (`idPersona`)
REFERENCES `dinSchema`.`Personas` (`idPersonas`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB;
This question already has answers here:
MySQL Creating tables with Foreign Keys giving errno: 150
(20 answers)
Closed 4 years ago.
I am currently coding a booking system for a company and they also wanted a task managment system but I have encountered an error which is driving me insane.
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
-- -----------------------------------------------------
-- Table `bvsv_system`.`task`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bvsv_system`.`task` (
`idtask` INT(11) NOT NULL,
`attGora` VARCHAR(200) NULL,
`status` VARCHAR(45) NULL,
`to` VARCHAR(45) NULL,
`jobstatus_id` INT(11) NOT NULL,
`worker_personnummer` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idtask`, `jobstatus_id`, `worker_personnummer`),
INDEX `fk_task_jobstatus1_idx` (`jobstatus_id` ASC),
INDEX `fk_task_worker1_idx` (`worker_personnummer` ASC),
CONSTRAINT `fk_task_jobstatus1`
FOREIGN KEY (`jobstatus_id`)
REFERENCES `bvsv_system`.`jobstatus` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_task_worker1`
FOREIGN KEY (`worker_personnummer`)
REFERENCES `bvsv_system`.`worker` (`personnummer`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 15 succeeded, 1 failed
I get this error when i try to add a foreign key linking these two tables
CREATE TABLE IF NOT EXISTS `bvsv_system`.`worker` (
`personnummer` VARCHAR(45) NOT NULL,
`fornamn` VARCHAR(45) NULL DEFAULT NULL,
`efternamn` VARCHAR(45) NULL DEFAULT NULL,
`extraanstalld` VARCHAR(45) NOT NULL,
PRIMARY KEY (`personnummer`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
and
CREATE TABLE IF NOT EXISTS `bvsv_system`.`task` (
`idtask` INT(11) NOT NULL,
`attGora` VARCHAR(200) NULL,
`status` VARCHAR(45) NULL,
`to` VARCHAR(45) NULL,
`jobstatus_id` INT(11) NOT NULL,
`worker_personnummer` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idtask`, `jobstatus_id`, `worker_personnummer`),
INDEX `fk_task_jobstatus1_idx` (`jobstatus_id` ASC),
INDEX `fk_task_worker1_idx` (`worker_personnummer` ASC),
CONSTRAINT `fk_task_jobstatus1`
FOREIGN KEY (`jobstatus_id`)
REFERENCES `bvsv_system`.`jobstatus` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_task_worker1`
FOREIGN KEY (`worker_personnummer`)
REFERENCES `bvsv_system`.`worker` (`personnummer`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Help would be much appreciated my fellow coders! :)
The foreign key and primary key need to have the exact same definition. The worker table defines DEFAULT CHARACTER SET = latin1.
I'm not sure why you would want different character sets for different tables. I would recommend just using the database default.
If you do need them, then make the character sets compatible. You can do this at the column level:
CREATE TABLE IF NOT EXISTS `task` (
`idtask` INT(11) NOT NULL,
`attGora` VARCHAR(200) NULL,
`status` VARCHAR(45) NULL,
`to` VARCHAR(45) NULL,
`jobstatus_id` INT(11) NOT NULL,
`worker_personnummer` VARCHAR(45) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY (`idtask`, `jobstatus_id`, `worker_personnummer`),
INDEX `fk_task_jobstatus1_idx` (`jobstatus_id` ASC),
INDEX `fk_task_worker1_idx` (`worker_personnummer` ASC),
CONSTRAINT `fk_task_worker1`
FOREIGN KEY (`worker_personnummer`)
REFERENCES `worker` (`personnummer`)
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.
I need to create sql statments to import data from an existing MySQL database into MsAccess.
Has anyone got any ideas on the best way i can do this?
This is the code i used to create the database in MySQL just to give an idea of the tables etc.
CREATE DATABASE IF NOT EXISTS horsedb;
USE horsedb; CREATE TABLE `horse`.`horse` (
`HORSE_id` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NULL,
`Colour` VARCHAR(45) NULL,
`Sire` INT NULL,
`Dam` INT NULL,
`Born` YEAR NULL,
`Trainer_id` INT NOT NULL,
PRIMARY KEY (`HORSE_id`))
ENGINE = InnoDB;
USE horsedb; CREATE TABLE `horse`.`showsite` (
`show_id` INT NOT NULL AUTO INCREMENT,
`Name` VARCHAR(45) NULL,
`Address` VARCHAR(45) NULL,
PRIMARY KEY (`show_id`));
USE horsedb; CREATE TABLE `horse`.`judge` (
`Judge_id` INT NOT NULL AUTO INCREMENT,
`Name` VARCHAR(45) NULL,
`Address` VARCHAR(45) NULL,
PRIMARY KEY (`Judge_id`));
USE horsedb; CREATE TABLE `horse`.`event` (
`Event_id` INT NOT NULL AUTO INCREMENT,
`Show_id` INT NOT NULL,
`Event_name` VARCHAR(45) NOT NULL,
`Judge_id` INT NOT NULL,
PRIMARY KEY (`Event_id`),
INDEX `show_id_idx` (`Show_id` ASC),
INDEX `judge_id_idx` (`Judge_id` ASC),
CONSTRAINT `show_id`
FOREIGN KEY (`Show_id`)
REFERENCES `horsedb`.`showsite` (`Show_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `judge_id`
FOREIGN KEY (`Judge_id`)
REFERENCES `horsedb`.`judge` (`Judge_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
USE horsedb; CREATE TABLE `horse`.`entry` (
`Event_id` INT NOT NULL AUTO INCREMENT,
`horse-id` INT NOT NULL,
`Place` INT NULL,
INDEX `horse_id_idx` (`Horse_id` ASC),
CONSTRAINT `horse_id`
FOREIGN KEY (`Horse_id`)
REFERENCES `horsedb`.`horse` (`Horse_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
USE horsedb; CREATE TABLE `horse`.`prize` (
`Event_id` INT NOT NULL AUTO INCREMENT,
`place` INT NULL,
`money` INT NULL,
));
USE horsedb; CREATE TABLE `horse`.`trainer` (
`Trainer_id` INT NOT NULL AUTO INCREMENT,
`Name` VARCHAR(45) NULL,
PRIMARY KEY (`Trainer_id`));
The most direct method for transferring MySQL tables into Access would be to install the MySQL ODBC driver (MySQL Connector/ODBC) and then use an ODBC connection to Import (not Link) the tables from MySQL. For more detailed instructions see:
Using Connector/ODBC with Microsoft Access
I'm using MySQL workbench to design a database. Server is mysql 5.5.6
I've defined a few foreign keys linking the "candidates" table to the "countries" table. I get this error:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'customer.candidats' (errno: 150)
The thing is: i'm referencing twice the countries table: once for the "nationality" column, once for the user address's country of origin. Is that allowed? Is this the right way to do it?
Here is the generated code that seems to trigger the issue.
CREATE TABLE IF NOT EXISTS `customer`.`candidats` (
`id` INT NOT NULL AUTO_INCREMENT,
`nom` VARCHAR(40) NULL,
`prenom` VARCHAR(40) NULL,
`qualite` ENUM('0001','0002') NULL COMMENT '0001 = Madame\n0002 = Monsieur',
`sexe` SET('1','2') NULL COMMENT '1 = Femme\n2 = Homme',
`date_de_naissance` DATE NULL,
`Nationalite` INT NOT NULL,
`selor_bilinguisme` TINYINT(1) NULL,
`rue` VARCHAR(60) NULL,
`numero` VARCHAR(10) NULL,
`pays` INT NOT NULL,
`region` INT NOT NULL,
`localité` VARCHAR(40) NULL,
`code_postal` VARCHAR(10) NULL,
`email` VARCHAR(241) NULL,
`tel_domicile` VARCHAR(30) NULL,
`tel_bureau` VARCHAR(30) NULL,
`tel_mobile` VARCHAR(30) NULL,
`tel_prefere` ENUM('01','02','03') NULL DEFAULT '03',
PRIMARY KEY (`id`),
INDEX `fk_candidats_pays_idx` (`Nationalite` ASC, `pays` ASC),
INDEX `fk_candidats_régions1_idx` (`region` ASC),
CONSTRAINT `fk_candidats_pays`
FOREIGN KEY (`Nationalite` , `pays`)
REFERENCES `customer`.`pays` (`id` , `id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_candidats_régions1`
FOREIGN KEY (`region`)
REFERENCES `customer`.`régions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
The "pays" table ("countries" in French)
CREATE TABLE IF NOT EXISTS `customer`.`pays` (
`id` INT NOT NULL AUTO_INCREMENT,
`nom_fr` VARCHAR(45) NULL,
`nom_nl` VARCHAR(45) NULL,
`nationalite_fr` VARCHAR(45) NULL,
`nationalite_nl` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB
Your schema generator doesn't work correctly.
It should generate:
CONSTRAINT `fk_candidats_pays`
FOREIGN KEY (`pays`)
REFERENCES `customer`.`pays` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_candidats_Nationalite`
FOREIGN KEY (`Nationalite`)
REFERENCES `customer`.`pays` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
To your other question: This type of referencing seems strange when you see it the first time, but it's quite normal and I think there is no other way of constructing this type of relationship.