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.
Related
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
I tried to excute the query. but a error was occurred. the error message is 'Cannot add foreign key constraint'.
I supposed to create this table. but it didn't work.
CREATE TABLE IF NOT EXISTS `mydb`.`member` (
`idseq` INT(1) NOT NULL AUTO_INCREMENT,
`id` VARCHAR(50) NOT NULL,
`pw` VARCHAR(20) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`email` VARCHAR(45) NOT NULL,
`mobile0` VARCHAR(3) NOT NULL,
`mobile1` VARCHAR(3) NOT NULL,
`mobile2` VARCHAR(4) NOT NULL,
`mobile3` VARCHAR(4) NOT NULL,
`birth` DATE NOT NULL,
`admin_YN` VARCHAR(45) NOT NULL DEFAULT 'N',
`reg_date` DATE NOT NULL,
`upd_date` DATE NOT NULL,
PRIMARY KEY (`idseq`, `id`))
ENGINE = InnoDB
AUTO_INCREMENT = 6
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `mydb`.`reservation` (
`reservation_seq` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` VARCHAR(50) NOT NULL,
`playMv_seq` INT(11) NOT NULL,
`reservaion_seat_code` VARCHAR(20) NOT NULL,
`reservaion_seat_num` INT(11) NOT NULL,
`reservation_charge` VARCHAR(20) NOT NULL,
`reservation_date` DATETIME NOT NULL,
PRIMARY KEY (`reservation_seq`, `user_id`, `playMv_seq`),
FOREIGN KEY (`user_id`)
REFERENCES `mydb`.`member` (`id`)
)
ENGINE = InnoDB
AUTO_INCREMENT = 8
DEFAULT CHARACTER SET = utf8;
So I checked it detail from a query'show engine innodb status. the detail error message is
Error in foreign key constraint of table mydb/reservation:
FOREIGN KEY (`user_id`)
REFERENCES `mydb`.`member` (`id`)
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
how can I resolve the error
You're trying to create a foreign key referencing the id column of the target table:
FOREIGN KEY (`user_id`)
REFERENCES `mydb`.`member` (`id`)
But what is the actual key on that target table?:
PRIMARY KEY (`idseq`, `id`)
It's not id, if the composite of idseq and id. In order to reference that as a foreign key, you need local columns which match that:
`user_idseq` INT(1) NOT NULL,
`user_id` VARCHAR(50) NOT NULL,
And use both of them for the foreign key:
FOREIGN KEY (`user_idseq`, `user_id`)
REFERENCES `mydb`.`member` (`idseq`, `id`)
(Side Note: That primary key in member looks pretty strange to me in the first place. Why can't idseq by itself be the primary key? What is the purpose of id?)
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 ....
I'm going crazy trying to figure out the reason of the well-known ERROR 1215 (MySQL 6.3.5).
I have to declare a table with a four-column PK, in which 3 of the columns are FK referencing other table.
CREATE TABLE `zen`.`pago_abono` (
`fecha_pago` DATETIME NOT NULL,
`cod_abono` VARCHAR(4) NOT NULL,
`mes` INT(2) NOT NULL,
`anio` YEAR NOT NULL,
`monto_pago` DECIMAL(10,2) NULL,
PRIMARY KEY (`fecha_pago`, `cod_abono`, `mes`, `anio`),
INDEX `cuota_fk_idx` (`mes` ASC, `anio` ASC, `cod_abono` ASC),
CONSTRAINT `cuota_fk`
FOREIGN KEY (`mes` , `anio` , `cod_abono`)
REFERENCES `zen`.`cuota_abono` (`mes` , `anio` , `cod_abono`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
"cuota_fk" references the table "cuota_abono"
CREATE TABLE `cuota_abono` (
`cod_abono` varchar(4) NOT NULL,
`mes` int(2) NOT NULL,
`anio` year(4) NOT NULL,
`valor` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`cod_abono`,`anio`,`mes`),
CONSTRAINT `cod_abono_fk` FOREIGN KEY (`cod_abono`) REFERENCES `abonos`(`cod_abono`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Finally, "cuota_abono" has a FK, "cod_abono_fk", referencing "abonos"
CREATE TABLE `abonos` (
`cod_abono` varchar(4) NOT NULL,
`direccion` varchar(45) NOT NULL,
`tel_verificacion` varchar(45) NOT NULL,
`fecha_alta` date DEFAULT NULL,
`descripcion` varchar(45) DEFAULT NULL,
`cuenta_cte` decimal(10,2) DEFAULT '0.00',
`cod_localidad` varchar(3) DEFAULT NULL,
`cod_cobrador` varchar(3) DEFAULT NULL,
`nombre_servicio` varchar(45) DEFAULT NULL,
PRIMARY KEY (`cod_abono`),
KEY `zona_cobranza_fk_idx` (`cod_localidad`,`cod_cobrador`),
CONSTRAINT `zona_cobranza_fk` FOREIGN KEY (`cod_localidad`, `cod_cobrador`) REFERENCES `zonas_cobranza` (`cod_localidad`, `cod_cobrador`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Can you find the mistake?
Thanks!
P.S.: All tables have already been created, except from "pago_abono". I'm showing the CREATE statement of tables "abonos" and "cuota_abono" just to let you know their structure.
I even tried creating "pago_abono" just with the PK and then used ALTER to add the FKs, but it still shows the error.
CREATE TABLE `ROUTE` (
`route_ID` INT NOT NULL,
`route_name` VARCHAR(45) NOT NULL,
`DELIVERY_VEHICLE_veh_ID` INT NOT NULL,
`DELIVERY_DRIVER_dr_ID` INT NOT NULL,
PRIMARY KEY (`route_ID`),
CONSTRAINT `fk_ROUTE_DELIVERY1`
FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID` , `DELIVERY_DRIVER_dr_ID`)
(`VEHICLE_veh_ID` , `DRIVER_dr_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
I'm new to SQL and trying to learn by practicing. There's an error in my syntax that I don't understand. Any help/suggestions?
(VEHICLE_veh_ID , DRIVER_dr_ID) at line 9
CREATE TABLE `DRIVER` (
`dr_ID` INT NOT NULL,
`dr_title` VARCHAR(15) NOT NULL,
`dr_fname` VARCHAR(45) NOT NULL,
`dr_lname` VARCHAR(45) NOT NULL,
`dr_DOB` DATETIME NOT NULL,
`dr_licenceNo` VARCHAR(45) NOT NULL,
`dr_phone` VARCHAR(15) NOT NULL,
`dr_email` VARCHAR(45) NOT NULL,
PRIMARY KEY (`dr_ID`));
CREATE TABLE `VEHICLE` (
`veh_ID` INT NOT NULL,
`veh_reg` VARCHAR(15) NOT NULL,
`veh_make` VARCHAR(45) NOT NULL,
`veh_model` VARCHAR(45) NOT NULL,
`veh_mileage` INT NOT NULL,
`veh_MOTdate` DATETIME NOT NULL,
`veh_servicedate` DATETIME NOT NULL,
PRIMARY KEY (`veh_ID`));
These are the other tables ROUTE is related to. I had no issues inserting DRIVER and VEHICLE tables into the DB, what's wrong with ROUTE?
It looks like there are two foreign keys in the ROUTE table.
One of them references the DRIVER table.
The other references the VEHICLE table.
Your foreign key constraint definitions should look more like this:
, CONSTRAINT `fk_ROUTE_DELIVERY_DRIVER`
FOREIGN KEY (`DELIVERY_DRIVER_dr_ID`)
REFERENCES `DRIVER` (`dr_ID`)
ON DELETE RESTRICT ON UPDATE RESTRICT
, CONSTRAINT `fk_ROUTE_DELIVERY_VEHICLE`
FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID`)
REFERENCES `VEHICLE` (`veh_ID`)
ON DELETE RESTRICT ON UPDATE RESTRICT
CREATE TABLE `ROUTE` (
`route_ID` INT NOT NULL,
`route_name` VARCHAR(45) NOT NULL,
`DELIVERY_VEHICLE_veh_ID` INT NOT NULL,
`DELIVERY_DRIVER_dr_ID` INT NOT NULL,
PRIMARY KEY (`route_ID`),
CONSTRAINT `fk_ROUTE_VEHICLE`
FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID`)
REFERENCES `VEHICLE`(`veh_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_ROUTE_DRIVER`
FOREIGN KEY (`DELIVERY_DRIVER_dr_ID`)
REFERENCES `DRIVER`(`dr_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);