gpt4 book ai didi

mysql - 我的 'ON DELETE CASCADE ' 不工作

转载 作者:行者123 更新时间:2023-11-29 04:04:03 25 4
gpt4 key购买 nike

我有 3 个表:lt_hdefaultslt_hperiodslt_hrules。在 lt_hdefaults 中,有一行表示特定年份的属性。在 lt_hperiods 表中,可以有多个周期,称它们为季节。在 lt_hrules 表中,每个时间段可能有多个规则。现在,我无法解决的问题是:当用户从 lt_hdefaults 中删除一条记录时,与已删除记录相关的其他数据应该从 lt_hperiodslt_hrules< 中删除表。我试图通过使用

来实现这一点
FOREIGN KEY (lt_year,lt_id) REFERENCES lt_hdefaults(lt_year,lt_id) ON DELETE CASCADE

但是,它不起作用。我知道它看起来很长,但它不是很复杂。如果有人有任何想法,我将不胜感激。我知道如何使用 mysql,但我不是这方面的专家。非常感谢。

示例如下:

CREATE TABLE IF NOT EXISTS lt_hdefaults (
lt_year year(4) NOT NULL DEFAULT '0000',
lt_id int(255) NOT NULL DEFAULT '0',
period_name varchar(45) NOT NULL DEFAULT 'Default',
min_stay int(10) NOT NULL DEFAULT '1',
max_stay int(10) NOT NULL,
weekly_rate float(10,2) NOT NULL,
nightly_rate float(10,2) NOT NULL,
PRIMARY KEY (lt_year,lt_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

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),
FOREIGN KEY (lt_year,lt_id) REFERENCES lt_hdefaults(lt_year,lt_id) ON DELETE CASCADE
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=107 ;

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),
FOREIGN KEY (period_id) REFERENCES lt_hperiods(period_id) ON DELETE CASCADE
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO lt_hdefaults (lt_year, lt_id, period_name, min_stay, max_stay, weekly_rate, nightly_rate, min_guests, max_guests, surchargeAboveGuests, chargePerGuestFee, bondFee, cleaningFee, bookingServiceFee) VALUES
(2010, 2, 'Default', 0, 0, 1200.00, 171.43, 2, 5, 6, 85.00, 1000.00, 120.00, 0.00),
(2010, 3, 'Default', 0, 0, 1300.00, 185.71, 2, 5, 6, 44.00, 1000.00, 120.00, 0.00);

INSERT INTO lt_hperiods (period_id, lt_id, lt_year, period_name, min_stay, max_stay, fromDate, toDate, weekly_rate, nightly_rate, arriveDepartDays, noArriveDepartDays) VALUES
(105, 3, 2010, 'winter', 2, 66, '2010-12-22', '2011-01-15', 1500.00, 214.29, 'Monday,Tuesday,Wednesday,Thursday', 'Friday,Saturday,Sunday'),
(106, 3, 2010, 'summer', 2, 77, '2011-01-14', '2011-01-28', 4000.00, 571.43, 'Monday,Tuesday,Wednesday,Thursday,Friday,Saturday', 'Sunday');

INSERT INTO lt_hrules (period_id, lt_id, lt_year, rule_name, night_of_stay, fixed_rate) VALUES
(106, 3, 2010, 'r2', 2, 222.00),
(106, 3, 2010, 'r1', 1, 111.00),
(105, 3, 2010, 'r2', 2, 222.00),
(105, 3, 2010, 'r1', 1, 111.00);

最佳答案

ENGINE=MyISAM <-- MyISAM 不支持任何类型的外键。请改用 InnoDB。

关于mysql - 我的 'ON DELETE CASCADE ' 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4506272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com