sql删除关联表
1. sql 如何把两个表相关联的数据一同删除
其实你这个问题最好用数据库本身的外键解决。就是在子表建立指向父表的外键。当删除主表数据时,只要加上delete语句加上 on cascade,所有子表引用的数据就删除了。
2. SQL sever中要删除两个相关联的表该怎么进行级联删除
------解决方案-------------------------------------------------------- --1、建立一个触发器(推荐)
create trigger on p for deleteas �0�2delete from spj where pno = (select pno from deleted)go--执行删除delete from p where pname='螺丝'
--2、级联删除
alter table p add constraint pk_p_id primary key (pno)go--为tb创建外健,并指定级联删除
alter table spj add constraint fk_spj_aid foreign key (pno) references p(pno) on delete cascadego------解决方案----------------------------------------------------------推荐触发器控制,可控性比较强
SQL code --1、建立一个触发器(推荐) create trigger on p for delete as delete from spj where pno = (select pno from deleted) go --执行删除 delete from p where pname='螺丝' --2、级联删除 alter table p add constraint pk_p_id primary key (pno) go --为tb创建外健,并指定级联删除 alter table spj add constraint fk_spj_aid foreign key (pno) references p(pno) on delete cascade go
------解决方案--------------------------------------------------------建立测试数据
SQL code if object_id('dbo.SPJ') is not null drop table dbo.SPJ; go if object_id('dbo.P') is not null drop table dbo.P; go create table dbo.P ( pno int not null primary key, pname nvarchar(20) not null ); go create table dbo.SPJ ( sno int not null primary key, pno int not null ); insert into dbo.P select 1, 'type-a' union all select 2, 'type-b' union all select 3, 'type-c'; go insert into dbo.SPJ select 1, 1 union all select 2, 1 union all select 3, 1 union all select 4, 2 union all select 5, 3 union all select 6, 3; go
------解决方案--------------------------------------------------------建议用外键约束
先删除子表在删除父表
------解决方案-------------------------------------------------------- �0�2个人建议用事务处理。
3. sql server数据库怎样同时删除两张表的数据
1、打开SQL Server 2008 并连接一个数据库。
4. SQL中怎么删除2张表中有关联的数据信息
可以使用两种方式
1:删除子表数据,再删除主表数据
2:如果主细表中建立了级联删除操作,直接删除主表数据即可
5. 如何用sql清空关联表数据库表
那就删除两个表之间的外键就可以。
如下图的表:
可查询出:
select name from sys.foreign_key_columns f join sys.objects o on f.constraint_object_id=o.object_id where f.parent_object_id=object_id('表名')
删除的一般语法为:
alter table 表名 drop constraint 外键约束名;
6. sql多表关联删除
删除多表关联数据的三种方法
1、级联删除
createtablea
(
idvarchar(20)primarykey,
passwordvarchar(20)notnull
)
createtableb
(
idintidentity(1,1)primarykey,
namevarchar(50)notnull,
userIdvarchar(20),
foreignkey(userId)referencesa(id)ondeletecascade
)
表B创建了外码userId 对应A的主码ID,声明了级联删除
测试数据:
insertavalues('11','aaa')
insertavalues('23','aaa')
insertbvalues('da','11')
insertbvalues('das','11')
insertbvalues('ww','23')
删除A表内id为‘11’的数据,发现B表内userId 为“11”也被数据库自动删除了
deleteawhereid='11'
2、采用存储过程
A表:AID Aname 主健:AID
B表:BID BelongAID Bname 主健:BID,外健:BelongAID
C表:CID BelongBID Cname 主健:CID,外健:BelongBID
D表:DID BelongCID Dname 主健:DID,外健:BelongCID
其中:
A表和B表通过A.AID和B.BelongAID 创建了外健关系
B表和C表通过B.BID和C.BelongBID 创建了外健关系
C表和D表通过C.CID和D.BelongCID 创建了外健关系
3、采用触发器
删除Class表中的一条记录的同时删除该记录Class_No字段值在Student表中对应的记录。
CreateTriggerClass_delete
onClass
fordelete
as
begin
deletefromStudent
whereClass_No=(selectClass_Nofromdeleted)
end