sql数据库外键问题

发布于 2024-12-07 00:29:37 字数 2919 浏览 3 评论 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有关。

我用谷歌搜索了一下,发现了很多提示,但还没有任何效果……

所以有人有任何想法吗?

提前致谢!

I have a school assignment that that is giving me and my friends headaches... So this is my queries that i have executed, one at a time ofc...

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)
);

But this last query does not work:

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)
);

It gives me this error:
"Error Code: 1005. Can't create table 'gamblingsociety.gamblinginstance' (errno: 150)"

if I replace this:

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

with this:

FOREIGN KEY (GameTypeName) REFERENCES GamblingTable (GameTypeName),

It works, so it's something with the GamblingDenName and TableNr.

And i have google it and found a lot of tips, but nothing yeat have worked...

So anyone with any idea?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒时光 2024-12-14 00:29:37

GamblingTable 中没有可以满足此参考的键:

  REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName),

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

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

(我不得不说,我从未真正见过外键被用来作为其他外键的父级,但我想它是有效的)。

There is no key in GamblingTable that can satisfy this REFERENCE:

  REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName),

That is, there is no key in GamblingTable for which GamblingDenName, TableNr, GameTypeName are the only columns, or the first three columns.

There is a key that satisfies REFERENCES GamblingTable (GameTypeName), which is why that version works.

(I have to say that I've never actually seen FOREIGN KEYs being used to parent other FOREIGN KEYs, but I guess it works).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文