2017-04-10 146 views
0

我在Ubuntu 16.04 LTS上遇到Navicat問題,我正在嘗試執行腳本,並且它在最後一張表上表示不能添加外鍵約束,這裏是我的腳本,有誰知道我在哪裏錯了?MySQL不會將外鍵多於一個外鍵添加到同一個表

感謝諮詢

這裏是我的SQL腳本:

CREATE TABLE `SMER` (
    `Ssmer` INT(10) NOT NULL AUTO_INCREMENT, 
    `Nazivs` VARCHAR(255) NOT NULL, 
    PRIMARY KEY (`Ssmer`) 
    ); 

CREATE TABLE `Nastavnici` (
    `Snast` INT(10) NOT NULL AUTO_INCREMENT, 
    `Imen` VARCHAR(255) NOT NULL, 
    PRIMARY KEY (`Snast`) 
    ); 

CREATE TABLE `PREDMETI` (
    `SPRED` INT(10) NOT NULL AUTO_INCREMENT, 
    `NAZIVP` VARCHAR(255) NOT NULL, 
    PRIMARY KEY (`SPRED`) 
    ); 

CREATE TABLE `Studenti` (
    `Indeks` INT(10) NOT NULL, 
    `Upisan` INT(10) NOT NULL, 
    `Imes` VARCHAR(255) NOT NULL, 
    `Mesto` VARCHAR(255), 
    `Datr` DATETIME NOT NULL, 
    `Ssmer` INT(10) NOT NULL, 
    PRIMARY KEY (`Indeks`, `Upisan`), 
    CONSTRAINT `FK_Studenti_Ssmer` FOREIGN KEY (`Ssmer`) REFERENCES `SMER` (`Ssmer`) 
    ); 

CREATE TABLE `USLOVNI` (
    `Spred` INT(10) NOT NULL, 
    `UslPredmet` INT(10) NOT NULL, 
    PRIMARY KEY (`Spred`, `UslPredmet`), 
    CONSTRAINT `FK_Uslovni_Spred` FOREIGN KEY (`Spred`) REFERENCES `PREDMETI` (`SPRED`), 
    CONSTRAINT `FK_Uslovni_UslPredmet` FOREIGN KEY (`UslPredmet`) REFERENCES `PREDMETI` (`SPRED`) 
    ); 

CREATE TABLE `Planst` (
    `Ssmer` INT(10) NOT NULL, 
    `Spred` INT(10) NOT NULL, 
    `Semestar` VARCHAR(255) DEFAULT NULL, 
    PRIMARY KEY (`Ssmer`, `Spred`), 
    CONSTRAINT `FK_Planst_Ssmer` FOREIGN KEY (`Ssmer`) REFERENCES `SMER` (`Ssmer`), 
    CONSTRAINT `FK_Planst_Spred` FOREIGN KEY (`Spred`) REFERENCES `PREDMETI` (`SPRED`) 
    ); 

CREATE TABLE `Angazovanje` (
    `Snast` INT(10) NOT NULL, 
    `Spred` INT(10) NOT NULL, 
    `Ssmer` INT(10) NOT NULL, 
    PRIMARY KEY (`Snast`, `Spred`, `Ssmer`), 
    CONSTRAINT `FK_Angazovanje_Snast` FOREIGN KEY (`Snast`) REFERENCES `Nastavnici` (`Snast`), 
    CONSTRAINT `FK_Angazovanje_Spred` FOREIGN KEY (`Spred`) REFERENCES `PREDMETI` (`SPRED`), 
    CONSTRAINT `FK_Angazovanje_Ssmer` FOREIGN KEY (`Ssmer`) REFERENCES `SMER` (`Ssmer`) 
    ); 

CREATE TABLE `Prijave` (
    `Spred` INT(10) NOT NULL, 
    `Indeks` INT(10) NOT NULL, 
    `Upisan` INT(10) NOT NULL, 
    `Snast` INT(10) NOT NULL, 
    `Datump` DATETIME DEFAULT NOW(), 
    `Ocena` INT(2) NOT NULL, 
    PRIMARY KEY (`Spred`, `Indeks`, `Upisan`, `Datump`), 
    CONSTRAINT `FK_Prijave_Spred` FOREIGN KEY (`Spred`) REFERENCES `PREDMETI` (`SPRED`), 
    CONSTRAINT `FK_Prijave_Indeks` FOREIGN KEY (`Indeks`) REFERENCES `Studenti` (`Indeks`), 
    CONSTRAINT `FK_Prijave_Upisan` FOREIGN KEY (`Upisan`) REFERENCES `Studenti` (`Upisan`), 
    CONSTRAINT `FK_Prijave_Snast` FOREIGN KEY (`Snast`) REFERENCES `Nastavnici` (`Snast`) 
    ); 

以下是錯誤消息:

 [Err] 1215 - Cannot add foreign key constraint 
+0

粘貼確切的錯誤信息呢? –

+0

林不知道,但它可能是兩個不同的外鍵對一個和同一個表'Studenti'是不允許的(至少不是所有的MySQL版本)。如果刪除其中一個約束,您是否仍然遇到同樣的錯誤?例如,嘗試刪除行'CONSTRAINT FK_Prijave_Upisan FOREIGN KEY(Upisan)REFERENCES Studenti(Upisan)',並讓我們知道如果錯誤仍然存​​在... –

+0

當我刪除該約束時,它也可以工作 – tehnodrom

回答

1

您從Prijave表中的兩個FKS指向您Studenti表。 MySQL不能很好地播放。

代替這個試試這個

CONSTRAINT `FK_Prijave_Indeks` FOREIGN KEY (`Indeks`, `Upisan`) 
           REFERENCES `Studenti` (`Indeks`, `Upisan`), 

CONSTRAINT `FK_Prijave_Indeks` FOREIGN KEY (`Indeks`) REFERENCES `Studenti` (`Indeks`), /* wrong !*/ 
CONSTRAINT `FK_Prijave_Upisan` FOREIGN KEY (`Upisan`) REFERENCES `Studenti` (`Upisan`), 
+0

就是這樣。我不知道必須這樣做。 – tehnodrom