views:

636

answers:

3

I'm writing java application that is using both SQLite and MySQL using JDBC.

Are there any differences in SQL for those databases? Can I use same queries for both SQLite and MySQL, or is there any db specific stuff, that doesn't work on the other one?

As far I've worked only with MySQL, so I don't really know much about SQLite.

+4  A: 

If you stick to ANSI SQL92, you'll should be fine.

There are some SQL92 features missing from both MySQL and SQLite (e.g. FULL OUTER JOIN). MySQL has both RIGHT JOIN, and LEFT JOIN, SQLite only the LEFT JOIN. SQLite doesn't support FOREIGN KEY constraints, neither does MySQL with MyISAM tables. SQLite of course doesn't have GRANT/REVOKE, as permission system is based on underlying OS's file permissions.

vartec
SQLite supports FOREIGN KEY constraints since version 3.6.19, see http://www.sqlite.org/foreignkeys.html.
Alix Axel
A: 

A big difference is the type system. SQLite lets you put any type of data in any column. It does, however, cast data to the declared type of the column if possible.

dan04
A: 

I'm doing something similar. There are a few differences in addition to the ones mentioned that I ran into:

  • in the newer versions of SQLite3 with the Xerial JDBC driver, foreign keys are indeed supported. SQLite supports inline foreign key constraint definition:
    CREATE TABLE Blah (foreignId Integer REFERENCES OtherTable (id));

    MySQL (with InnoDB) will accept the same syntax, but won't actually enforce the constraint unless you use a separate FOREIGN KEY clause which explicitly names the foreign table and key column(s):
    CREATE TABLE Blah (foreignId INTEGER, FOREIGN KEY foreignId REFERENCES OtherTable (id));

  • old versions of the SQLite JDBC driver don't support Statement.RETURN_GENERATED_KEYS; fixed in newer Xerial drivers.

  • the syntax for auto-incrementing keys differs; SQLite: (id INTEGER PRIMARY KEY ASC, ...); MySQL: (id INTEGER PRIMARY KEY AUTO_INCREMENT, ...)

  • SQLite accepts n-way comma-delimited joins:
    SELECT * FROM A, B, C ON (A.x = B.y AND B.y = C.z);

    MySQL does not; the following works in both:
    SELECT * FROM A INNER JOIN B ON A.x = B.y INNER JOIN C ON B.y = C.z;

  • With respect to the type differences, a related annoyance with SQLite's JDBC drivers is that the same column can produce different types via ResultSet.getObject(.); for example, an Integer or a Long depending on the magnitude of the number contained.

  • auto-incrementing keys in SQLite MUST be declared type INTEGER; in MySQL any numeric integer type works.

chris