CASCADE删除mysql

时间:2017-04-03 09:57:03

标签: mysql cascade

我正在创建一个小型3表数据库,基本上将其用作数据缓冲区,因此它使用ENGINER = MEMORY。该数据库具有以下结构:

The overview

我对MySQL没那么多,所以我的问题是:是否可以使用CASCADE DELETE,这样当我删除MeassurementIteration时,所有与迭代相关的MeassurementSet和Meassurements都将被删除?

代码:

-- Last modification date: 2017-04-03 08:09:06.263

-- tables
-- Table: Meassurement
CREATE TABLE Meassurement (
    id int NOT NULL AUTO_INCREMENT,
    msWay varchar(75) NOT NULL,
    qosClass int NOT NULL,
    timeReceived timestamp NOT NULL,
    timeReceivedFarEnd timestamp NOT NULL,
    timeSent timestamp NOT NULL,
    timeSentFarEnd timestamp NULL,
    seq int NOT NULL,
    MeassurementSet_id int NOT NULL,
    CONSTRAINT Meassurement_pk PRIMARY KEY (id)
) ENGINE MEMORY COMMENT 'Contains data for a single meassurement';

-- Table: MeassurementIteration
CREATE TABLE MeassurementIteration (
    id int NOT NULL AUTO_INCREMENT,
    start timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    end timestamp NULL,
    active bool NOT NULL,
    CONSTRAINT MeassurementIteration_pk PRIMARY KEY (id)
);

-- Table: MeassurementSet
CREATE TABLE MeassurementSet (
    id int NOT NULL AUTO_INCREMENT,
    avgDelayRT double NULL,
    avgDelayTR double NULL,
    avgRTD double NULL,
    maxDelayRT double NULL,
    maxDelayTR double NULL,
    maxRTD double NULL,
    minRTD double NULL,
    minDelayRT double NULL,
    minDelayTR double NULL,
    qosClass int NOT NULL,
    stdDev double NULL,
    stdDevRT double NULL,
    stdDevTR double NULL,
    msWay varchar(75) NOT NULL,
    MeassurementIteration_id int NOT NULL,
    CONSTRAINT MeassurementSet_pk PRIMARY KEY (id)
) ENGINE MEMORY COMMENT 'Contains a set of meassurements specific to a qosclass and Meassurementway';

-- foreign keys
-- Reference: MeassurementSet_MeassurementIteration (table: MeassurementSet)
ALTER TABLE MeassurementSet
    ADD CONSTRAINT MeassurementSet_MeassurementIteration 
    FOREIGN KEY MeassurementSet_MeassurementIteration (MeassurementIteration_id)
        REFERENCES MeassurementIteration (id) ON DELETE CASCADE;

-- Reference: Meassurement_MeassurementSet (table: Meassurement)
ALTER TABLE Meassurement 
    ADD CONSTRAINT Meassurement_MeassurementSet
    FOREIGN KEY Meassurement_MeassurementSet (MeassurementSet_id)
        REFERENCES MeassurementSet (id) ON DELETE CASCADE;

-- End of file.

最好的问候。

2 个答案:

答案 0 :(得分:1)

您无法将ON DELETE CASCADEENGINE = MEMORY一起使用。 不支持外键。

这里有一个参考:( MySQL Doc。)[https://dev.mysql.com/doc/refman/5.6/en/memory-storage-engine.html]

 Table 15.4 MEMORY Storage Engine Features
 ---------------------------------------------------
 Foreign key support    No

你也可以检查一下,另一个类似的问题。 MySql memory engine do not check on update foreign key?

答案 1 :(得分:0)

SELECT table_nameenter code here FROM referential_constraints WHERE constraint_schema ='classicmodels'enter code here AND       referenced_table_name ='buildings'AND       delete_rule ='CASCADE'