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

[{'answer_id': 47391, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 0, 'selected': False, 'text': '<p>A trigger is a named database object that is associated with a table and that is activated when a particular event occurs for the table.<br />\n<br />To create a trigger:<br /></p>\n\n<pre><code>CREATE TRIGGER triggerName [BEFORE|AFTER] [INSERT|UPDATE|DELETE|REPLACE] ON tableName FOR EACH ROW SET stuffToDoHERE;\n</code></pre>\n\n<p><br />Even though I answered this part the other question still stands.</p>\n'}, {'answer_id': 47392, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 1, 'selected': False, 'text': '<p>From <a href="http://dev.mysql.com/doc/refman/5.0/en/triggers.html" rel="nofollow noreferrer">dev.mysql.com</a>, a trigger is </p>\n\n<blockquote>\n  <p>...a named database object that is\n  associated with a table and that is\n  activated when a particular event\n  occurs for the table.</p>\n</blockquote>\n\n<p>The syntax to create them <a href="http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html" rel="nofollow noreferrer">is also documented at that site</a>.</p>\n\n<p>Briefly,</p>\n\n<pre><code>CREATE\n    [DEFINER = { user | CURRENT_USER }]\n    TRIGGER trigger_name trigger_time trigger_event\n    ON tbl_name FOR EACH ROW trigger_stmt\n</code></pre>\n\n<p>And they provide an example:</p>\n\n<pre><code>CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));\nCREATE TRIGGER ins_sum BEFORE INSERT ON account FOR EACH ROW SET @sum = @sum + NEW.amount;\n</code></pre>\n\n<p>You at least need to abide by <a href="http://dev.mysql.com/doc/refman/5.0/en/routine-restrictions.html" rel="nofollow noreferrer">all the restrictions on stored functions</a>.\nYou won\'t be able to lock tables, alter views, or modify the table that triggered the trigger. Also triggers <a href="http://dev.mysql.com/doc/refman/5.0/en/stored-procedure-logging.html" rel="nofollow noreferrer">may cause replication problems</a>.</p>\n'}, {'answer_id': 47393, 'author': 'Matthew Watson', 'author_id': 3839, 'author_profile': 'https://Stackoverflow.com/users/3839', 'pm_score': 5, 'selected': True, 'text': '<p>Triggers allow you to perform a function in the database as certain events happen (eg, an insert into a table). </p>\n\n<p>I can\'t comment on mysql specifically.</p>\n\n<p>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).</p>\n\n<p>Some things I\'d still use triggers for</p>\n\n<ul>\n<li>Keeping track of "date_created" and "date_last_edited" fields</li>\n<li>Inserting "ID"\'s (in oracle, where there is no auto id field)</li>\n<li>Keeping change history</li>\n</ul>\n\n<p>Things you wouldn\'t want to use triggers for</p>\n\n<ul>\n<li>business rules/logic</li>\n<li>anything which connects outside of the database (eg a webservice call)</li>\n<li>Access control</li>\n<li>Anything which isn\'t transactional ( anything you do in the trigger MUST be able to rollback with the transaction )</li>\n</ul>\n'}, {'answer_id': 16703058, 'author': 'Federico Razzoli', 'author_id': 1090198, 'author_profile': 'https://Stackoverflow.com/users/1090198', 'pm_score': 0, 'selected': False, 'text': "<p>This question is old and other answers are very good, but since the user asked about precautions that should be taken, I want to add something:</p>\n\n<ul>\n<li>If you use replication in a complex environment, don't make a massive use of Triggers, and don't call stored procedures from triggers.</li>\n<li>Triggers are slow in MySQL.</li>\n<li>You can't use some SQL statements within triggers. And some statements are permitted but should be avoided, like LOCK. The general rule is: if you don't fully understand the implications of what you are doing, you shouldn't do it.</li>\n<li>Triggers can cause endless loops, so be careful.</li>\n</ul>\n"}]
['https://Stackoverflow.com/questions/47387', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/115/']