MySQL Forums
Forum List  »  Newbie

Triggers on two tables to get Primary Keys (Auto Increment) into third linking table
Posted by: Vladimir Velan
Date: February 22, 2016 04:19AM

Hi guys, I am new to SQL and looking for some help. I have a three tables: author, study and casestudy(which is linking table). What I want to achieve is when data are inserted into author and study tables (from webform) their Auto increment ID will be inserted into "casestudy" table if it is possible. I guess I will need to create triggers. Table structure is as follow:


CREATE TABLE `test`.`new_table` (
`AuthorId` INT(11) NOT NULL AUTO_INCREMENT,
`AuthorTitle` VARCHAR(45) NOT NULL,
PRIMARY KEY (`AuthorId`),
UNIQUE INDEX `AuthorId_UNIQUE` (`AuthorId` ASC));


CREATE TABLE `test`.`study` (
`StudyId` INT(11) NOT NULL AUTO_INCREMENT,
`Title` VARCHAR(45) NOT NULL,
PRIMARY KEY (`StudyId`),
UNIQUE INDEX `StudyId_UNIQUE` (`StudyId` ASC));



CREATE TABLE `test`.`casestudy` (
`AuthorId` INT(11) NOT NULL,
`StudyId` INT(11) NOT NULL,
PRIMARY KEY (`AuthorId`, `StudyId`),
INDEX `StudyId_idx` (`StudyId` ASC),
CONSTRAINT `AuthorId`
FOREIGN KEY (`AuthorId`)
REFERENCES `test`.`author` (`AuthorId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `StudyId`
FOREIGN KEY (`StudyId`)
REFERENCES `test`.`study` (`StudyId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

Any advice will be appreciated
Thank you

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.