Database SQL – Why Do Relational Databases Only Accept SQL Queries?

databasesql

As far as I know, most relational databases do not offer any driver-level API for queries, except a query function which takes an SQL string as an argument.

I'm thinking how easier it would be if one could do:

var result = mysql.select('article', {id: 3})

For joined tables, it would be slightly more complex, but still possible. For example:

var tables = mysql.join({tables: ['article', 'category'], on: 'categoryID'});
mysql.select(tables, {'article.id': 3}, ['article.title', 'article.body', 'category.categoryID'])

Cleaner code, no string parsing overhead, no injection problems, easier reuse of query elements… I can see a lot of advantages.

Is there a specific reason why it was chosen to only provide access to queries through SQL?

Best Answer

Databases are out of process - they run on a different server usually. So even if you had an API, it would need to send something across the wire that represents your query and all of its projections, filters, groups, subqueries, expressions, joins, aggregate functions etc. That something could be XML or JSON or some proprietary format, but it may as well be SQL because that is tried, tested and supported.

It is less common these days to build up SQL commands yourself - many people use some sort of ORM. Even though these ultimately translate into SQL statements, they may provide the API you are after.

Related Topic