Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax differences between mysql, sqlite and pgsql

I'm creating a tiny activerecord library using PDO and I'm planning to support MySQL, Sqlite and PgSQL.

My question is how I can be sure that the query string works with all adapters? There will mostly be CRUD statements with some joins etc. Is there a standard I can follow that works for all of these?

Thanks / Tobias

EDIT: Thanks for all your answers but my question was more about the SQL 'syntax' differences between them.

like image 626
sandelius Avatar asked Nov 28 '10 12:11

sandelius


People also ask

Is there any syntax difference between PostgreSQL and MySQL?

Both MySQL and PostgreSQL databases support stored procedures, but MySQL only supports standard SQL syntaxes, while PostgreSQL supports very advanced procedures. Stored Procedures are implemented in the form of functions in PostgreSQL with a RETURN VOID clause.

Are SQLite and MySQL syntax same?

They are completely different. SQLite is a public domain, open-source project. It is what is called an “embedded” database which means the DB engine runs as part of your app. MySQL is also open-source but is owned by Oracle.

What is the difference between MySQL and SQLite?

SQLite is a server-less database and is self-contained. This is also referred to as an embedded database which means the DB engine runs as a part of the app. On the other hand, MySQL requires a server to run. MySQL will require a client and server architecture to interact over a network.

Should I use SQLite PostgreSQL or MySQL?

Do you prioritize scalability? You should choose MySQL over PostgreSQL and SQLite. MySQL supports multi-threading, which makes it a highly scalable RDBMS.


1 Answers

If you want to write your own DB layer, I'd suggest you:

  1. Use placeholders, if you aren't already. They add security too.
  2. Use bindParam/bindValue with value type (e.g. BOOLEANS don't exist in SQLite but work if bound with PARAM_BOOL)...
  3. Use stored procedures from MySQL, create matching names in PostgreSQL, and define them in SQLite with sqliteCreateAggregate/sqliteCreateFunction.
  4. Do all parameter checking in PHP, because SQLite won't do any (e.g. validate date variables)...
  5. Use InnoDB for MySQL to get transactions.

Note: By supporting these vastly different RDBMs, you're demoting the database to just a data store. Keep in mind that SQLite is very limited. It does not have native data types save from number/string. E.g. it's missing date handling and intervals, and so on. All three databases support transactions, which are essential for data integrity when the integrity is maintained outside the DB.

Edit: Removed mention of MySQL triggers, which are availabe for 5.0.

like image 127
jmz Avatar answered Sep 28 '22 03:09

jmz