(错误:150“外键约束形成错误”)

时间:2016-01-10 17:54:08

标签: mysql

以下是我正在尝试实施的数据库: -

CREATE DATABASE Cinema;
USE Cinema;

-- Person Supertype
CREATE TABLE Person (
    ID INT(2) UNSIGNED NOT NULL,
    Name VARCHAR(40) NOT NULL,
    Age TINYINT(120) UNSIGNED NOT NULL,
    PhoneNumber VARCHAR(20),
    PRIMARY KEY(ID)
);

-- Customer Subtype
CREATE TABLE Customer (
    Customer_ID INT(2) UNSIGNED NOT NULL,
    Type enum('Member','Adult','Student','Child'),
    PRIMARY KEY(Customer_ID),
    FOREIGN KEY(Customer_ID) REFERENCES Person(ID)
);

-- Staff Subtype
CREATE TABLE Staff (
    Staff_ID INT(2) UNSIGNED NOT NULL,
    SalaryPerMonth DECIMAL(6,2),
    PRIMARY KEY(Staff_ID),
    FOREIGN KEY(Staff_ID) REFERENCES Person(ID)
);

--Booking enitity
CREATE TABLE Booking (
    Booking_ID INT(3) NOT NULL,
    Price DECIMAL(3,2),
    TicketQuantity INT(100),
    PRIMARY KEY(Booking_ID)
);

--Customer_Booking relationship
CREATE TABLE Customer_Booking (
    Customer_ID INT(2) UNSIGNED NOT NULL,
    Booking_ID INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY(Customer_ID, Booking_ID),
    FOREIGN KEY(Customer_ID) REFERENCES Customer(Customer_ID),
    FOREIGN KEY(Booking_ID) REFERENCES Booking(Booking_ID)
);

输入上表时出现错误。

1 个答案:

答案 0 :(得分:1)

customer_booking 预订表格中的booking_id列不相等。一个是 UNSIGNED ,但其他人不是。因此,您可以使用下表创建脚本“ customer_booking ”表

CREATE TABLE Customer_Booking (
    Customer_ID INT(2) UNSIGNED NOT NULL,
    Booking_ID INT(3)  NOT NULL,
    PRIMARY KEY(Customer_ID, Booking_ID),
    FOREIGN KEY(Customer_ID) REFERENCES Customer(Customer_ID),
    FOREIGN KEY(Booking_ID) REFERENCES Booking(Booking_ID)
);

或者

如果您希望 booking_id 未签名,请在预订表创建脚本中进行更正。