MySQL workbench data importing, foreign key error - mysql

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;

Related

MySql CONSTRAINT issues

Fetching back view definitions in final form.
Nothing to fetch
Executing SQL script in server results in:
ERROR: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT `fk_employee_department`
FOREIGN KEY (`dpt_id`)
REFERENCES `r' at line 16
SQL Code:
CREATE TABLE IF NOT EXISTS `rrm17b`.`employee` (
`emp_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`dpt_id` INT UNSIGNED NULL,
`emp_first` VARCHAR(20) NOT NULL,
`emp_last` VARCHAR(20) NOT NULL,
`emp_type` ENUM('f', 'p') NOT NULL,
`emp_street` VARCHAR(30) NOT NULL,
`emp_city` VARCHAR(20) NOT NULL,
`emp_state` CHAR(2) NOT NULL,
`emp_zip` INT UNSIGNED NOT NULL,
`emp_phone` BIGINT UNSIGNED NOT NULL,
`emp_email` VARCHAR(45) NOT NULL,
`emp_notes` VARCHAR(100) NULL,
PRIMARY KEY (`emp_id`),
INDEX `fk_employee_department_idx` (`dpt_id` ASC)
CONSTRAINT `fk_employee_department`
FOREIGN KEY (`dpt_id`)
REFERENCES `rrm17b`.`department` (`dpt_id`)
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.
Nothing to fetch
Please try this:
CREATE TABLE IF NOT EXISTS rrm17b.employee(
emp_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
dpt_id INT UNSIGNED NULL,
emp_first VARCHAR(20) NOT NULL,
emp_last VARCHAR(20) NOT NULL,
emp_type ENUM('f', 'p') NOT NULL,
emp_street VARCHAR(30) NOT NULL,
emp_city VARCHAR(20) NOT NULL,
emp_state CHAR(2) NOT NULL,
emp_zip INT UNSIGNED NOT NULL,
emp_phone BIGINT UNSIGNED NOT NULL,
emp_email VARCHAR(45) NOT NULL,
emp_notes VARCHAR(100) NULL,
PRIMARY KEY (emp_id),
INDEX fk_employee_department_idx(dpt_id ASC),
CONSTRAINT fk_employee_department
FOREIGN KEY (dpt_id)
REFERENCES rrm17b.department(dpt_id) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB
Fixed multiple things:
--Added comma after INDEX
--Removed "`" after dpt_id

MySQL: Why is Foreign Key failing?

Trying to insert a row into MySQL using phpMyAdmin, and it is failing my with error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`staging`.`user_profiles`, CONSTRAINT `fk_user_profiles_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Can't figure out why. I created the database design in MySQL Workbench and it was working on MySQL 5.7 in development and now won't work with Percona server 5.5. Where am I NOT looking??
SQL Statement and Error
Foriegn key in phpMyAdmin
This is the table structure I created in MySQL Workbench (dashes for obscuring client info =P):
DROP TABLE IF EXISTS `staging_-----------`.`users` ;
CREATE TABLE IF NOT EXISTS `staging_-----------`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(128) NOT NULL,
`role` ENUM('admin', 'vet', 'client') NOT NULL,
PRIMARY KEY (`id`));
DROP TABLE IF EXISTS `staging_-----------`.`user_profiles` ;
CREATE TABLE IF NOT EXISTS `staging_-----------`.`user_profiles` (
`user_id` INT NOT NULL,
`address_one` VARCHAR(255) NULL,
`address_two` VARCHAR(255) NULL,
`age` INT NULL,
`sex` ENUM('m', 'f') NULL,
`first_name` VARCHAR(45) NULL,
`last_name` VARCHAR(45) NULL,
`city` VARCHAR(45) NULL,
`state` VARCHAR(45) NULL,
`zip` VARCHAR(6) NULL,
`phone` VARCHAR(45) NULL,
`photo` VARCHAR(255) NULL,
PRIMARY KEY (`user_id`),
INDEX `fk_user_profiles_users2_idx` (`user_id` ASC),
CONSTRAINT `fk_user_profiles_users`
FOREIGN KEY (`user_id`)
REFERENCES `staging_-----------`.`users` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
I've been trying to get my working code working again for 2 days now, and no love. What could have gone wrong? Thank you so much for your time.
Check if the value you are inserting exists in user_id exists in staging_-----------.users
Maybe this is the problem ....

Error Code 1215. Cannot add foreign key constraint

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

MySQL Workbench Database error

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

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`)