Reputation: 15
I've got a problem which drives me crazy. I searched on the internet but all i could find was: The data on which the foreign key references, is not inserted. Well, I checked it like 100 times, they are inserted.
My tables are:
Stundenanfrage:
CREATE TABLE IF NOT EXISTS Stundenanfrage(
LehrerKuerzel CHAR(10),
Anfangszeit TIMESTAMP,
Endzeit TIMESTAMP,
StundeGehalten TINYINT,
Akzeptiert TINYINT,
Lernprozess TEXT,
Sterne INT, -- 1-5 1 schlecht; 5 gut
BetrauNr INT REFERENCES ILB_Betrauung(BetrauNr),
PRIMARY KEY(LehrerKuerzel, Anfangszeit, Endzeit, BetrauNr),
FOREIGN KEY(LehrerKuerzel, Anfangszeit, Endzeit) REFERENCES Lehrerzeiten(LehrerKuerzel, Anfangszeit, Endzeit)
);
Lehrerzeiten:
CREATE TABLE IF NOT EXISTS Lehrerzeiten(
LehrerKuerzel CHAR(10) REFERENCES ILB_Lehrer,
Anfangszeit TIMESTAMP ,
Endzeit TIMESTAMP ,
Einzelunterricht TINYINT(1) DEFAULT 0,
Thema VARCHAR(100),
PRIMARY KEY(LehrerKuerzel, Anfangszeit, Endzeit)
);
And ilb_betrauung:
CREATE TABLE IF NOT EXISTS ILB_Betrauung(
BetrauNr INT PRIMARY KEY AUTO_INCREMENT,
LehrerKuerzel CHAR(10) REFERENCES ilb_lehrer,
MatNr CHAR(20) REFERENCES fw_schueler,
zweckmaeßig_erachtet_Lehrer TINYINT,
zweckmaeßig_erachtet_Schueler TINYINT,
Betrauung_AV TINYINT,
Eltern_informiert TINYINT
);
The inserted data:
My update query looks like this:
UPDATE stundenanfrage SET Akzeptiert = 1
WHERE LehrerKuerzel = "bb" AND Anfangszeit = "2017-02-20 12:20:00" AND
Endzeit = "2017-02-20 13:00:00";
and threw an error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`db_ilb`.`stundenanfrage`, CONSTRAINT `stundenanfrage_ibfk_1` FOREIGN KEY (`LehrerKuerzel`, `Anfangszeit`, `Endzeit`) REFERENCES `lehrerzeiten` (`LehrerKuerzel`, `Anfangszeit`, `Endzeit`)
I also joined all tables and selected that particular record I wanted to update:
SELECT * FROM stundenanfrage s
JOIN lehrerzeiten l ON s.Lehrerkuerzel = l.Lehrerkuerzel AND s.Anfangszeit =
l.Anfangszeit AND s.Endzeit = l.Endzeit
WHERE l.LehrerKuerzel = "bb" AND l.Anfangszeit = "2017-02-20 12:20:00" AND
l.Endzeit = "2017-02-20 13:00:00" AND s.BetrauNr = 1;
The output was that one record I wanted to update. I also asked my teacher about this error and she had no clue either.
Upvotes: 2
Views: 132
Reputation: 16551
Check Extra
:
mysql> DESC `Stundenanfrage`;
+----------------+------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------+------+-----+-------------------+-----------------------------+
| LehrerKuerzel | char(10) | NO | PRI | NULL | |
| Anfangszeit | timestamp | NO | PRI | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| Endzeit | timestamp | NO | PRI | NULL | |
| StundeGehalten | tinyint(4) | YES | | NULL | |
| Akzeptiert | tinyint(4) | YES | | NULL | |
| Lernprozess | text | YES | | NULL | |
| Sterne | int(11) | YES | | NULL | |
| BetrauNr | int(11) | NO | PRI | NULL | |
+----------------+------------+------+-----+-------------------+-----------------------------+
8 rows in set (0.00 sec)
See: 12.3.5 Automatic Initialization and Updating for TIMESTAMP and DATETIME.
Try:
CREATE TABLE IF NOT EXISTS Stundenanfrage (
LehrerKuerzel CHAR(10),
Anfangszeit TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Endzeit TIMESTAMP,
StundeGehalten TINYINT,
Akzeptiert TINYINT,
Lernprozess TEXT,
Sterne INT, -- 1-5 1 schlecht; 5 gut
BetrauNr INT REFERENCES ILB_Betrauung(BetrauNr),
PRIMARY KEY(LehrerKuerzel, Anfangszeit, Endzeit, BetrauNr),
FOREIGN KEY(LehrerKuerzel, Anfangszeit, Endzeit)
REFERENCES Lehrerzeiten(LehrerKuerzel, Anfangszeit, Endzeit)
);
mysql> DESC `Stundenanfrage`;
+----------------+------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------+------+-----+-------------------+-------+
| LehrerKuerzel | char(10) | NO | PRI | NULL | |
| Anfangszeit | timestamp | NO | PRI | CURRENT_TIMESTAMP | |
| Endzeit | timestamp | NO | PRI | NULL | |
| StundeGehalten | tinyint(4) | YES | | NULL | |
| Akzeptiert | tinyint(4) | YES | | NULL | |
| Lernprozess | text | YES | | NULL | |
| Sterne | int(11) | YES | | NULL | |
| BetrauNr | int(11) | NO | PRI | NULL | |
+----------------+------------+------+-----+-------------------+-------+
8 rows in set (0.00 sec)
Upvotes: 2