要设计采购单的表,一共二个表,主表(ta),分录表(tb)主表与分录表以内码的形式相关连CREATE TABLE ta( FInterID varchar(40),FOrderNumber varchar(40))CREATE TABLE tb( FInterID varchar(40),FNumber varchar(40))如何建立表之间的关联,当在主表ta删除某一FInterID时,从表tb中的FInterID这么记录也被删除掉而在从表tb删除某一FInterID时,却不删除主表ta中的FInterID求解……
WHERE FInterID='00001'则不用DELETE FROM tb表就会自动删除从表tb中的00001编号的一行数据
as
delete a from tb a where exists(select 1 from deleted b where a.FInterID=b.FInterID)
而在从表tb删除某一FInterID时,却不删除主表ta中的FInterID自己弄的,直接外键关联,勾上级联删除即可
自己开的,直接写触发器也可K3、U8什么的,不是很多层啥,直接业务规则层上代码检查一致性不就搞定
是的。use test
GO
Set Nocount Onif object_id('tb') is Not Null
Drop Table tb
Goif object_id('ta') is Not Null
Drop Table ta
Go
create table ta
(
FInterID int ,constraint PK_ta primary Key(FInterID Asc)
)
create table tb
(
FInterID int Not Null,
Constraint FK_tb_FInterID Foreign Key(FInterID) References ta(FInterID) On DELETE Cascade
)
GO
Insert Into ta(FInterID) Values(1)
Insert Into ta(FInterID) Values(2)
Insert Into ta(FInterID) Values(3)
Insert Into ta(FInterID) Values(4)
GO
Insert Into tb(FInterID) Values(1)
Insert Into tb(FInterID) Values(3)
Insert Into tb(FInterID) Values(4)
Go
Set Nocount Off
Go
--删除ta表中FInterID=3的记录,再看tb表中FInterID=3的记录是否还存在
Delete ta Where FInterID=3
Go
Select * From tb --tb表中FInterID=3的记录已给删除
/*
FInterID
-----------
1
4
*/
Go--删除tb表中FInterID=4的记录,再看ta表FInterID=4的是否给删除
Delete tb Where FInterID=4
Go
Select * From ta where FInterID=4 --ta表中FInterID=4的记录没有给删除/*
FInterID
-----------
4
*/