Insert into author (Cl1,cl2,...) values (val1,val2,..)
--check if exist authorId
If exists select authorId from author where authorid = @last_insertedId
DELIMITER $$
USE `mybook_store`$$
DROP PROCEDURE IF EXISTS `myProc4`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `myProc4`()
BEGIN
DECLARE authId INT DEFAULT 0;
DECLARE pubId INT DEFAULT 0;
DECLARE categId INT DEFAULT 0;
DECLARE CheckExists INT DEFAULT 0;
SET CheckExists = 0;
BEGIN
SELECT COUNT(*) INTO CheckExists FROM author WHERE fname = 'xxxx' AND surname='xxxx';
IF (CheckExists > 0) THEN
SELECT authorid INTO authId FROM author WHERE fname = 'xxxx' AND surname='xxxx';
ELSE
INSERT author(fname, surname) VALUES('xxxx', 'xxxx');
SET authId = LAST_INSERT_ID();
INSERT author_book (authorId) VALUES(authId);
SELECT authId;
END IF;
END;
BEGIN
SELECT COUNT(*) INTO CheckExists FROM publisher WHERE publisher.Name = 'xxxx' ;
IF (CheckExists > 0) THEN
SELECT publisherId INTO pubId FROM publisher WHERE publisher.Name = 'xxxx';
ELSE
INSERT publisher(publisher.Name) VALUES('xxxx');
SET pubId = LAST_INSERT_ID();
SELECT pubId;
END IF;
END;
BEGIN
SELECT COUNT(*) INTO CheckExists FROM category WHERE category.Name = 'test1' ;
IF (CheckExists > 0) THEN
SELECT categoryId INTO categId FROM category WHERE publisher.Name = 'test1';
ELSE
INSERT category(category.Name) VALUES('test1');
SET categId = LAST_INSERT_ID();
SELECT categId;
END IF;
END;
END$$
DELIMITER ;
1. Check the publisher, if exists get the publisherid, else add new publisher and return the publisherid
2. Check the author, if exists get the authorid, else add new author and return the authorid
3. Insert new record to book table with publisherid and authorid you get from 2 steps above
But note that you might have to pass not only fields of book, but also fields of publisher (date_published, name) and author (fname, surname).