我有一所學校的作業,這是給我和我的朋友們頭痛......所以這是我的查詢,我已執行,一次一個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工作過......
有任何想法,因此,任何人?
在此先感謝!
請問您的代碼真的有兩個'FORIEGN KEY'條款,以'GamblingTable'在同一時間? – mellamokb
哦,只是看到現在......第一行不應該有...只是一個錯字 – Mockarutan