October 12, 2008, 05:40:40
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News
: Check your Page Rank, Backlinks, or Link Popularity in our tools section.
Home
Forum
Help
Search
Directory
Calendar
Login
Register
News
Check your Page Rank, Backlinks, or Link Popularity in our tools section.
IT Resources
Recover Deleted Files
Php Hosting
Hot Wallpapers
Free Articles
Mousepad
Cheap Hosting
PDF to Doc Converter
Internet Eraser
Linux web hosting
DoFollow Sites
Movie Reviews
Webmaster's Talks !
>
Forum
>
Programming
>
Databases
>
Article: A fast introduction to Mysql triggers
Pages: [
1
]
« previous
next »
Print
Author
Topic:
Article: A fast introduction to Mysql triggers
(Read 1381 times)
0 Members and 1 Guest are viewing this topic.
TheGodFather
Administrator
Hero Member
Points: 202
Offline
Posts: 2570
Article: A fast introduction to Mysql triggers
«
on:
May 11, 2006, 11:25:17 »
I started to work on a pet project for tracking deleted/pending domains (myDomainTracker) and after starting development I was facing a big problem: How to do the data validation directly from MySQL ?
Many times I wanted to do data validation directly when inserting into a MySQL table and leave the source code of my application nice, clean and very readable, but support for that was only in Oracle and other SQL servers but not in MySQL.
Beginning with version 5 of MySQL the development team has added "triggering" to their SQL server engine, to meet the growing demand of users.
But what are triggers ?
A trigger is a database object that is associated with a table and is activated when an event occurs for that table. Can be successfully used for database data validation and any other calculations done directly on the database.
When is a trigger activated ?
A trigger is activated when changes are made on that table where it is associated with, more precisely on Insert/Update/Deleted execution statements
A simple example:
Firstly we create a test table
CREATE TABLE test (id INT, sum DECIMAL(10,2));
now we create the trigger for that table:
delimiter//
create trigger insert_trg before insert on test
for each row
begin
if new.sum<0 then
set new.sum=0;
endif;
end//
delimiter;
What is this trigger doing ?
It is a simple field validation.
When we will try to insert into the table (ex insert into test values (1,-3)) this field sum will always be greater than 0 because we change negative values to 0.
How it works:
Create trigger is creating the trigger with the name insert_trg
The keyword BEFORE indicates the trigger action time. In this case, the trigger should activate before each row inserted into the table. You can use the AFTER keyword to activate it after each inserted row in the table.
The Insert keyword stands for the action what will activate the trigger. You can also use Update or Delete.
For each row means this trigger will be activated for each inserted action.
The OLD and NEW keywords enable you to access columns in the rows affected by a trigger. (OLD and NEW are not case sensitive.) In an INSERT trigger, only NEW.col_name can be used as there is no old row. In a DELETE trigger, only OLD.col_name can be used as there is no new row. In an UPDATE trigger, you can use OLD.col_name to refer to the columns of a row before they were updated and NEW.col_name to refer to the columns of the row after they were updated.
By using the BEGIN ... END construct you can define a trigger that executes multiple statements.
How can a trigger be removed?
Just execute command: Drop trigger trigger_name.
Conclusion
I hope this little article will prove helpful for those with prior experience in MySQL but who haven't studied its new features.
Logged
█
Deleted Domains
█
Hostmaster Web Tools
█
Webmaster Web Tools
sintex
Jr. Member
Points: 7
Offline
Posts: 96
Re: Article: A fast introduction to Mysql triggers
«
Reply #1 on:
May 15, 2006, 12:40:27 »
Nice article. We can use other table fields from other tables/databases on new/old statrements ?
Logged
TheGodFather
Administrator
Hero Member
Points: 202
Offline
Posts: 2570
Re: Article: A fast introduction to Mysql triggers
«
Reply #2 on:
May 17, 2006, 11:58:17 »
No. Cannot be used.
Regards
Logged
█
Deleted Domains
█
Hostmaster Web Tools
█
Webmaster Web Tools
xulescu
Full Member
Points: 8
Offline
Posts: 245
Re: Article: A fast introduction to Mysql triggers
«
Reply #3 on:
June 06, 2006, 14:24:05 »
Good article. I learn something today
Best Regards
Logged
Tracking Deleted Domains, Link Popularity and more ...
niceboy
Jr. Member
Points: 0
Offline
Posts: 14
Re: Article: A fast introduction to Mysql triggers
«
Reply #4 on:
October 12, 2006, 17:22:18 »
very handy friend. i really wanted to know abt triggers.
Logged
Web Directory of Validated Online Resources
Why spend money on hosting? Chose your free hosting provider at :
Free Hosting World
Pages: [
1
]
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
Web Design and Graphics
-----------------------------
=> General
=> Web Design Discussion
===> Adobe Photoshop, Corel PhotoPaint or other Image editing software
===> Dreamweaver, FrontPage or other WYSIWYG software
=> Web Design Reviews
=> Design Contest
-----------------------------
Programming
-----------------------------
=> Databases
=> PHP
=> ASP
=> Javascript
=> Java
=> General Coding
===> Perl
===> .NET
-----------------------------
Search Engine
-----------------------------
=> Google
=> Yahoo
=> MSN
=> Other Search Engines
=> Web Directories
===> SMF Directory
-----------------------------
Domain Names
-----------------------------
=> Domain Name Discussion
=> Domain Appraisals
=> Expired Domains
-----------------------------
Server Related
-----------------------------
=> Forum/CMS Related
=> Apache/IIS
=> Security
=> Linux
===> General Questions
===> Installations
===> Desktop & Multimedia
===> System configurations problems
===> Kernel things
-----------------------------
The Webmasters Talks !
-----------------------------
=> General Discussion
===> Fun
=> Tools
===> Link Popularity
===> Google Datacenter Query
===> Google Backlinks Checker
===> Google Page Rank Checker
===> Html Validation
===> Alexa Rank Checker
===> SEO 4 SMF
=====> Showcase
=====> Joomla
=====> Bugs
=====> Features
=> New Member Introductions
=> Suggestions & Feedback
=> News and Announcements
Loading...