MySql数据库触发器学年论文

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

数学与计算科学学院学年论文(课程设计)

题目触发器在MySQL数据库中的应用

姓名吴碧青

学号**********

专业年级2013级信息与计算科学

指导教师李玉梅副教授

2015年12 月10 日

学年论文(课程设计)成绩评定表

触发器在MySQL数据库中的应用

摘要

触发器是SQL server提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表的事件相关的特殊的存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发。我们这篇文章中提到的是三个基本触发器的相关应用,当我们对一个表进行操作插入、更新、删除时来然后去激活它执行。

在本文中阐述了三种常见的触发器:insert触发器、delete触发器、update 触发器,而这三种触发器是在MySQL数据库中的应用的比较广泛,实用性也很强。这三种触发器都可以查询其他表,也可以包含复杂SQL语句。它们主要用于强制服从复杂的业务规则或要求。我们可以利用这三个触发器来改变一些数据库表中的数据,比如:当教师的底薪升高了,而我们需要改变教师表和工资表中底薪我们就可以用触发器来实现同时把两个表中的信息修改到自己想要的值。

关键词

触发器、insert触发器、delete触发器、update触发器

Application of trigger in MySQL database

Abstract

The trigger is a way to ensure data integrity by server SQL, which is a special storage process associated with a table event. It is not a program call, nor a manual, but an event. The relevant application of the three basic triggers, when we use a table to insert, update, delete, and then activate it.

In this paper, three kinds of common triggers are described in this paper: insert, delete, update, and these three kinds of flip flops are widely used in MySQL database. These three triggers can query other tables, also can contain complex SQL statements. They are mainly used to enforce compliance with complex business rules or requirements. We can change some database table data, using the three triggers such as: when the teacher's salary has increased, and we need to change the teachers' salary and wage table table we can use triggers to implement the two tables in the information you want to modify the value.

Key words

Trigger、insert Trigger、delete Trigger、update Trigger

1前言

触发器是一个被指定关联到一个表的数据库对象,当对一个表的特定事件出现时,它将被激活。我们将要提到的触发器是与表有关的命名数据库对象,当表上出现特定事件时,将激活该对象。我们根据触发的事件,可分为insert, delete 和update等等触发器。下文我们将建立一个insert触发器,从insert触发器中调用SQL语句,然后在SQL语句中去执行相关的语句,从而实现insert触发器的功能,同时将从触发器的创建到执行再到删除来阐述这三个触发器的实际应用。2触发器的创建和查看

2.1触发器创建的语法格式

Create trigger< trigger_name >

{ Before | After} < trigger_event> on < table_name >

For each row

< trigger _statement >

说明:

trigger_name:触发器的名字,是触发器在改数据库中的唯一也是必须要具备的民称,在某个特定的数据库中建立的时候一定要加上它的数据库名称。

trigger_event;所需要触发的事件,一般指的是插入、删除、更新等语句。

trigger _statement:触发动作,包含了触发器需要执行的语句,当然如果需要使用多个语句的时候就需要用到begin…and等复合语句。

table_name:与触发器触发相关的表,但是同一个表中不能出现多个触发器。

例2.1:创建一个表tb1,其中只有一列sjk,在表上创建一个触发器,每次插入操作时,将变量str的值设定为“trigger is ture”。

Create table tbl (a integer);

Create trigger tbl_insert after intsert on table tbl for each row

Set @str =’trigger is ture’;

向tbl中插入一行数据:

Insert into tbl values(5);

查看str的值:

Select @str;图2.1 执行触发器后的结果图

相关文档
最新文档