Keeping a second table in sync with a main table

Sometimes you may want all changes recorded to another table and keep them in sync.
There are of course a few different ways of doing this, you could play with binlogs and such. I got asked by a course attendee to do it in a specific way using triggers, so I wrote this up, and it should work:

mysql> CREATE TABLE t1 ( a INT, b CHAR(5) );
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE TABLE t2 ( a INT, b CHAR(5) );
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TRIGGER trg1 AFTER INSERT ON t1 FOR EACH ROW
-> INSERT INTO t2 VALUES (NEW.a, NEW.b);
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TRIGGER trg2 AFTER DELETE ON t1 FOR EACH ROW
-> DELETE FROM t2 WHERE a = OLD.a AND b = OLD.b LIMIT 1;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TRIGGER trg3 AFTER UPDATE ON t1 FOR EACH ROW
-> UPDATE t2 SET a = NEW.a, b = NEW.b WHERE a = OLD.a AND b = OLD.b LIMIT 1;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO t1 VALUES (1, 'a');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO t1 VALUES (2, 'a');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO t1 VALUES (2, 'b');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO t1 VALUES (2, 'b');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO t1 VALUES (2, 'b');
Query OK, 1 row affected (0.00 sec)

mysql> DELETE FROM t1 WHERE a = 1;
Query OK, 1 row affected (0.00 sec)

mysql> UPDATE t1 SET a = 1 WHERE b = 'b' LIMIT 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT * FROM t1;
+---+---+
| a | b |
+---+---+
| 2 | a |
| 1 | b |
| 2 | b |
| 2 | b |
+---+---+
4 rows in set (0.00 sec) mysql> SELECT * FROM t2;
+---+---+
| a | b |
+---+---+
| 2 | a |
| 1 | b |
| 2 | b |
| 2 | b |
+---+---+
4 rows in set (0.00 sec)

If I ever get the IRC twitch and no computer is close

Nice to have putty on the cellphone to ssh home over 3G and irc. Unfortunately the keys you use a lot on *nix systems are some 16-20 key presses in on the special symbol button ( / * ; : etc).

Maybe time to replace the harddrive in my laptop…

Mar 16 17:01:49 flupps KernelEventAgent[61]: tid 00000000 received unknown event (256)
Mar 16 17:01:49 flupps kernel[0]: disk0s3: 0xe0030005 (UNDEFINED).
Mar 16 17:01:49 flupps kernel[0]: disk0s3: 0xe0030005 (UNDEFINED).
Mar 16 17:01:49 flupps kernel[0]: disk0s3: 0xe0030005 (UNDEFINED).
Mar 16 17:01:49 flupps kernel[0]: disk0s3: 0xe0030005 (UNDEFINED).
Mar 16 17:01:49 flupps kernel[0]: disk0s3: 0xe0030005 (UNDEFINED).
Mar 16 17:01:49 flupps kernel[0]: disk0s3: 0xe0030005 (UNDEFINED).
Mar 16 17:01:49 […]

I think I owe someone a beer…

…whoever wrote this patch:
Server version: 5.0.33-log flupps build (probably not stable…)
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql> use world;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SELECT * FROM country, city, city c, city c2, […]

Sorting US Address Data.

So, in Europe the street name comes first, then the number of the building.
In the US the number of the building comes first, then the actual street, which makes it a little bit rougher to sort.
Imagine a table like this:
mysql> SELECT * FROM numsort;
+——+
| a |
+——+
| 5 |
| a […]

Desperate for Connectivity

Was just going back home after being out for a while the other day and I saw this in my stairs:

Tic toc tic kets

Now I just have to find some people to go with.

Blink: The Power of Thinking Without Thinking

You ever been in the situation where something makes you feel uneasy about a person or situation, but you stayed in it, and after being with that person or in that situation longer, you started feeling more comfortable.
Then a long time later something bad happens and you wish you would have listened to that initial […]

Anthony Kiedis - Scar Tissue

I’ve been reading this book now for a while during travels. I picked it up sometime in June in London and have been just paging through is slowly.
It’s a biography co-written with Larry Sloman and covers the time from before Anthony was born and up until the fall 2004.
It’s basically a story about sex, […]

US Immigration problems

In April this year I had a run-in with the US Customs and got turned around. I got permanently stripped of my rights as a EU citizen to enter the US under the VISA Waiver program. Here’s the short story about what happened. (Everything is fixed now, though, but took a lot of paperwork and […]