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:
Warum erhalte ich Fehler #1215 - Fremdschlüssel-Beschränkung kann nicht hinzugefügt werden? Wo liegt mein Fehler? Kann mir jemand helfen?
Danke.
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.
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Kommentar vom Moderator tomolpi am 19.04.2020 um 15:09:11 Uhr
Code-Tags hinzugefügt und Titel korrigiert
Content-ID: 566120
Url: https://administrator.de/forum/foreign-key-bei-mysql-8-0-566120.html
Ausgedruckt am: 17.04.2025 um 17:04 Uhr
5 Kommentare
Neuester Kommentar
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.