unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    Label1: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    DataSource1: TDataSource;
    Query1: TQuery;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  try
    with Query1 do
    begin
      close;
      SQL.clear;
      SQL.Add('create trigger test on 学生表');
      SQL.Add('instead of insert as drop table 教师表');
      ExecSQL;
    end;
    Application.MessageBox('触发器创建成功!','提示',64);
  Except
    Application.MessageBox('触发器创建失败!','提示',64);
  end;
end;

end.

Create Trigger 的语法格式:

------------------------------------------------------------

Create Trigger Trigger_name

On {table|view}

[with Encryption]

{For|After|Instead of} {[Delete] [,] [Insert] [,] [Update]}

As

Sql_statements

-------------------------------------------------------------

Trigger_name 为要创建的触发器名称。触发器名称在数据库中必须是惟一的。

table|view 表示在哪个表或视图上建立触发器。

with Encryption 对触发器进行加密。防止将触发器作为Sql Server 复制的一部分发布。

Instead of 指定执行触发器而不是执行触发SQL语句,从而替代触发语句的操作。

{[Delete] [,] [Insert] [,] [Update]} 是指定在表或视图上执行哪些数据修改语句时将激活触发器的关键字。必须至少指定一个选项。

As 表示触发器要执行的操作。

Sql_statements 表示触发器的条件与操作。

posted on 2011-04-11 19:40  巅枫  阅读(489)  评论(0编辑  收藏  举报