7

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.

Jakub Arnold
  • 79,807
  • 86
  • 218
  • 314

3 Answers3

6

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
  • 61
  • 1
  • 1
  • can you verify which version of sqlite jdbc driver you are using which seems to support RETURN_GENERATED_KEYS ? I have tried many different versions with no luck – Amit Nov 28 '10 at 18:26
  • http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC working at least as of 3.6.20.1 – chris Dec 17 '10 at 19:22
6

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
  • 118,560
  • 34
  • 206
  • 238
  • 6
    SQLite supports FOREIGN KEY constraints since version 3.6.19, see http://www.sqlite.org/foreignkeys.html. – Alix Axel May 12 '10 at 14:36
1

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
  • 77,360
  • 20
  • 153
  • 184