zoukankan      html  css  js  c++  java
  • 20141111--SQL触发器

    ---------------------触发器-----------------------------
    --触发器本质上还是一个存储过程,trigger
    --只不过不是通过exec调用执行,而是通过增删改查数据库的操作来触发
    --针对主外键表操作时 将关联的表一起修改
    --触发器 方便记忆的命名格式:Tr(缩写)_(表名)_(增/删/改)
    --instead of 在...之前  等于 替换操作,只执行触发器里面的语句
    --for (after) 在...之后  先执行语句在执行触发器内的语句
    select *from Student--学生信息
    
    alter trigger Tr_student_delete
    on student
    instead of delete
    as
        delete from score where sno=109
        delete from Student where Sno=109
    go
    delete from Student where Sno=109
    
    --------------------参数-------------------------
    create trigger tr_dongtai
    on teacher
    instead of delete
    as
    begin
        declare @tno char(10)
        set @tno=(select tno from deleted)--deleted虚拟表,
        update teacher set tname='刘燕' where tno=@tno
    end
    go
    drop trigger tr_dongtai
    select * from Teacher
    delete from Teacher where Tno='804'
    -----------------------------------------
    create trigger tr_teacher_insert
    on teacher 
    for insert
    as
    begin
        declare @tno varchar(50)
        set @tno =(select tno from inserted)
        delete from teachar where tno=@tno
    end
    --------------------禁用,开启
    alter table teacher disable trigger all--禁用所有触发器
    alter table teacher enable trigger all--开启所有触发器
    --all改成触发器名 可以开启或禁用单个触发器
  • 相关阅读:
    vue学习之遇见的问题
    npm install 报错
    git错误
    mysql解压缩方式安装和彻底删除
    webpack 报错:Module build failed: Unknown word (1:1)
    简单分析Java的HashMap.entrySet()的实现
    spring的四种依赖注入的方式
    探秘static——类不需实例化就能用?
    【转】java并发编程:synchronized
    【转】我们为什么要使用AOP?
  • 原文地址:https://www.cnblogs.com/Tirisfal/p/4089475.html
Copyright © 2011-2022 走看看