2011-09-27 112 views
0

我有一所學校的作業,這是給我和我的朋友們頭痛......所以這是我的查詢,我已執行,一次一個OFC ...問題在SQL數據庫外鍵

CREATE DATABASE GamblingSociety; 

USE GamblingSociety; 

CREATE TABLE GamblingDen 
(
    Name VARCHAR (20) PRIMARY KEY, 
    Address VARCHAR (50), 
    Phone VARCHAR (10) 
); 

CREATE TABLE Room (
    RoomNr INT, 
    GameCapaity INT, 
    GamblingDenName VARCHAR (20), 
    PRIMARY KEY (RoomNr, GamblingDenName), 
    FOREIGN KEY (GamblingDenName) REFERENCES GamblingDen (Name) 
); 

CREATE TABLE Employee (
    SSN CHAR (11) PRIMARY KEY, 
    Name VARCHAR (20), 
    Address VARCHAR(50), 
    Salary INT, 
    isBoss BOOL, 
    GamblingDenName VARCHAR (20), 
    FOREIGN KEY (GamblingDenName) REFERENCES GamblingDen (Name) 
); 

CREATE TABLE GameType (
    Name VARCHAR (20) PRIMARY KEY, 
    WinningProcentage FLOAT, 
    ResponsibleEmployee CHAR (11), 
    FOREIGN KEY (ResponsibleEmployee) REFERENCES Employee (SSN) 
); 

CREATE TABLE Supplier (
    Name VARCHAR (20) PRIMARY KEY, 
    Address VARCHAR (50) 
); 

CREATE TABLE SupplierOfGameType (
    SupplierName VARCHAR (20), 
    GameTypeName VARCHAR (20), 
    PRIMARY KEY(SupplierName, GameTypeName), 
    FOREIGN KEY (SupplierName) REFERENCES Supplier (Name), 
    FOREIGN KEY (GameTypeName) REFERENCES GameType (Name) 
); 

CREATE TABLE GamblingTable (
    TableNr INT, 
    RoomNr INT, 
    GamblingDenName VARCHAR (20), 
    GameTypeName VARCHAR (20), 
    Comments VARCHAR (128), 
    PRIMARY KEY (RoomNr, TableNr, GamblingDenName, GameTypeName), 
    FOREIGN KEY (RoomNr, GamblingDenName) REFERENCES Room (RoomNr, GamblingDenName), 
    FOREIGN KEY (GameTypeName) REFERENCES GameType (Name) 
); 

CREATE TABLE Shylock (
    Phone CHAR (10) PRIMARY KEY, 
    Name VARCHAR (20), 
    Contry CHAR (3) 
); 

CREATE TABLE Customer (
    SSN CHAR (11) PRIMARY KEY, 
    Name VARCHAR (20), 
    Phone CHAR (10), 
    Address VARCHAR (50) 
); 

但這最後一個查詢不起作用:

USE GamblingSociety; 

CREATE TABLE GamblingInstance (
    StartTime DATETIME, 
    EndingTime DATETIME, 
    GamblingDenName VARCHAR (20), 
    TableNr INT, 
    GameTypeName VARCHAR (20), 
    Customer CHAR (11), 
    Shylock CHAR (10), 
    Debt INT, 
    DebtPayed BOOL, 
    DebtPayedDate DATETIME, 
    PRIMARY KEY (StartTime, GameTypeName, Customer), 
    FOREIGN KEY (GamblingDenName, TableNr, GameTypeName) REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName), 
    FOREIGN KEY (Customer) REFERENCES Customer (SSN), 
    FOREIGN KEY (Shylock) REFERENCES Shylock (Phone) 
); 

它給我這個錯誤: 「錯誤代碼:1005無法創建表 'gamblingsociety.gamblinginstance'(錯誤:150)」

,如果我取代這個:

FOREIGN KEY (GamblingDenName, TableNr, GameTypeName) REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName), 

與此:

FOREIGN KEY (GameTypeName) REFERENCES GamblingTable (GameTypeName), 

它的工作原理,所以它的東西與GamblingDenName和TableNr。

和我有谷歌了一下,發現了很多技巧,但沒有yeat工作過......

有任何想法,因此,任何人?

在此先感謝!

+1

請問您的代碼真的有兩個'FORIEGN KEY'條款,以'GamblingTable'在同一時間? – mellamokb

+0

哦,只是看到現在......第一行不應該有...只是一個錯字 – Mockarutan

回答

0

有一個在GamblingTable沒有鑰匙能夠滿足這個參考:

REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName), 

也就是說,存在GamblingTable針對GamblingDenName, TableNr, GameTypeName是唯一的列,或者前三欄沒有鑰匙。

There is一個滿足REFERENCES GamblingTable (GameTypeName)的密鑰,這就是該版本工作的原因。

(我不得不說,我從來沒有真正看到被用來父其它外鍵外鍵,但我想它的工作原理)。

+0

嗯..因此,換句話說,如果'PRIMARY KEY'被安排在這樣一種方式,前三它會工作列相匹配的外鍵引用,即'PRIMARY KEY(GamblingDenName,TableNr,GameTypeName,RoomNr)'? – mellamokb

+0

是的,該*應該*工作。在某個地方有一個數據庫,需要進行某種設置更改以允許這樣的部分鍵引用,它可能是MySQL。 –

+0

但需要注意的是,對於加快搜索的目的,你必須包含在索引中的可轉位比較,列先從最左邊的列。所以,如果你在主鍵排序的列目的是速度房間號搜索將不會與不同地佈置PRIMARY KEY發生了。 –