My 'ON DELETE CASCADE ' does not work

后端 未结 3 1505
暗喜
暗喜 2021-01-23 09:28

I have got 3 tables: lt_hdefaults, lt_hperiods and lt_hrules. In lt_hdefaults, there is one row for a property for a particul

相关标签:
3条回答
  • 2021-01-23 09:47

    Something like this works..as an example:

    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    
    CREATE TABLE IF NOT EXISTS lt_hperiods ( 
        period_id          int(255)      NOT NULL,  
        lt_id              int(255)      NOT NULL,
        lt_year            year(4)       NOT NULL DEFAULT '0000',  
        period_name        varchar(45)   NOT NULL,  
        min_stay           int(10)       NOT NULL,
        max_stay           int(10)       NOT NULL,   
        fromDate           date          NOT NULL,
        toDate             date          NOT NULL,  
        weekly_rate        float(10,2)   DEFAULT NULL, 
        nightly_rate       float(10,2)   NOT NULL,  
        arriveDepartDays   varchar(150)  DEFAULT 'sunday,monday,tuesday,wednesday,thursday,friday,saturday',
        noArriveDepartDays varchar(150)  DEFAULT NULL,
    
        PRIMARY KEY (period_id),
        INDEX (lt_id),
        INDEX (lt_year),
        CONSTRAINT FOREIGN KEY (`lt_id`, `lt_year`) 
        REFERENCES lt_hdefaults(`lt_id`, `lt_year`) ON DELETE CASCADE
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=107;
    
    
    
    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    
    CREATE TABLE IF NOT EXISTS lt_hrules (
        period_id      int(255)     NOT NULL,
        lt_id          int(255)     NOT NULL,
        lt_year        year(4)      NOT NULL DEFAULT '0000',
        rule_name      varchar(45)  NOT NULL,
        night_of_stay  int(10)      NOT NULL,
        fixed_rate     float(10,2)  NOT NULL,
    
        PRIMARY KEY (period_id,lt_id,night_of_stay),
        INDEX(period_id),
        CONSTRAINT FOREIGN KEY (`period_id`)
        REFERENCES lt_hperiods(`period_id`) ON DELETE CASCADE
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
    
    0 讨论(0)
  • 2021-01-23 09:48

    for Foreign key referencing use innoDB

    0 讨论(0)
  • 2021-01-23 10:04

    ENGINE=MyISAM <-- MyISAM doesn't support any kind of foreign keys. Use InnoDB instead.

    0 讨论(0)
提交回复
热议问题