Featured Database Articles

Moving Logic Into the Database - Page 4

Now there is one serious pitfall you can encounter here. What if a mischievous end-user tries to make Task A dependent on Task C, creating a circular dependency? With all this logic in the database, I believe you could create an infinite loop of triggers firing and fighting back and forth inside of your database. I didn't try to prevent this with PL/pgsql logic (if anyone can figure out how to do that, let me know), but instead created a few lines of recursive PHP code to eliminate any chance of circular dependencies.

There, now we have guaranteed data integrity, and we got a ton of functionality for free while we were at it. Our database rows now manage themselves - pushing themselves back and forth as necessary to maintain the constraints we have designated. Now, I'll grant you that most PL/pgsql projects will be far easier than this, especially if you are just getting started, but once you get your toes wet in true database programming, you are only going to want more.