Ich erstelle ein SQL-Skript und importiere es in phpMyAdmin. Hier ist der Code:SQL-Skriptfehler in phpMyAdamin - Fremdschlüssel-Zurückhaltung
DROP DATABASE IF EXISTS Carpool;
CREATE DATABASE Carpool;
USE Carpool;
CREATE TABLE IF NOT EXISTS users (
username varchar(100) NOT NULL,
firstname varchar(100) NOT NULL,
lastname varchar(100) NOT NULL,
age int(10) NOT NULL,
email varchar(100) NOT NULL,
gender varchar(1) NOT NULL,
passhash varchar(100) NOT NULL,
PRIMARY KEY(username)
);
CREATE TABLE IF NOT EXISTS cars (
car_id int(50) NOT NULL AUTO_INCREMENT,
cartype varchar(50),
neatness varchar(50),
color varchar(50),
seats int(50),
PRIMARY KEY(car_id)
);
CREATE TABLE IF NOT EXISTS startinglocation (
startinglocation_id int(100) NOT NULL AUTO_INCREMENT,
address varchar(200) NOT NULL,
zipcode varchar(50) NOT NULL,
city varchar(50) NOT NULL,
state varchar(50) NOT NULL,
longitude float(10) NOT NULL,
latitude float(10) NOT NULL,
PRIMARY KEY(startinglocation_id)
);
CREATE TABLE IF NOT EXISTS endinglocation (
endinglocation_id int(100) NOT NULL AUTO_INCREMENT,
address varchar(200) NOT NULL,
zipcode varchar(50) NOT NULL,
city varchar(50) NOT NULL,
state varchar(50) NOT NULL,
longitude float(10) NOT NULL,
latitude float(10) NOT NULL,
PRIMARY KEY(endinglocation_id)
);
CREATE TABLE IF NOT EXISTS carpool (
car_id int(50),
carpool_id int(50) NOT NULL AUTO_INCREMENT,
datetime varchar(50),
duration varchar(50),
numberofpassengers int(10),
startingtime varchar(50) NOT NULL,
endingtime varchar(50) NOT NULL,
recurrencelevel int(10),
startinglocation_id int(100),
endinglocation_id int(100),
PRIMARY KEY(carpool_id),
FOREIGN KEY(car_id)
REFERENCES cars(car_id)
ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY(startinglocation_id)
REFERENCES startinglocation(startinglocation_id)
ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY(endinglocation_id)
REFERENCES endinglocation(endinglocation_id)
ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS driver (
username varchar(50) NOT NULL,
carpool_id int(50) NOT NULL,
FOREIGN KEY (username)
REFERENCES users(username)
ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY (carpool_id)
REFERENCES carpool(carpool_id)
ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS passenger (
username varchar(50) NOT NULL,
carpool_id int(50) NOT NULL,
FOREIGN KEY (username)
REFERENCES users(username)
ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY (carpool_id)
REFERENCES carpool(carpool_id)
ON DELETE SET NULL ON UPDATE CASCADE
);
GRANT SELECT, INSERT, UPDATE, DELETE
ON *
TO mgs_user
IDENTIFIED BY 'Password1';
Und Ich erhalte diese als Fehlerbericht:
Fehler SQL-Abfrage:
CREATE TABLE IF NOT EXISTS carpool (
car_id int(50),
carpool_id int(50) NOT NULL AUTO_INCREMENT,
datetime varchar(50),
duration varchar(50),
numberofpassengers int(10),
startingtime varchar(50) NOT NULL,
endingtime varchar(50) NOT NULL,
recurrencelevel int(10),
startinglocation_id int(100),
endinglocation_id int(100),
PRIMARY KEY(carpool_id),
FOREIGN KEY(car_id)
REFERENCES cars(car_id)
ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY(startinglocation_id)
REFERENCES startinglocation(startinglocation_id)
ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY(endinglocation_id)
REFERENCES endinglocation(endinglocation_id)
ON DELETE SET NULL ON UPDATE CASCADE
)
MySQL sagte: Dokumentation
1005 - Can‘ t Tabelle carpool
erstellen. driver
(errno: 150 "Foreign-Key-Constraint ist falsch gebildet") (Details ...)
Ich habe das Gefühl, dass ich die Tabellen korrekt für die Fremdschlüssel referenziert, aber mit geringen Änderungen, die ich versucht habe, nicht erfolgreich waren.
Sie können keine Tabelle referenzieren, die noch nicht definiert ist. Ändern Sie die Reihenfolge der Tabellenerstellung. –
Ich habe den obigen Code bearbeitet, um zu zeigen, dass es sich um eine Methode handelt, die ich ausprobiert habe. Ich habe sichergestellt, dass alle Fremdschlüssel vorhanden sind, nachdem die Tabelle erstellt wurde, auf die verwiesen wird. Es erzeugt den gleichen Fehler. –
driver.username und users.username sind nicht vom selben Typ (Länge). Warum verwenden Sie keine auto_incrementing user_id? –