设置MySQL触发器 [英] Setting Up MySQL Triggers

查看:77
本文介绍了设置MySQL触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在听说触发器,并且有几个问题.
什么是触发器?
如何设置它们?
除了典型的SQL内容外,是否还应采取其他预防措施?

I've been hearing about triggers, and I have a few questions.
What are triggers?
How do I set them up?
Are there any precautions, aside from typical SQL stuff, that should be taken?

推荐答案

通过触发器,您可以在发生某些事件(例如,插入到表中)时在数据库中执行功能.

Triggers allow you to perform a function in the database as certain events happen (eg, an insert into a table).

我不能特别评论mysql.

I can't comment on mysql specifically.

注意事项:触发器可能非常诱人,当您首次使用它们时,它们似乎是解决各种问题的灵丹妙药.但是,它们使神奇"的事情发生了,如果您不知道数据库由内而外的话,那么看起来好像真的很奇怪(例如,向其他表中的插入,输入数据的更改等).在将事物实现为触发器之前,我会认真考虑改为在架构周围(最好在数据库中,但如果不能,则在外部)强制使用API​​.

Precaution: Triggers can be very alluring, when you first start using them they seem like a magic bullet to all kinds of problems. But, they make "magic" stuff happen, if you don't know the database inside out, it can seem like really strange things happen (such as inserts into other tables, input data changing, etc). Before implementing things as a trigger I'd seriously consider instead enforcing the use of an API around the schema (preferably in the database, but outside if you can't).

有些事情我仍然会使用触发器

Some things I'd still use triggers for

  • 跟踪"date_created"和"date_last_edited"字段
  • 插入"ID"(在oracle中没有自动ID字段)
  • 保留更改历史记录

您不想使用触发器的东西

Things you wouldn't want to use triggers for

  • 业务规则/逻辑
  • 连接数据库外部的任何内容(例如,Web服务调用)
  • 访问控制
  • 任何非事务性的内容(您在触发器中所做的任何事情都必须能够回滚该事务)

这篇关于设置MySQL触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆