sqlserver级联删除

wccwccwcw 2012-05-24 11:05:52
sqlserver怎么实现级联删除啊 求指教 最好来个通俗易懂的例子

我是弄个论坛 在删主题时报错 不能带着回复一起删掉

拜托了
...全文
228 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Felixzhaowenzhong 2012-05-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
SQL code


--级联删除
create table ta(id int not null primary key)
insert ta
select 1

create table tb(id int foreign key references ta(id) on delete cascade)
insert tb
select 1

select * from……
[/Quote]

++
gogodiy 2012-05-25
  • 打赏
  • 举报
回复
如果表已经建立好了,那么右键表,选择修改,在GUI界面中,右键任一字段,弹出的GUI界面中,选择关系,会自动显示外关键字,然后在右边的INSERT和UPDATE规范点击前面的+,设置更新规则、删除规则即可。
  • 打赏
  • 举报
回复

--->>>>TravyLee生成测试数据
if object_id('test')is not null
drop table test
go
create table test(
id int primary key,
value varchar(10)
)
go
insert test(id,value)
select 1,'test1' union all
select 2,'test2' union all
select 3,'test3' union all
select 4,'test4' union all
select 5,'test5'
go


if object_id('tbl')is not null
drop table tbl
go
create table tbl(
id int foreign key references test(id)
on delete cascade --指定级联删除
on update cascade, --指定级联更新
value varchar(5)
)
go
insert tbl
select 1,'true' union all
select 2,'false' union all
select 3,'false' union all
select 4,'true' union all
select 5,'false'
go

delete from test where value='test2'


select * from test

/*
test数据
-------------------------
id value
1 test1
3 test3
4 test4
5 test5
-------------------------
tbl数据
--------------------------
id value
1 true
3 false
4 true
5 false
*/
koumingjie 2012-05-25
  • 打赏
  • 举报
回复

--级联删除
create table ta(id int not null primary key)
insert ta
select 1

create table tb(id int foreign key references ta(id) on delete cascade)
insert tb
select 1

select * from ta
select * from tb
delete ta
select * from ta
select * from tb

drop table tb
drop table ta


人生无悔 2012-05-24
  • 打赏
  • 举报
回复

if exists (select 1 from sysobjects where name='FK_Aid_B')
alter table B drop constraint [FK_Aid_B]
go
if object_id('A') is not null drop table A
if object_id('B') is not null drop table B
create table A(
id int not null primary key
)
go
create table B(
id int not null,
AId int not null
)
go

alter table B add constraint [PK_B] primary key clustered(id,AId) on [PRIMARY]
go
alter table B add constraint [FK_Aid_B] foreign key(AId) references A(id)
on delete cascade
go

insert into A select 1 union all select 2

insert into B select 1,1 union all select 2,2
--删除前
select * from A
select * from B
delete A where id=1
--删除后
select * from A
select * from B
go
以前用Sql Server只会对图形界面进行操作,现在发现自己的Sql语言功底是越来越差了,例如如何为两个表添加关联,让他们级联更新和级联 删除。 到晚上查了一下,发现可以用两种办法 触发器方式: create trigger trg_A on A for update,delete as begin if exists(select 1 from inserted) update B set Name=(select Name from inserted) where Name=(select Name from deleted) else delete B where Name=(select Name from deleted) end go 级联更新和级联删除方式: ALTER TABLE [dbo].[T_USERGROUP] ADD CONSTRAINT [FK_T_USERGROUP_T_ACCTTEMPLATE] FOREIGN KEY ( [ATNAME] ) REFERENCES [dbo].[T_ACCTTEMPLATE] ( [ATNAME] ) ON UPDATE CASCADE ON DELETE CASCADE ---------------------------------------------------------------------------------------------------------------------------- 如果,现在有两张表A(id,xx.xx...),C(id,Cid,xx,xx...),预实现C中的id与A 中id级联删除、级联更新/数据同步,可有两种实现方式: 一. 利用sql server自带的级联删除、级联更新功能,即其外键约束途径 alter table dbo.C add constraint FK_C_A_AID foreign key(AID) references dbo.A(AID) on delete cascade on update cascade go alter table dbo.C add constraint FK_C_B_BID foreign key(BID) references dbo.B(BID) on delete cascade on update cascade go 级联更新和级联删除方式: Alter TABLE [dbo].[T_USERGROUP] ADD CONSTRAINT [FK_T_USERGROUP_T_ACCTTEMPLATE] FOREIGN KEY ( [ATNAME] ) REFERENCES [dbo].[T_ACCTTEMPLATE] ( [ATNAME] ) ON Update CASCADE ON Delete CASCADE

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧