SQL怎么用alter加入级联删除
已经定义好的表createtableSC(Snochar(9),Cnochar(4),Gradesmallint,PRIMARYKEY(Sno,Cno),FOREIGNK...
已经定义好的表create table SC
(
Sno char(9),
Cno char(4),
Grade smallint,
PRIMARY KEY (Sno,Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno),
FOREIGN KEY (Cno) REFERENCES Course(Cno)
)
现在想当删除Student中的某一项是同时把SC中的也删除。就是要达到
create table SC
(
Sno char(9),
Cno char(4),
Grade smallint,
PRIMARY KEY (Sno,Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno)
on delete cascade,
FOREIGN KEY (Cno) REFERENCES Course(Cno)
)
的效果,怎么用ALTER实现。 展开
(
Sno char(9),
Cno char(4),
Grade smallint,
PRIMARY KEY (Sno,Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno),
FOREIGN KEY (Cno) REFERENCES Course(Cno)
)
现在想当删除Student中的某一项是同时把SC中的也删除。就是要达到
create table SC
(
Sno char(9),
Cno char(4),
Grade smallint,
PRIMARY KEY (Sno,Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno)
on delete cascade,
FOREIGN KEY (Cno) REFERENCES Course(Cno)
)
的效果,怎么用ALTER实现。 展开
展开全部
建删除触发器 给你个例子
CREATE TRIGGER YHDZD_D ON YHDZD FOR DELETE AS
BEGIN
DECLARE @TMPCOUNT INT
SELECT @TMPCOUNT=@@ROWCOUNT
IF @@NESTLEVEL<>1
RETURN
IF Exists (select 1 from deleted where QZRQ<>null)
BEGIN
ROLLBACK TRANSACTION
ROLLBACK
TRIGGER WITH RAISERROR 17006 "已对帐的记录不能删芹亮除!"
RETURN
END
IF (@TMPCOUNT > 1) AND (@TMPCOUNT <嫌带宽> (select count(*) from deleted where QZRQ <>null))
BEGIN
ROLLBACK TRANSACTION
ROLLBACK TRIGGER WITH RAISERROR 17006
"有行桥未对帐的记录存在!"
END
ELSE IF @TMPCOUNT > 1
BEGIN
RETURN
END
DECLARE @KMID Int
DECLARE @RQ smalldatetime
DECLARE @INX numeric(9,0)
DECLARE @DiffYE money
DECLARE curYHDZD_D CURSOR FOR
SELECT KMID,
RQ,INX,JFJE-DFJE FROM deleted
WHERE QZRQ=null
ORDER BY RQ,INX
OPEN curYHDZD_D
FETCH curYHDZD_D INTO @KMID,@RQ,@INX,@DiffYE
WHILE (@@sqlstatus = 0)
BEGIN
UPDATE YHDZD SET YE=YE-@DiffYE
WHERE KMID=@KMID AND (RQ>@RQ OR
RQ=@RQ AND INX>@INX)
FETCH curYHDZD_D INTO @KMID,@RQ,@INX,@DiffYE
END
CLOSE curYHDZD_D
DEALLOCATE CURSOR curYHDZD_D
END
CREATE TRIGGER YHDZD_D ON YHDZD FOR DELETE AS
BEGIN
DECLARE @TMPCOUNT INT
SELECT @TMPCOUNT=@@ROWCOUNT
IF @@NESTLEVEL<>1
RETURN
IF Exists (select 1 from deleted where QZRQ<>null)
BEGIN
ROLLBACK TRANSACTION
ROLLBACK
TRIGGER WITH RAISERROR 17006 "已对帐的记录不能删芹亮除!"
RETURN
END
IF (@TMPCOUNT > 1) AND (@TMPCOUNT <嫌带宽> (select count(*) from deleted where QZRQ <>null))
BEGIN
ROLLBACK TRANSACTION
ROLLBACK TRIGGER WITH RAISERROR 17006
"有行桥未对帐的记录存在!"
END
ELSE IF @TMPCOUNT > 1
BEGIN
RETURN
END
DECLARE @KMID Int
DECLARE @RQ smalldatetime
DECLARE @INX numeric(9,0)
DECLARE @DiffYE money
DECLARE curYHDZD_D CURSOR FOR
SELECT KMID,
RQ,INX,JFJE-DFJE FROM deleted
WHERE QZRQ=null
ORDER BY RQ,INX
OPEN curYHDZD_D
FETCH curYHDZD_D INTO @KMID,@RQ,@INX,@DiffYE
WHILE (@@sqlstatus = 0)
BEGIN
UPDATE YHDZD SET YE=YE-@DiffYE
WHERE KMID=@KMID AND (RQ>@RQ OR
RQ=@RQ AND INX>@INX)
FETCH curYHDZD_D INTO @KMID,@RQ,@INX,@DiffYE
END
CLOSE curYHDZD_D
DEALLOCATE CURSOR curYHDZD_D
END
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在sql server建议灶陆李悉答用触发器级隐迟联删除
可参考
http://hi.baidu.com/java_road/blog/item/a560bdf056dbb9aba50f5236.html
可参考
http://hi.baidu.com/java_road/blog/item/a560bdf056dbb9aba50f5236.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |