DEV Community

Cover image for Using SQL or NoSQL
Nevergarden
Nevergarden

Posted on

Using SQL or NoSQL

This Monday on an interview I was asked if I was supposed to start a project how do I know whether I use SQL or NoSQL?
And to tell the truth I was confused and puzzled that I really never thought of when to use them?

I forgot about that question until now and then I searched for when to use them the first thing was the actual implementation of them.

Structure

SQL is structured while NoSQL can accept unstructured data.
SQL accepts gives you a form and asks you to fill it exactly as is.
But when talking NoSQL you can provide any data to any document. But then you have to check this information when you retrieve that document.

Transaction

SQL is transactional while NoSQL provides a document-level-based transaction but not what we look for.
Think you are programming some wallets and Jack transfers 100 dollars to Jills wallet.
You write a transaction in SQL like:

BEGIN TRANSACTION
UPDATE Jack.Money -= 100;
UPDATE Jill.Money += 100;
COMMIT
Enter fullscreen mode Exit fullscreen mode

This way both of them should happen so if the first command works and the second one fails then Jack's money doesn't go to waste.
But with NoSQL, we have to check their documents and then request again. (I might be wrong about this)
Also, this answer explained how NoSQL databases have lighter transactions.

Generally speaking, NoSQL solutions have lighter weight transactional semantics than relational databases, but still have facilities for atomic operations at some level.

Generally, the ones which do master-master replication provide less in the way of consistency, and more availability. So one should choose the right tool for the right problem.

…

Top comments (3)

Collapse
 
fredericksalazar profile image
FrederickSalazar

the use of the NoSQL or Relational Database is based en the needs of the bussines, also Mongodb is a part of NoSQL databases but exist more databases, now for example have a newSQL paradigm this data bases are relational but supporting horizontal scaling, clustering and partitional of the data en shards, example voltdb.

i recomend that Mongodb is not only NoSQL, for example in NoSQL DBs exists redis, riak, Cassandra, Mongodb, couchdb, monetdb and more and more

in new paradigm NewSQL you can find about voltdb, memsql, SQLFire de GoPivotal etc

Collapse
 
nevergarden profile image
Nevergarden

There is a comment to that answer on stackoverflow says that there is a NoSQL database fully supporting transactions.
And yes this NewSQL concept is so nice.

Collapse
 
fredericksalazar profile image
FrederickSalazar

sure, and the concept of polyglot persistence is also fundamental, with which organizations use in different databases in the architecture