简体   繁体   English

无法在 mySQL 中创建表

[英]Can't create a table in mySQL

I am trying a table where the primary keys are foreing keys of a table where the primary keys are foreing keys, but it doesn't create the table and I don't know why its not creating.我正在尝试一个表,其中主键是主键是外键的表的外键,但它不创建表,我不知道为什么它不创建。

Table that I want to create:我要创建的表:

CREATE TABLE proj__exames(
    floor NUMERIC(2), 
    room NUMERIC(3), 
    name VARCHAR(40), 
    PRIMARY KEY (floor, room, name),
    FOREIGN KEY (floor) REFERENCES proj__rooms (floor) ON DELETE CASCADE,
    FOREIGN KEY (room) REFERENCES proj__rooms (room) ON DELETE CASCADE,
    FOREIGN KEY (name) REFERENCES proj__rooms (name) ON DELETE CASCADE
);

Table where I am getting the foreign key from (this was created without any problems)我从中获取外键的表(创建时没有任何问题)

CREATE TABLE proj__rooms(
    floor NUMERIC(2),
    room NUMERIC(3),
    name VARCHAR(40),
    equipment_type VARCHAR(40),
    max_exam_number NUMERIC(2) NOT NULL,
-- ========
    CONSTRAINT pk_rooms
        PRIMARY KEY (name, floor, room),
-- ========
    CONSTRAINT fk_salas_nome   
        FOREIGN KEY (name)
            REFERENCES clinic (name) ON DELETE CASCADE,
-- ========
    CONSTRAINT ck_rooms_max_exam_number
        CHECK (max_exam_number >= 0) 
);

The error turned out to be missing indexes on the referenced columns.错误原来是引用列上缺少索引。 Basically you'd have to restructure the proj__rooms DDL like this:基本上你必须像这样重组proj__rooms DDL:

CREATE TABLE proj__rooms(
    floor NUMERIC(2),
    room NUMERIC(3),
    name VARCHAR(40),
    equipment_type VARCHAR(40),
    max_exam_number NUMERIC(2) NOT NULL,   
    -- ========
    CONSTRAINT pk_rooms
        PRIMARY KEY (name, floor, room),
    KEY(floor),
    KEY(room),
    KEY(name),
    -- ========
    CONSTRAINT fk_salas_nome   
        FOREIGN KEY (name)
            REFERENCES clinic (name) ON DELETE CASCADE,
    -- ========
    CONSTRAINT ck_rooms_max_exam_number
        CHECK (max_exam_number >= 0) 
);

I also suggest you to add the MySQL console error in futher questions, it could make it easier to understand the problem you're facing我还建议您在进一步的问题中添加 MySQL 控制台错误,这样可以更容易理解您面临的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM