Latest articles under Database category

Swap values in 2 rows SQL

I encountered a situation at work that I feel would be a good brain teaser. I was working on a project and needed a way to swap values from 2 different rows in a SQL table. Obviously I can do it on client side (like PHP) to do it; but that wouldn’t be a good [...]

Posted on October 11th, 2009 under General SQL, MySQL, PostgreSQL  •  15 Comments

Solving sudoku using PostgreSQL PL/pgSQL

I have no idea why I would want to solve sudoku in PostgreSQL PL/pgSQL. My guess would be just for the fun of it. I’m also hoping that it can serve as a tutorial example in programming PostgreSQL PL/pgSQL.
So what is sudoku? It is basically a number puzzle where the objective is to fill 9×9 [...]

Posted on August 19th, 2009 under General / Algorithm, PostgreSQL  •  4 Comments

Use EXISTS() instead of COUNT(*)

A lot of times, I saw programmers use COUNT(*) and check if the result is greater than 0 to see if a particular record exits in a table. If your only purpose is to know if a record exists or not and you don’t really care about how many matched records are there, the better [...]

Posted on June 30th, 2009 under General SQL  •  3 Comments

Bulk alter schema in MySQL.

There are times when you want to alter schema for all tables in the database. For example, let’s say in your project you have multiple databases with multiple tables on each of them. Suppose that you have columns within that table defined as DECIMAL(11,6). What it means is that the column will have 11 digits [...]

Posted on April 20th, 2009 under MySQL  •  1 Comment

Getting around MySQL TIMEDIFF() for hours greater than 838

One of the golden rule of programming is to know about the function you are calling. You need to know what parameters it takes and what return values and type it gives back. For the most part, it’s not too hard to find that out. For MySQL, however, I found that it’s not always easy [...]

Posted on March 11th, 2009 under MySQL  •  12 Comments

Page 1 of 3123»