2017-05-24 5 views
0
Import

Ich habe den folgenden SQL-Skript:Kann keinen Fremdschlüssel hinzufügen, wenn sie von php meiner Admin

use restaurant; 
set foreign_key_checks=0; 
create table rtable_schedule(
    id int(11) not null, 
    rdate date not null, 
    start_hour tinyint, 
    start_min tinyint, 
    end_hour tinyint, 
    end_min tinyint, 
    foreign key (id,rdate) references rtable(id,reservation_date), 
    primary key (id,rdate) 
); 

Jedoch, wenn ich es über phpMyAdmin importieren bekomme ich folgende Fehlermeldung:

#1215 - Cannot add foreign key constraint Irgendwelche Ideen, wie kann ?, das gelöst werden Beachten sie schon, dass rtable

+0

Hat 'rtable'' id' und 'reservation_date' als kombinierten Primärschlüssel? –

+0

Nein, es hat ID als Primärschlüssel –

Antwort

1

Here's die MySQL-Dokumentation für Foriegn Schlüssel vorhanden ist, das ist, was es sagt:

InnoDB permits a foreign key to reference any column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.

NDB requires an explicit unique key (or primary key) on any column referenced as a foreign key.

Da reservation_date Spalte kein Teil eines Primärschlüssels ist, ist es wahrscheinlich, dass es keinen Index in rtable hat. Ich würde empfehlen, einen Index auf reservation_date Spalte zu erstellen, bevor Sie eine Tabelle mit Foreign Key erstellen.

+0

Sorry, aber ich bin neu auf SQL.Was meinst du mit der Erstellung eines Index? –

+0

https://dev.mysql.com/doc/refman/5.7/de/create-index.html –

Verwandte Themen