0

我有一项让我和我的朋友头疼的学校作业...所以这是我执行的查询,一次一个...

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'(errno:150)”

如果我替换这个:

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

有了这个:

FOREIGN KEY (GameTypeName) REFERENCES GamblingTable (GameTypeName),

它有效,所以它与 GamblingDenName 和 TableNr 相关。

我用谷歌搜索并找到了很多提示,但没有任何工作...

那么有任何想法的人吗?

提前致谢!

4

1 回答 1

0

没有GamblingTable可以满足这个 REFERENCE 的键:

  REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName),

也就是说,没有GamblingTable唯一GamblingDenName, TableNr, GameTypeName的列或前三列的键。

有一个满足密钥REFERENCES GamblingTable (GameTypeName),这就是该版本有效的原因。

(我不得不说我实际上从未见过外键被用来作为其他外键的父级,但我想它可以工作)。

于 2011-09-27T22:10:02.880 回答