Monthly Archives: January 2013

A Simple MySQL Trigger Example

I was searching for this a few days back and I was struck at how Google had all these terrible explanations at the top of the results. Here’s a simple way to create and understand triggers in mysql.

CREATE TABLE IF NOT EXISTS `emails` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `emails` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `emails_triggers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `emails` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

DELIMITER $$
CREATE TRIGGER trigger_test AFTER
INSERT ON emails
FOR EACH ROW
BEGIN
 INSERT INTO `emails_triggers` (email) values (NEW.email);
END$$
DELIMITER ;

insert into emails values('test@test.com');
select * from emails_trigger;

Just a note: the DELIMITER keyword is used via a tool like PhpMyAdmin or the MySQL Client. I could not get this keyword to be recognized by my SQL editor and had problems until I just pasted it via PhpMyAdmin.

The JavaScript Module Pattern

One thing that really bothers me is often how obtuse programmers explain relatively simple things. Take for example the JavaScript Module Pattern.

<script type="text/javascript">
    var module = (function () {
        var foo = 0;
        return {
            bar:function () {
                foo = foo + 1;
                alert(foo);
            }
        };
    }());

    module.bar();
    module.bar();
</script>

This is really just two things:

  1. An anonymous function which allows for immediate execution.
  2. Returning a function, which behave like “public variables/methods”, keeping the other functions/properties inside the module which behave like “private variables/methods”

That’s it, no need to explain this with 5 pages of explanation about encapsulation, etc.