Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL and SQLite differences in SQL

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.

like image 478
Jakub Arnold Avatar asked Apr 15 '09 11:04

Jakub Arnold


People also ask

What is the difference between SQL and MySQL and SQLite?

SQLite is file-based whereas SQL is a server based. MySQL is an open-source Relational Database Management System (RDBMS) that uses Structured Query Language (SQL). It is used for a wide range of purposes, including data warehousing, e-commerce, and logging applications.

Are MySQL and SQLite commands the same?

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.

Which is faster MySQL or SQLite?

MySQL, while also easy to use, provides a more robust set of features, better security, better performance at scale and overall better multi-user management. SQLite lacks these features and optimizations.


2 Answers

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.

like image 161
chris Avatar answered Oct 06 '22 17:10

chris


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.

like image 32
vartec Avatar answered Oct 06 '22 18:10

vartec