SQL:使用外键插入3个表

时间:2015-03-26 15:38:14

标签: mysql sql

我正在尝试将数据添加到3个表中。桌子" Gebruiker"是父表。 我使用以下代码,它给了我以下错误:

我的SQL代码:

SELECT *
FROM Gebruiker
INNER JOIN Inlog
ON Gebruiker.idGebruiker=Inlog.Gebruiker_idGebruiker
INNER JOIN GGevens
ON Gebruiker.idGebruiker=GGevens.Gebruiker_idGebruiker
INNER JOIN Domein
ON Gebruiker.Domein_idDomeint=Domein.idDomeint;
BEGIN;
INSERT INTO Gebruiker (Domein_idDomeint,idGebruiker)
VALUES (1,NULL);

INSERT INTO Inlog (Gebruiker_idGebruiker,UserName,UserPass)
VALUES (LAST_INSERT_ID(),'profile','drie');

INSERT INTO GGevens (Gebruiker_idGebruiker,Email,Voornaam,Tussenvoeg,Achternaam,Geslacht,Opleiding,GebDatum)
VALUES (LAST_INSERT_ID(),'aapje@peer.nl','Aapje','van','Drie',1,'Zeerslim','2014-11-11');
COMMIT;

错误:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`zolstm001`.`GGevens`, CONSTRAINT `fk_GGevens_Gebruiker1` FOREIGN KEY (`Gebruiker_idGebruiker`) REFERENCES `Gebruiker` (`idGebruiker`) ON DELETE NO ACTION ON UPDATE NO ACTION) 

我的数据库结构: http://i.imgur.com/bxeQbhQ.png

我认为这是因为表格" GGevens"需要表格中的外键" Gebruiker"

有人知道我怎么能添加这个吗?

(LAST_INSERT_ID()不起作用......

用于创建表的SQL代码:

-- MySQL Workbench Forward Engineering

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 zolstm001
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema zolstm001
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `zolstm001` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `zolstm001` ;

-- -----------------------------------------------------
-- Table `zolstm001`.`Domein`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Domein` (
  `idDomeint` INT NOT NULL AUTO_INCREMENT,
  `Naam` VARCHAR(45) NOT NULL,
  `OverigeGegevens` VARCHAR(45) NULL,
  PRIMARY KEY (`idDomeint`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `zolstm001`.`Gebruiker`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Gebruiker` (
  `idGebruiker` INT NOT NULL AUTO_INCREMENT,
  `Domein_idDomeint` INT NOT NULL,
  PRIMARY KEY (`idGebruiker`),
  INDEX `fk_Gebruiker_Domein1_idx` (`Domein_idDomeint` ASC),
  CONSTRAINT `fk_Gebruiker_Domein1`
    FOREIGN KEY (`Domein_idDomeint`)
    REFERENCES `zolstm001`.`Domein` (`idDomeint`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `zolstm001`.`GGevens`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`GGevens` (
  `idGGevens` INT NOT NULL AUTO_INCREMENT,
  `Gebruiker_idGebruiker` INT NOT NULL,
  `Email` VARCHAR(45) NOT NULL,
  `GebDatum` DATE NOT NULL,
  PRIMARY KEY (`idGGevens`),
  INDEX `fk_GGevens_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
  CONSTRAINT `fk_GGevens_Gebruiker1`
    FOREIGN KEY (`Gebruiker_idGebruiker`)
    REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `zolstm001`.`Inlog`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Inlog` (
  `idInlog` INT NOT NULL AUTO_INCREMENT,
  `UserName` VARCHAR(45) NOT NULL,
  `UserPass` VARCHAR(45) NOT NULL,
  `Gebruiker_idGebruiker` INT NOT NULL,
  PRIMARY KEY (`idInlog`),
  INDEX `fk_Inlog_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
  CONSTRAINT `fk_Inlog_Gebruiker1`
    FOREIGN KEY (`Gebruiker_idGebruiker`)
    REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `zolstm001`.`Startup`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Startup` (
  `idStartup` INT NOT NULL AUTO_INCREMENT,
  `Naam` VARCHAR(45) NOT NULL,
  `Gebruiker_idGebruiker` INT NOT NULL,
  PRIMARY KEY (`idStartup`),
  INDEX `fk_Startup_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
  CONSTRAINT `fk_Startup_Gebruiker1`
    FOREIGN KEY (`Gebruiker_idGebruiker`)
    REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `zolstm001`.`Reactie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Reactie` (
  `idReactie` INT NOT NULL AUTO_INCREMENT,
  `Gebruiker_idGebruiker` INT NOT NULL,
  `Bericht` VARCHAR(45) NOT NULL,
  `Startup_idStartup` INT NOT NULL,
  `Reactiecol` VARCHAR(45) NULL,
  PRIMARY KEY (`idReactie`),
  INDEX `fk_Reactie_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
  INDEX `fk_Reactie_Startup1_idx` (`Startup_idStartup` ASC),
  CONSTRAINT `fk_Reactie_Gebruiker1`
    FOREIGN KEY (`Gebruiker_idGebruiker`)
    REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Reactie_Startup1`
    FOREIGN KEY (`Startup_idStartup`)
    REFERENCES `zolstm001`.`Startup` (`idStartup`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `zolstm001`.`Category`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Category` (
  `idCategory` INT NOT NULL AUTO_INCREMENT,
  `Startup_idStartup` INT NOT NULL,
  PRIMARY KEY (`idCategory`),
  INDEX `fk_Category_Startup1_idx` (`Startup_idStartup` ASC),
  CONSTRAINT `fk_Category_Startup1`
    FOREIGN KEY (`Startup_idStartup`)
    REFERENCES `zolstm001`.`Startup` (`idStartup`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `zolstm001`.`Unit`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Unit` (
  `idUnit` INT NOT NULL AUTO_INCREMENT,
  `Startup_idStartup` INT NOT NULL,
  PRIMARY KEY (`idUnit`),
  INDEX `fk_Unit_Startup1_idx` (`Startup_idStartup` ASC),
  CONSTRAINT `fk_Unit_Startup1`
    FOREIGN KEY (`Startup_idStartup`)
    REFERENCES `zolstm001`.`Startup` (`idStartup`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `zolstm001`.`SGegevens`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`SGegevens` (
  `idSGegevens` INT NOT NULL AUTO_INCREMENT,
  `Startup_idStartup` INT NOT NULL,
  PRIMARY KEY (`idSGegevens`),
  INDEX `fk_SGegevens_Startup1_idx` (`Startup_idStartup` ASC),
  CONSTRAINT `fk_SGegevens_Startup1`
    FOREIGN KEY (`Startup_idStartup`)
    REFERENCES `zolstm001`.`Startup` (`idStartup`)
    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;

1 个答案:

答案 0 :(得分:0)

我会将INSERT INTO gebruiker更改为     INSERT INTO Gebruiker(Domein_idDomeint)VALUES(1);

此外,要使其正常工作,您需要使用一些编程代码来帮助您。这可以以存储过程的形式完成。

CREATE PROCEDURE gebruikerProc (IN {your_params})
BEGIN 
DECLARE lastId INT;
INSERT INTO Gebruiker (Domein_idDomeint) VALUES (1);
SET lastId=LAST_INSERT_ID();
INSERT INTO Inlog (Gebruiker_idGebruiker,UserName,UserPass)
VALUES (lastId,'profile','drie');

INSERT INTO GGevens (Gebruiker_idGebruiker,Email,Voornaam,Tussenvoeg,Achternaam,Geslacht,Opleiding,GebDatum)
VALUES (lastId,'aapje@peer.nl','Aapje','van','Drie',1,'Zeerslim','2014-11-11');
END;

一个警告:不要使用TAB在MySQL中的存储过程/函数/触发器等中缩进代码。你会收到一个错误。它只是无法处理TAB。