当前位置: 代码迷 >> 报表 >> sql server2005解决思路
  详细解决方案

sql server2005解决思路

热度:202   发布时间:2016-05-05 08:07:31.0
sql server2005
怎么解决级联删除问题,如两个表一个学生表,一个班级表,如果把那个班的最后一个学生删了以后,那个班也就没有了,怎么可以实现呢??

------解决方案--------------------
这不是级连删除, 
如果一定要在数据库中实现,则可以用触发器。


当您的问题得到解答后请及时结贴.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
------解决方案--------------------
触发器。
select 1 class_id ,'一班' class_NAME INTO CLASS_TB
UNION ALL
select 2 class_id, '二班' class_NAME
UNION ALL
select 3 class_id, '三班' class_NAME

SELECT * FROM CLASS_TB

select 1 class_id , 1 STU_ID,'张三' STU_NAME INTO STU_TB
UNION ALL
select 2 class_id, 1 STU_ID,'李四' STU_NAME
UNION ALL
select 3 class_id, 1 STU_ID,'王五' STU_NAME
UNION ALL
select 3 class_id, 2 STU_ID,'王二' STU_NAME

SELECT * FROM STU_TB
SELECT * FROM CLASS_TB

create trigger STU_TB_delete on STU_TB for delete
as
 declare @i int
 SELECT @i= count (*) from STU_TB
 WHERE class_id=(select class_id from deleted)
 IF (@i=0)
 BEGIN
delete CLASS_TB where class_id = (select class_id from deleted)
END
  相关解决方案