16

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.

3 Answers 3

13

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
xerial.org/trac/Xerial/wiki/SQLiteJDBC working at least as of 3.6.20.1
10

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.

1 Comment

SQLite supports FOREIGN KEY constraints since version 3.6.19, see sqlite.org/foreignkeys.html.
5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.