Good evening.
For a project, I have to create a system. In this system, users can subscribe for a masterclass. The administrator should be able to delete users and masterclasses, but the latter only if no users has subscribed to it.
My database:
CREATE TABLE IF NOT EXISTS Speler_masterclass (
Sid INT(11) NOT NULL AUTO_INCREMENT,
naam VARCHAR(50) NOT NULL,
adres VARCHAR(100) NOT NULL,
postcode VARCHAR(100) NOT NULL,
woonplaats VARCHAR(100) NOT NULL,
telefoonnr INT(20) NOT NULL,
email VARCHAR(100) NOT NULL,
ratingscore INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY(Sid)
)ENGINE=InnoDb;
CREATE TABLE IF NOT EXISTS db11071230.Masterclass (
Mid INT(11) NOT NULL AUTO_INCREMENT,
naam VARCHAR(50) NOT NULL,
tijd VARCHAR(20) NOT NULL,
datum VARCHAR(10) NOT NULL,
plaats VARCHAR(100) NOT NULL,
minimale_rating INT(11) NOT NULL,
PRIMARY KEY (Mid)
) ENGINE=InnoDb;
CREATE TABLE IF NOT EXISTS db11071230.Geeft_masterclass (
Rankingspunten_cadeau INT(11) NOT NULL,
Sid INT(11) NOT NULL,
Mid INT(11) NOT NULL,
FOREIGN KEY (Sid) REFERENCES Speler_masterclass (Sid),
FOREIGN KEY (Mid) REFERENCES Masterclass (Mid),
PRIMARY KEY (Sid, Mid)
) ENGINE=InnoDb;
CREATE TABLE IF NOT EXISTS db11071230.Inschrijving_masterclass (
betaling INT(1) NOT NULL,
Sid INT(11) NOT NULL ,
Mid INT(11) NOT NULL ,
FOREIGN KEY (Sid) REFERENCES Speler_masterclass (Sid),
FOREIGN KEY (Mid) REFERENCES Masterclass (Mid)
ON DELETE RESTRICT
ON UPDATE CASCADE,
PRIMARY KEY (Sid, Mid)
) ENGINE=InnoDb;
IN this, I want to insert the following:
INSERT INTO Geeft_masterclass
VALUES (5, 1, 1)
;
INSERT INTO Geeft_masterclass
VALUES (6, 2, 2)
;
INSERT INTO Geeft_masterclass
VALUES (7, 2, 2)
;
INSERT INTO Inschrijving_masterclass
VALUES (0, 1, 1)
;
INSERT INTO Inschrijving_masterclass
VALUES (1, 2, 1)
;
INSERT INTO Inschrijving_masterclass
VALUES (1, 3, 2)
;
INSERT INTO Masterclass
VALUES (1, 'Masterclass 1', '10.30 uur', '15-2-2012',' Den Haag', 10)
;
INSERT INTO Masterclass
VALUES (2, 'Masterclass 2', '11.30 uur', '16-2-2012',' Den Haag', 11)
;
INSERT INTO Masterclass
VALUES (3, 'Masterclass 3', '12.30 uur', '17-2-2012',' Den Haag', 12)
;
INSERT INTO Speler_masterclass
VALUES (1, 'Speler 1', 'Adres', 'postcode','Den Haag', '0612345678', 'email@adres.nl', 0)
;
INSERT INTO Speler_masterclass
VALUES (2, 'Speler 2', 'Adres', 'postcode','Den Haag', '0612345678', 'email@adres.nl', 0)
;
INSERT INTO Speler_masterclass
VALUES (3, 'Speler 3', 'Adres', 'postcode','Den Haag', '0612345678', 'email@adres.nl', 0)
;
HOWEVER, if I try to do so, I get the following error:
Error code: 1452. Cannot add or update a child row: a foreign key constraint fails.
I have not got a clue what I’m doing wrong. 🙁
When you have foreign key references from one table (source) into another (target), you need to set up the data in that target table first.
At the point where you’re inserting (for example)
{5,1,1}intoGeeft_masterclass(and those second and third column have foreign key constaints), there is no data in theSpeler_masterclassorMasterclasstables, the two tables which are the targets for those constraints.Quick solution, populate the tables in the same order that you create them. Then the data should exist in the target tables which will allow the constraints to succeed in the source tables:
Speler_masterclass;Masterclass;Geeft_masterclass, referencesSpeler_masterclassandMasterclass; andInschrijving_masterclass, referencesSpeler_masterclassandMasterclass.