mysql外键级联删除子表并让子表触发触发器 10
MYSQL外键级联删除子表并让子表触发触发器tableA:field:a_idPRItable:Bfield:b_idPRIb_comment并在用tableB中ALTE...
MYSQL外键级联删除子表并让子表触发触发器
table A :
field:a_id PRI
table:B
field: b_id PRI
b_comment
并在用table B中
ALTER TABLE B
ADD CONSTRAINT `fx`
FOREIGN KEY (`b_id` )
REFERENCES A (`a_id` )
ON DELETE CASCADE
ON UPDATE NO ACTION;
和
DELIMITER $$
CREATE TRIGGER `del_b`
AFTER DELETE ON b
FOR EACH ROW
begin
set @x=852369;
end$$
insert into A(a_id)values(1);
insert into B(b_id,b_comment) values(1,2);
delete from B where b_id = 1;
select @x;
显示@x = NULL
不好意思上面那句删除的delete from B where b_id = 1;写错了 修改成 delete from A;
但是最后面怎么就显示@x=NULL, 不是@x的值应该是852369的吗 展开
table A :
field:a_id PRI
table:B
field: b_id PRI
b_comment
并在用table B中
ALTER TABLE B
ADD CONSTRAINT `fx`
FOREIGN KEY (`b_id` )
REFERENCES A (`a_id` )
ON DELETE CASCADE
ON UPDATE NO ACTION;
和
DELIMITER $$
CREATE TRIGGER `del_b`
AFTER DELETE ON b
FOR EACH ROW
begin
set @x=852369;
end$$
insert into A(a_id)values(1);
insert into B(b_id,b_comment) values(1,2);
delete from B where b_id = 1;
select @x;
显示@x = NULL
不好意思上面那句删除的delete from B where b_id = 1;写错了 修改成 delete from A;
但是最后面怎么就显示@x=NULL, 不是@x的值应该是852369的吗 展开
展开全部
/**********************************************/
/*触发器:删除岗位时删除 岗位考试 能力项 **********/
/********************************************/
drop trigger del_Position_all
go
create trigger del_Position_all
on t_Position
for delete
as
declare @a char(10)
declare @guid char(40)
select @a=po_id,@guid=po_ca_guid from deleted
delete from t_PositionToCourse where pc_po_id=@a and pc_ca_guid=@guid /*删除岗位与课程对应关系*/
delete from t_spreadword where sw_st_id in(select st_id from t_spreadType where st_s_id in (select s_id from t_spread where s_po_id=@a and s_ca_guid=@guid))--先删除文档
delete from t_spreadType where st_s_id in (select s_id from t_spread where s_po_id=@a and s_ca_guid=@guid)--删步聚
delete from t_spread where s_po_id=@a and s_ca_guid=@guid --删传承
go
不要用外键。还在用!!
/*触发器:删除岗位时删除 岗位考试 能力项 **********/
/********************************************/
drop trigger del_Position_all
go
create trigger del_Position_all
on t_Position
for delete
as
declare @a char(10)
declare @guid char(40)
select @a=po_id,@guid=po_ca_guid from deleted
delete from t_PositionToCourse where pc_po_id=@a and pc_ca_guid=@guid /*删除岗位与课程对应关系*/
delete from t_spreadword where sw_st_id in(select st_id from t_spreadType where st_s_id in (select s_id from t_spread where s_po_id=@a and s_ca_guid=@guid))--先删除文档
delete from t_spreadType where st_s_id in (select s_id from t_spread where s_po_id=@a and s_ca_guid=@guid)--删步聚
delete from t_spread where s_po_id=@a and s_ca_guid=@guid --删传承
go
不要用外键。还在用!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询