INSERT ON DUPLICATE KEY with INNER JOIN - mysql

Given t.id, a.id, t1.name and t2.name, how do I add or update t1_has_t2.data?
I can update it if there is currently a record.
UPDATE t1_has_t2
INNER JOIN t1 ON t1.id=t1_has_t2.t1_id
INNER JOIN t2 ON t2.id=t1_has_t2.t2_id
SET t1_has_t2.data=123
WHERE t1.name="foo" AND t1.t_id=333 AND t2.name="bar" AND t2.t_id=333;
How can I insert it if a record currently doesn't exist?
EDIT. Would it be something like the following? Seems a waste to include t in the JOIN.
INSERT INTO t1_has_t2(t1_id,t2_id,data)
SELECT t1.id, t2.id, 123
FROM t
INNER JOIN t1 ON t1.t_id=t.id
INNER JOIN t2 ON t2.t_id=t.id
WHERE t1.name="foo" AND t1.t_id=333 AND t2.name="bar" AND t2.t_id=333
ON DUPLICATE KEY SET t1_has_t2.data=123;
EDIT2. Ah, maybe I get it now. I just JOIN t1 and t2 to each other through their shared t.id?
INSERT INTO t1_has_t2(t1_id,t2_id,data)
SELECT t1.id, t2.id, 123
FROM t1
INNER JOIN t2 ON t2.t_id=t1.t_id
WHERE t1.name="foo" AND t1.t_id=333 AND t2.name="bar" AND t2.t_id=333
ON DUPLICATE KEY UPDATE t1_has_t2.data=123;
-- MySQL Script generated by MySQL Workbench
-- 08/08/16 07:40:04
-- Model: New Model Version: 1.0
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 mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`accounts`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`accounts` (
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`t`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t` (
`id` INT NOT NULL AUTO_INCREMENT,
`accounts_id` INT NOT NULL,
PRIMARY KEY (`id`, `accounts_id`),
INDEX `fk_t_accounts_idx` (`accounts_id` ASC),
CONSTRAINT `fk_t_accounts`
FOREIGN KEY (`accounts_id`)
REFERENCES `mydb`.`accounts` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`t1`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t1` (
`id` INT NOT NULL AUTO_INCREMENT,
`t_id` INT NOT NULL,
`t_accounts_id` INT NOT NULL,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
INDEX `fk_t1_t1_idx` (`t_id` ASC, `t_accounts_id` ASC),
UNIQUE INDEX `un1` (`t_id` ASC, `name` ASC),
CONSTRAINT `fk_t1_t1`
FOREIGN KEY (`t_id` , `t_accounts_id`)
REFERENCES `mydb`.`t` (`id` , `accounts_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`t2`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t2` (
`id` INT NOT NULL AUTO_INCREMENT,
`t_id` INT NOT NULL,
`t_accounts_id` INT NOT NULL,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
INDEX `fk_t2_t1_idx` (`t_id` ASC, `t_accounts_id` ASC),
UNIQUE INDEX `un2` (`t_id` ASC, `name` ASC),
CONSTRAINT `fk_t2_t1`
FOREIGN KEY (`t_id` , `t_accounts_id`)
REFERENCES `mydb`.`t` (`id` , `accounts_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`t1_has_t2`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t1_has_t2` (
`t1_id` INT NOT NULL,
`t2_id` INT NOT NULL,
`data` VARCHAR(45) NULL,
PRIMARY KEY (`t1_id`, `t2_id`),
INDEX `fk_t1_has_t2_t21_idx` (`t2_id` ASC),
INDEX `fk_t1_has_t2_t11_idx` (`t1_id` ASC),
CONSTRAINT `fk_t1_has_t2_t11`
FOREIGN KEY (`t1_id`)
REFERENCES `mydb`.`t1` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_t1_has_t2_t21`
FOREIGN KEY (`t2_id`)
REFERENCES `mydb`.`t2` (`id`)
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;

Works fine if I understand what you want with Insert on Duplicate Key Update (IODKU).
Data Load:
insert accounts(id) values (NULL); -- id = 1
insert t(accounts_id) values (1); -- id = 1
insert t1(t_id,t_accounts_id,name) values (1,1,'n1'); -- id=1
insert t1(t_id,t_accounts_id,name) values (1,1,'n2'); -- id=2
insert t2(t_id,t_accounts_id,name) values (1,1,'n1'); -- id=1
insert t2(t_id,t_accounts_id,name) values (1,1,'n2'); -- id=2
insert t1_has_t2(t1_id,t2_id,data) values(1,1,'one_one'); -- success
insert t1_has_t2(t1_id,t2_id,data) values(1,77,'x'); -- Error 1452 as expected
insert t1_has_t2(t1_id,t2_id,data) values(77,1,'x'); -- Error 1452 as expected
insert t1_has_t2(t1_id,t2_id,data) values(1,2,'one_two'); -- success
insert t1_has_t2(t1_id,t2_id,data) values(2,1,'two_one'); -- success
insert t1_has_t2(t1_id,t2_id,data) values(2,2,'two_two'); -- success
Your Query:
UPDATE t1_has_t2
INNER JOIN t1 ON t1.id=t1_has_t2.t1_id
INNER JOIN t2 ON t2.id=t1_has_t2.t2_id
SET t1_has_t2.data='I am a string'
WHERE t1.name="n1" AND t1.t_id=1 AND t2.name="n1" AND t2.t_id=1;
IODKU:
insert t1_has_t2(t1_id,t2_id,data) values(2,2,'two_two_version002')
on duplicate key update data='anchovies';
See results:
select * from t1_has_t2;
+-------+-------+---------------+
| t1_id | t2_id | data |
+-------+-------+---------------+
| 1 | 1 | I am a string |
| 1 | 2 | one_two |
| 2 | 1 | two_one |
| 2 | 2 | anchovies |
+-------+-------+---------------+
You can also look into
insert ignore t1_has_t2(t1_id,t2_id,data) [something];
Which succeeds or fails silently by design.

Related

Delete with inner join and trigger throw error: Can't update table 'table_b' in stored function/trigger

Why the following error (Can't update table 'table_b' in stored function/trigger because it is already used by statement which invoked this stored function/trigger) appears after i try to delete with inner join?
Can i solve it?
DROP TABLE if exists table_b;
DROP TABLE if exists table_a;
CREATE TABLE table_a (
id int auto_increment,
name varchar(255) DEFAULT NULL,
primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
DROP TABLE if exists table_b;
CREATE TABLE table_b (
id int auto_increment,
name varchar(255) DEFAULT NULL,
id_table_a int NOT null,
another_table_id int NOT null,
foreign key (id_table_a) references table_a(id),
primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
DELIMITER $$
drop trigger if exists tg_test$$
create trigger tg_test before delete on table_a for each row
begin
delete from table_b where id_table_a = OLD.id;
end$$
DELIMITER ;
insert into table_a(name) values('t-a');
insert into table_b(name, id_table_a, another_table_id) values('t-b', 1, 23);
-- Error Can't update table 'table_b' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
-- in any of this two lines
delete table_a from table_a inner join table_b on table_b.id_table_a = table_a.id where another_table_id = 23;
delete from table_a where id in (select id_table_a from table_b where another_table_id = 23);
-- Success
delete from table_a where id = 1;
I don't see the point for a trigger here. The functionality you want can be achieved by just adding the on delete cascade option to the declaration of your foreign key:
CREATE TABLE table_b (
id int auto_increment,
name varchar(255) DEFAULT NULL,
id_table_a int NOT null,
another_table_id int NOT null,
foreign key (id_table_a) references table_a(id) on delete cascade,
primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Now anytime a record is dropped from table_a, the children record (as designated by the foreign key definition) are deleted from table_b.
With this set-up (and without the trigger), both your delete statements would run fine. I would actually use exists rather than a join or in, but that's mostly a matter of taste:
delete from table_a
where exists(
select 1
from table_b b
where b.id_table_a = table_a.id and b.another_table_id = 23
);

how to join tables with generalization

I'm having trouble performing a JOIN on tables the problem is this:
In a report cart system I have users, such as students, parents and school employees. I need to generate an SQL statement that when I enter the access ID of the parents it lists all students related to parents ID
Follow the Model:
Is this the best way to implement this "Generalization" and this relationship between parents and students, since they are all users? Can someone help me?
SQL code:
-- -----------------------------------------------------
-- Table `testeboletim`.`type_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testeboletim`.`type_user` (
`idtype_user` INT NOT NULL AUTO_INCREMENT,
`role` VARCHAR(45) NULL,
PRIMARY KEY (`idtype_user`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testeboletim`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testeboletim`.`user` (
`iduser` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`ID` VARCHAR(20) NULL,
`birth` DATE NULL,
`telephone` VARCHAR(20) NULL,
`phone` VARCHAR(20) NULL,
`email` VARCHAR(45) NULL,
`type_user_idtype_user` INT NOT NULL,
PRIMARY KEY (`iduser`, `type_user_idtype_user`),
INDEX `fk_usuario_tipo_usuario_idx` (`type_user_idtype_user` ASC),
CONSTRAINT `fk_usuario_tipo_usuario`
FOREIGN KEY (`type_user_idtype_user`)
REFERENCES `testeboletim`.`type_user` (`idtype_user`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testeboletim`.`student`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testeboletim`.`student` (
`idstudent` INT NOT NULL AUTO_INCREMENT,
`user_iduser` INT NOT NULL,
`user_type_user_idtype_user` INT NOT NULL,
PRIMARY KEY (`idstudent`, `user_iduser`, `user_type_user_idtype_user`),
INDEX `fk_aluno_usuario1_idx` (`user_iduser` ASC, `user_type_user_idtype_user` ASC),
CONSTRAINT `fk_aluno_usuario1`
FOREIGN KEY (`user_iduser` , `user_type_user_idtype_user`)
REFERENCES `testeboletim`.`user` (`iduser` , `type_user_idtype_user`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testeboletim`.`parents`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testeboletim`.`parents` (
`idparents` INT NOT NULL AUTO_INCREMENT,
`user_iduser` INT NOT NULL,
`user_type_user_idtype_user` INT NOT NULL,
PRIMARY KEY (`idparents`, `user_iduser`, `user_type_user_idtype_user`),
INDEX `fk_responsavel_usuario1_idx` (`user_iduser` ASC, `user_type_user_idtype_user` ASC),
CONSTRAINT `fk_responsavel_usuario1`
FOREIGN KEY (`user_iduser` , `user_type_user_idtype_user`)
REFERENCES `testeboletim`.`user` (`iduser` , `type_user_idtype_user`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testeboletim`.`student_has_parents`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testeboletim`.`student_has_parents` (
`student_idstudent` INT NOT NULL,
`student_user_iduser` INT NOT NULL,
`parents_idparents` INT NOT NULL,
`parents_user_iduser` INT NOT NULL,
PRIMARY KEY (`student_idstudent`, `student_user_iduser`, `parents_idparents`, `parents_user_iduser`),
INDEX `fk_aluno_has_responsavel_responsavel1_idx` (`parents_idparents` ASC, `parents_user_iduser` ASC),
INDEX `fk_aluno_has_responsavel_aluno1_idx` (`student_idstudent` ASC, `student_user_iduser` ASC),
CONSTRAINT `fk_aluno_has_responsavel_aluno1`
FOREIGN KEY (`student_idstudent` , `student_user_iduser`)
REFERENCES `testeboletim`.`student` (`idstudent` , `user_iduser`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_aluno_has_responsavel_responsavel1`
FOREIGN KEY (`parents_idparents` , `parents_user_iduser`)
REFERENCES `testeboletim`.`parents` (`idparents` , `user_iduser`)
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;
In order to solve this problem, we would need to look at testeboletim.student, testeboletim.parents, and either testeboletim.student_has_parents or testeboletim.user. I have decided to solve your problem using testeboletim.user, because it was clearer in terms of refrence keys, and what not.
Solution using testeboletim.user
Based on your question, we are looking for all rows in testeboletim.student, that have a corresponding iduser testeboletim.user based on the user_iduser of testeboletim.parents.
-- SQL Definition:
SELECT * FROM `testeboletim`.`student` WHERE `user_iduser` IN
(SELECT DISTINCT(`iduser`) FROM `testeboletim`.`user` WHERE `iduser` IN
(SELECT DISTINCT(`user_iduser`) FROM `testeboletim`.`parents`)
);
Now to do the same thing with JOIN, would require the use of LEFT JOIN; in this case testeboletim.student.
SELECT * FROM `testeboletim`.`student` AS `student`
LEFT JOIN `testeboletim`.`user` AS `user`
ON `student`.`user_iduser` = `user`.`iduser`
LEFT JOIN `testeboletim`.`parents` AS `parents`
ON `user`.`iduser` = `parents`.`user_iduser`;
Since I don't have any values, I'm going to share with you the explanation, in order to "prove" that the query works.
mysql> SELECT * FROM `testeboletim`.`student` AS `student`
-> LEFT JOIN `testeboletim`.`user` AS `user`
-> ON `student`.`user_iduser` = `user`.`iduser`
-> LEFT JOIN `testeboletim`.`parents` AS `parents`
-> ON `user`.`iduser` = `parents`.`user_iduser`;
Empty set (0.01 sec)
mysql> EXPLAIN SELECT * FROM `testeboletim`.`student` AS `student`
-> LEFT JOIN `testeboletim`.`user` AS `user`
-> ON `student`.`user_iduser` = `user`.`iduser`
-> LEFT JOIN `testeboletim`.`parents` AS `parents`
-> ON `user`.`iduser` = `parents`.`user_iduser`;
+------+-------------+---------+-------+-----------------------------+-----------------------------+---------+--------------------------+------+-------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------+-------+-----------------------------+-----------------------------+---------+--------------------------+------+-------------------------------------------------+
| 1 | SIMPLE | student | index | NULL | PRIMARY | 12 | NULL | 1 | Using index |
| 1 | SIMPLE | user | ALL | PRIMARY | NULL | NULL | NULL | 1 | Using where; Using join buffer (flat, BNL join) |
| 1 | SIMPLE | parents | ref | fk_responsavel_usuario1_idx | fk_responsavel_usuario1_idx | 4 | testeboletim.user.iduser | 1 | Using where; Using index |
+------+-------------+---------+-------+-----------------------------+-----------------------------+---------+--------------------------+------+-------------------------------------------------+
3 rows in set (0.00 sec)

groups and sub-group up to 5 level database structure

i was wondering what is the best structure for a table in mysql,
The structure needs to have:
Parent (level 0)
-region (inside regions are)(level 1)
-countrys (inside countrys are)(level 2)
-Districts (level 3)
So I need to store all that info but in the same table, any clues??
If you need to know i building this app on cakephp.
Thanks in advance.
I've impulsively made an EER diagram for your situation.
Please tell me if it works (and if you agree), I'll be awaiting your feedback.
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';
DROP SCHEMA IF EXISTS `hierarchy` ;
CREATE SCHEMA IF NOT EXISTS `hierarchy` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `hierarchy` ;
-- -----------------------------------------------------
-- Table `hierarchy`.`level`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `hierarchy`.`level` (
`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(20) NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `name_UNIQUE` (`name` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `hierarchy`.`location`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `hierarchy`.`location` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`parent` INT UNSIGNED NULL ,
`name` VARCHAR(50) NOT NULL ,
`level` TINYINT UNSIGNED NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_location_location_idx` (`parent` ASC) ,
INDEX `fk_location_level1_idx` (`level` ASC) ,
INDEX `index_name` (`name` ASC) ,
CONSTRAINT `fk_location_location`
FOREIGN KEY (`parent` )
REFERENCES `hierarchy`.`location` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_location_level1`
FOREIGN KEY (`level` )
REFERENCES `hierarchy`.`level` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
USE `hierarchy` ;
-- -----------------------------------------------------
-- Placeholder table for view `hierarchy`.`details_location`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `hierarchy`.`details_location` (`location_id` INT, `location_name` INT, `parent_id` INT, `level_id` INT, `level_name` INT, `children` INT);
-- -----------------------------------------------------
-- View `hierarchy`.`details_location`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `hierarchy`.`details_location`;
USE `hierarchy`;
CREATE OR REPLACE VIEW `hierarchy`.`details_location` AS
SELECT
x.`id` AS `location_id`,
x.`name` AS `location_name`,
IFNULL(x.`parent`, 0) AS `parent_id`,
y.`id` AS `level_id`,
y.`name` AS `level_name`,
(SELECT COUNT(*) FROM location AS l WHERE l.parent = x.id) AS `children`
FROM location AS `x`
INNER JOIN `level` AS `y`
ON (x.`level` = y.id);
USE `hierarchy`;
DELIMITER $$
USE `hierarchy`$$
CREATE TRIGGER `insert_location` BEFORE INSERT ON location
FOR EACH ROW BEGIN
DECLARE x INT UNSIGNED DEFAULT 1;
IF NEW.parent IS NOT NULL THEN
SELECT `level`+1 INTO x FROM location WHERE id = NEW.parent;
END IF;
SET NEW.`level` = x;
END$$
DELIMITER ;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
-- -----------------------------------------------------
-- Data for table `hierarchy`.`level`
-- -----------------------------------------------------
START TRANSACTION;
USE `hierarchy`;
INSERT INTO `hierarchy`.`level` (`id`, `name`) VALUES (1, 'parent');
INSERT INTO `hierarchy`.`level` (`id`, `name`) VALUES (2, 'region');
INSERT INTO `hierarchy`.`level` (`id`, `name`) VALUES (3, 'country');
INSERT INTO `hierarchy`.`level` (`id`, `name`) VALUES (4, 'district');
COMMIT;
INSERT INTO location (parent,name,level) VALUES
(NULL,'a1',NULL), (NULL,'a2',NULL), (NULL,'a3',NULL);
INSERT INTO location (parent,name,level) VALUES
(1,'b1',NULL), (1,'b2',NULL), (2,'b3',NULL),
(2,'b4',NULL), (3,'b5',NULL), (3,'b6',NULL);
INSERT INTO location (parent,name,level) VALUES
(4,'c1',NULL), (4,'c2',NULL), (5,'c3',NULL),
(5,'c4',NULL), (6,'c5',NULL), (6,'c6',NULL),
(7,'c7',NULL), (7,'c8',NULL), (8,'c9',NULL),
(8,'c10',NULL), (9,'c11',NULL), (9,'c12',NULL);
SELECT * FROM details_location;
[(Mostly) auto-generated code from MySQL Workbench 5.2]
Note that the trigger insert_location determines item's level at registration time, increasing so the parent's level by one unit.

Error Code: 1452. Cannot add or update a child row

I am trying to load data to following tables:
-- -----------------------------------------------------
-- Table `mydb`.`Gener`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Gener` (
`Movie_Name` VARCHAR(100) NOT NULL ,
`Genres_Type` VARCHAR(100) NULL ,
`Movie_Year` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`Movie_Name`, `Movie_Year`) ,
CONSTRAINT `fk_Gener_Movie1`
FOREIGN KEY (`Movie_Name` , `Movie_Year` )
REFERENCES `mydb`.`Movie` (`Name` , `Year` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
-- -----------------------------------------------------
-- Table `mydb`.`Movie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Movie` (
`Name` VARCHAR(100) NULL ,
`Lenght` INT(20) NULL ,
`Year` VARCHAR(100) NOT NULL ,
`id` INT(5) NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`id`, `Name`, `Year`) )
-- -----------------------------------------------------
-- Table `mydb`.`tempGener`
-- -----------------------------------------------------
CREATE TABLE `mydb`.`tempGener' (
`mGName` VARCHAR(100),
`mGType` VARCHAR(100),
`mGYear` VARCHAR(100)
)
I successfully uploading data to following two tables.
tempGener,
movie.
Now I trying to load data to my Gener table by using following command,
INSERT INTO gener (Movie_Name, Genres_Type, Movie_Year)
(SELECT movie.Name, tempgener.mGType , movie.Year
FROM tempgener, movie)
But I receive following errors:
Error Code: 1452. Cannot add or update a child row: a foreign key
constraint fails (mydb3.gener, CONSTRAINT fk_Gener_Movie1
FOREIGN KEY (Movie_Name, Movie_Year) REFERENCES mydb.movie
(Name, Year) ON DELETE NO ACTION ON UPDATE NO ACTION)
I solve my problem, the problem was, I was missing WHERE statement.
Thanks for every body giving me fast response.

MySQL - How to create a child record under table2 whenever table1 is having a new record using MySQL workbench?

Trigger actually. But how do i make this in mysql-workbench. So that anytime i have any new records in table1 it creates a new record referencing table1.id to table2.parentid ?
--- [OK] --- This is created
CREATE TABLE IF NOT EXISTS `test`.`table1` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB
--- [FAIL] --- #1005 - Can't create table 'test.table2' (errno: 150)
CREATE TABLE IF NOT EXISTS `test`.`table2` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`parentid` VARCHAR(45) NULL ,
`table1_id` BIGINT(20) NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_table2_table1` (`parentid` ASC) ,
CONSTRAINT `fk_table2_table1`
FOREIGN KEY (`parentid` )
REFERENCES `test`.`table1` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
Follow up:
1) Mysql workbench do not offer that
2) Click the table1 > click the triggers tab bottom > write
-- trigger module initiate
DELIMITER $$
CREATE TRIGGER triggertest1 BEFORE INSERT ON table1
FOR EACH ROW BEGIN
INSERT INTO table2 SET parentid = NEW.id;
END;
$$
DELIMITER ; --- return to normal
Thats because you have different datatype of id column in your child table change it to BIGINT(20)