theussen
Goto Top

Foreign Key bei MySQL 8.0

Hallo zusammen,

ich möchte mir eine Datenbank für eine Internetseite über unsere Nachbarschaft erstellen. Ich habe unter anderen drei Tabellen angelegt. Dabei handelt es sich um jeweils eine Tabelle für Telefonummern mit den Einträgen Telefonnummer und Typ (Typ steht für Handy, Festnetz, FAX, etc.) und eine für SocialMedia für Verwednung und Typ (also, Email,, Homepage, Facebook, etc.) Die dritte Tabelle dient dazu auf die beiden anderen zu referenzeiren und soviele Verweise je Benutzer zustellen, wie benötigt werden (also: z.B. drei Einträge für Telefonnummern und zwei Einträge für Soziale Verbindung zu speichern.)

Die Tabellen sehen wie folgt aus:

CREATE TABLE CommunicationDetails_Phone (
	CommunicationDetails_Phone_ID VARCHAR(4) NOT NULL,
	CommunicationDetails_Phone_Name VARCHAR(128),
	CommunicationDetails_Phone_Number VARCHAR(128),
	PRIMARY KEY (CommunicationDetails_Phone_ID)
) ENGINE=INNODB;

CREATE TABLE CommunicationDetails_SocialMedia (
	CommunicationDetails_SocialMedia_ID VARCHAR(4) NOT NULL,
	CommunicationDetails_SocialMedia_Name VARCHAR(128),
	CommunicationDetails_SocialMedia_Item VARCHAR(512),
	PRIMARY KEY (CommunicationDetails_SocialMedia_ID)
) ENGINE=INNODB;

CREATE TABLE Communication (
	Communication_ID VARCHAR(4) NOT NULL,
	Communication_Phone_ID VARCHAR(4) NOT NULL,
	Communication_SocialMedia_ID VARCHAR(4) NOT NULL,
	PRIMARY KEY (Communication_ID),
	INDEX (Communication_Phone_ID),
	INDEX (Communication_SocialMedia_ID),
	FOREIGN KEY (Communication_Phone_ID)
		REFERENCES CommunicationDetails_Phone(CommunicationDetails_Phone_ID)
			ON UPDATE CASCADE ON DELETE RESTRICT,
	FOREIGN KEY (Communication_SocialMedia_ID)
		REFERENCES CommunicationDetails_Socialmedia(CommunicationDetails_SocialMedia_ID)
			ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=INNODB;

Warum erhalte ich Fehler #1215 - Fremdschlüssel-Beschränkung kann nicht hinzugefügt werden? Wo liegt mein Fehler? Kann mir jemand helfen?

Danke.
Kommentar vom Moderator tomolpi am Apr 19, 2020 um 13:09:11 Uhr
Code-Tags hinzugefügt und Titel korrigiert

Content-Key: 566120

Url: https://administrator.de/contentid/566120

Printed on: April 28, 2024 at 18:04 o'clock

Member: falscher-sperrstatus
Solution falscher-sperrstatus Apr 19, 2020 updated at 12:41:49 (UTC)
Goto Top
Hallo,

vermutlich fehlt dir dein Auto-increment oder die Unique-ID, als Ansatz zur Fehlersuche.

Grüße
Member: akretschmer
Solution akretschmer Apr 19, 2020 at 12:57:32 (UTC)
Goto Top
test=*# CREATE TABLE Communication (
test(# Communication_ID VARCHAR(4) NOT NULL,
test(# Communication_Phone_ID VARCHAR(4) NOT NULL,
test(# Communication_SocialMedia_ID VARCHAR(4) NOT NULL,
test(# PRIMARY KEY (Communication_ID),
test(# INDEX (Communication_Phone_ID),
test(# INDEX (Communication_SocialMedia_ID),
test(# FOREIGN KEY (Communication_Phone_ID)
test(# REFERENCES CommunicationDetails_Phone(CommunicationDetails_Phone_ID)
test(# ON UPDATE CASCADE ON DELETE RESTRICT,
test(# FOREIGN KEY (Communication_SocialMedia_ID)
test(# REFERENCES CommunicationDetails_Socialmedia(CommunicationDetails_SocialMedia_ID)
test(# ON UPDATE CASCADE ON DELETE RESTRICT
test(# ) ;
ERROR:  syntax error at or near "("  
LINE 6: INDEX (Communication_Phone_ID),
              ^
test=*# CREATE TABLE Communication (
test(# Communication_ID VARCHAR(4) NOT NULL,
test(# Communication_Phone_ID VARCHAR(4) NOT NULL,
test(# Communication_SocialMedia_ID VARCHAR(4) NOT NULL,
test(# PRIMARY KEY (Communication_ID),FOREIGN KEY (Communication_Phone_ID)
test(# REFERENCES CommunicationDetails_Phone(CommunicationDetails_Phone_ID)
test(# ON UPDATE CASCADE ON DELETE RESTRICT,
test(# FOREIGN KEY (Communication_SocialMedia_ID)
test(# REFERENCES CommunicationDetails_Socialmedia(CommunicationDetails_SocialMedia_ID)
test(# ON UPDATE CASCADE ON DELETE RESTRICT
test(# );
CREATE TABLE
test=*#

finde den Unterschied und füge die Indexe später hinzu.

PostgreSQL.
Member: theussen
theussen Apr 19, 2020 at 13:41:24 (UTC)
Goto Top
Danke, habe den Fehler gefunden.
Member: falscher-sperrstatus
falscher-sperrstatus Apr 19, 2020 at 13:54:50 (UTC)
Goto Top
Na dann, Lösung posten und Ende Gelände.
Member: theussen
theussen Apr 19, 2020 at 14:08:44 (UTC)
Goto Top
Es war ein Schreibfehler

test(# REFERENCES CommunicationDetails_Socialmedia(CommunicationDetails_SocialMedia_ID)

CommunicationDetails_Socialmedia war das 'm' von SocialMedia klein statt groß geschrieben. Das hatte ich übersehen.