我正在尝试使用带有 mutable FK 的模式创建 table(订单)

I am trying to create a table (Order) using schema with mutable FK

Region (RegionCode, RegionName)

Customer (CustCode, CustName, CustAdress, CustBalance, RegionCode)

EmployeeType (EmployeeType, HourlyPayRate,)

Employee (EmployeeNo, EmployeeName, EmployeeAddress, RegionCode, EmployeeType)

Product (ProductCode, ProductName, UnitPrice, StockOnHand)

Order (OrderNo, CustCode, ProductCode, QtyOrdered, EmployeeNo)

The primary keys are in Bold.

A Region is uniquely identified by a RegionCode. Each Customer is uniquely identified by a CustCode. Each Customer can order more than one Product and each Product can have many Customers. Each Order is dealt with by an employee and an Employee can work with more than one Order.

我很困惑,因为我不明白加粗的语句是否暗示它是一个复合键 (OrderNo,EmployeeNo),或者这是否是问题中的一个干扰?

以下是我如何从 Order 架构

创建 table
Create Table Order{
     OrderNo INT (11) AUTO INCREMENT NOT NULL PRIMARY KEY,
     FOREIGN KEY CustCode REFERENCES Customer(CustCode)
     ON DELETE CASCADE
     ON UPDATE CASCADE,
     FOREIGN KEY ProductCode REFERENCES Product(ProductCode)
     ON DELETE CASCADE
     ON UPDATE CASCADE,
     QtyOrdered int (100),
     FOREIGN KEY EmployeeNo REFERENCES Employee(EmployeeNo)
     ON DELETE CASCADE
     ON UPDATE CASCADE
}

我想知道这是否是解决此问题的正确方法?

你对问题的理解还可以。您的老师希望您在 Order table 中包含引用 table、CustomerEmployeeProduct.[=19 的列=]

但是您的代码有缺陷。您需要先声明列,然后再声明外键。声明外键不会自动创建列。

其他问题:

  • table 声明用括号 () 括起来,而不是 {}

  • Order 是 MySQL(以及所有其他 RDBMS)中的保留字;您需要用反引号将其括起来(更明智的解决方案是为此选择另一个名称 table)

考虑:

Create Table `Order`(
    OrderNo INT (11) AUTO INCREMENT NOT NULL PRIMARY KEY,
    CustCode INT,
    ProductCode INT,
    QtyOrdered INT(100),
    EmployeeNo INT,
    FOREIGN KEY CustCode REFERENCES Customer(CustCode) 
        ON DELETE CASCADE ON UPDATE CASCADE,
    FOREIGN KEY ProductCode REFERENCES Product(ProductCode) 
        ON DELETE CASCADE ON UPDATE CASCADE,
    FOREIGN KEY EmployeeNo REFERENCES Employee(EmployeeNo) 
        ON DELETE CASCADE ON UPDATE CASCADE
);

旁注:外键列必须与引用列具有相同的数据类型;我假设所有这些列都为 INT,您可能希望根据您的真实架构对其进行修复。