数据库触发器(Mysql&&SQLserver)

MYSQL:
引用内容 引用内容
Create TABLE `area` (
  `area_id` smallint(5) unsigned NOT NULL DEFAULT '0',
  `area_nm` char(16) NOT NULL DEFAULT '',
  `area_idx` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `show_ord` tinyint(3) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`area_id`),
  KEY `area_idx` (`area_idx`),
  KEY `show_ord` (`show_ord`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;

Create TRIGGER `area_after_upd_tr` AFTER Update ON `area`
  FOR EACH ROW
BEGIN
     IF OLD.area_nm!=NEW.area_nm THEN
        Insert INTO area_1(area_id,area_nm,area_idx,show_ord)
               Select area_id,NEW.area_nm,area_idx,show_ord FROM
                      area Where area_id=OLD.area_id;
     END IF;
END;

Create TABLE `area_1` (
  `area_id` smallint(5) unsigned NOT NULL DEFAULT '0',
  `area_nm` char(16) NOT NULL DEFAULT '',
  `area_idx` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `show_ord` tinyint(3) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`area_id`),
  KEY `area_idx` (`area_idx`),
  KEY `show_ord` (`show_ord`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;


MYSQL触发器特性限制:http://dev.mysql.com/doc/refman/5.1/zh/restrictions.html#routine-restrictions

SQLServer见:

引用内容 引用内容
Create TRIGGER trigger_name
ON { table | view }
[ WITH ENCRYPTION ]
{
    { {FOR | AFTER | INSTEAD OF } { [ Insert ] [ , ] [ Update ] [ , ] [ Delete ] }
        [ WITH APPEND ]
        [ NOT FOR REPLICATION ]
        AS
        [ { IF Update ( column )
            [ { AND | or } Update ( column ) ]
                [ ...n ]
        | IF ( COLUMNS_UpdateD ( ) { bitwise_operator } updated_bitmask )
                { comparison_operator } column_bitmask [ ...n ]
        } ]
        sql_statement [...n ]
    }
}

很多例子见参考地址
http://msdn.microsoft.com/en-us/library/aa258254(SQL.80).aspx

文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 79
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭