Main Content

Which database should I use? MySQL v SQLite

Archive - Originally posted on "The Horse's Mouth" - 2013-02-16 16:06:00 - Graham Ellis

Which SQL Database would I advise newcomers with a new application to use?

Five years ago, MySQL would have been the obvious choice in many circumstances. But these days, MySQL has grown into a much bigger product and there are very serious licensing and other commercial issues that need to be considered; whilst it remains open source, it's a very long way from having a license that's close to public domain.

The answer these days may, still, be MySQL. Or it may be PostgreSQL. Or it may be SQLite. But the answer will certainly include a caveat to warn the newcomer to write his SQL in such a way that it's not impossible to change at a future date. A wrapper - a set of functions that hides the actual database - is a good idea. And that may be quite thin, or it may include lots of extra facilities. Such a wrapper is included, for example, in your model classes in Ruby on Rails via Active Records and a host of other methods and facilities.

Reduced to SQL commands, SQL doesn't vary as much as you would expect; the admin, logging in, and control of multiple appications differs but the actual tables and table commands are remarkably similar ... with SQLite typically offering the most restricted set. It's therefore a logical approach to code for SQLite if it provides everything you need, in the knowledge that you can move on to MySQL or PostgreSQL or something else later.

By way of example, I wrote a series of SQL commands in MySQL earlier today, and then sourced them though the mysql client program. I then edited that series of commands to see how much needed changing for SQLite to be sourced through the sqlite client program. The answer was "not very much". Apart from the removal of the database stuff (and replacing database level flushes with table level flushes), the only thing that caused an issue was a right join ... and that could easily be rewritten as a left join.

The SQLite file is [here] and the MySQL file I started with is [here].

And here is the full difference report:

trainee@brugges:~/ruby$ diff seeds.mysql seeds.sqlite
1,4c1,3
< #%% MySQL example - table joins
< drop database if exists jamie;
< create database jamie;
< use jamie;
---
> #%% SQLite example - table joins
> drop table aisles;
> drop table products;
26d24
< select * from aisles right join products on aisles.aid = products.aid;