更新级联在子表中未生效
我有一个父表,其创建语句如下:
Create Table "GL_Cash_Book"
(
"y_trans_code" Integer Not Null,
"trans_date" Integer Default current_timestamp,
"receipt_no" Text,
"amount_dr" Real,
Primary Key("y_trans_code")
)
我已经把它与一个子表连接起来,创建语句如下:
Create Table "GL_income_account"
(
"trans_id" Integer Not Null,
"trans_date" Integer Default current_timestamp,
"receipt_no" Text,
"amount_cr" Real,
Primary Key("trans_id" Autoincrement),
Foreign Key("trans_code") References "GL_Cash_Book"("y_trans_code")
On Update Cascade
)
我还设置了我的 PRAGMA 语法,以便在我想执行更新语句时随时运行。
现在我的挑战和想法是,当我在父表中对收据号、日期或金额中的任意一项进行修改时,使用 y_trans_code 作为我的 where 语句,这应该会影响在子表中具有相同 trans_code 的整行记录。若我改变父表中的金额,实际更新的只有父表,子表并未更新。
有意思的是,当我只更新父表中的 y_trans_code 时,它会级联影响子表中的 trans_code。
我想要做的是,只要我对父表的任意列进行修改,子表的整行也应随之更新。
解决方案
我的挑战和想法是,每当我在我的PT中使用y_trans_code作为where条件,对收据号、日期或金额进行修改时,这应该会影响在CT中具有相同trans_code的整行。只是PT被更新,若我改变PT的金额,CT就不会更新。
这根本就不是级联外键的工作方式。级联只影响被引用的列,不会修改被引用表中的其他列。
说实话,你的表设计是非规范化的。父表中的列本来不应该在子表中重复出现,因为这会破坏第三范式。所以它应该像这样:
create table "GL_Cash_Book" (
"y_trans_code" Integer not null,
"trans_date" Integer default current_timestamp,
"receipt_no" Text not null,
"amount_dr" Real not null,
primary key("y_trans_code")
);
create table "GL_income_account" (
"trans_id" Integer not null,
"trans_code" Integer not null,
"amount_cr" Real not null,
primary key("trans_id" Autoincrement),
foreign key("trans_code") references "GL_Cash_Book"("y_trans_code") on update cascade
);
although说实话,这些表到底代表什么也并不清楚。它们是针对那些付款的支付和退款吗?如果没有涉及账户,子表的名称中为什么会有 account?相关的账户信息到底在哪里?
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。