CodeNewbie Community


Posted on

What's the difference between a SQL and a NoSQL database?

What's the difference between a SQL and a NoSQL database? I'm beginning to learn about databases and would love some further clarification on the difference here.

Discussion (3)

djuber profile image
Daniel Uber • Edited on

A SQL database is a database that implements SQL, and almost always a "relational database", which means data is stored in (many) tables, with a fixed set of columns, and a changeable set of rows (the values in the row, and the number of rows, can change, but the names of columns are mostly constant, and every row in the same table has the same set of attributes or columns). Many vendors (oracle, microsoft, ibm, mysql, postgresql) provide a SQL database product, and the SQL language to query and update data has been standardized (and the standard receives updates and maintenance) since the 1980's, so you are only ever lightly locked into any product, you could always ask (in SQL) for all of your data from the database, and push it all (using SQL, again) into another vendor's database system, as long as you didn't rely too heavily on any product-specific extensions. Finding and working with your data will be almost the exact same no matter which system you're using, but managing these systems might be very different.

A NoSQL database usually includes any database system that doesn't use the relational model. Document databases like MongoDB store all of the information about an entity in a single JSON document, making retrieving all of the relevant information about the item a single fetch, graph databases or key-value stores like caches also fit in the NoSQL name. There are a lot of ideas, but not a common shared interface.

One of the key guarantees that most SQL databases give, and many NoSQL databases do not, is the idea of an atomic transaction, basically ensuring that either all of the changes, or none of the changes, in a set of updates, is applied. A good example of when you might want this kind of guarantee would be a bank transfer between two accounts, with a transfer receipt logged to the ledger table. It would be an error to add money to my account while leaving the money in your account, and it would be an error to have removed money from your account without also adding it to mine, and we never want to have moved money without preserving a record of it (with the amount, a timestamp, and both account numbers) - if we can't successfully do all three updates at the same time, we don't want to do any of them. Additionally, no other query should show only part of the changes and not the other parts.

One of the major benefits you can get from relaxing that restriction is much faster updates, at the cost of sometimes getting out of date information. Because you don't need to have strict guarantees about the correctness of the data, you can distribute the system to multiple machines in multiple places. It's also much simpler if you're storing objects in your system to have representations that look like your objects, instead of possibly needing to update many tables when something changes, and searching or aggregating data from many tables when fetching an object from the database.

testinnplayin profile image

To have a slightly different take on this, SQL means "structured query language". This query language is fairly uniform across all different kinds of SQL databases though there can be a few differences here in there. However, it's quite easy to go from one to the other because you can already have a general idea of what structure your query should have.

On the other hand, NoSQL can have many different kinds of query languages. They are not standardised by any means. Just compare MongoDB's query language to that of IndexedDB, ElasticSearch, Redis, whatever.

SQL has been around for a long time by the way.

adiatiayu profile image
Ayu Adiati

SQL is a relational database. It's fixed and structured. We can see it as a table, with the relations between rows and columns.
And all data in the table has to be filled in, even with n/a when there is no data.

While No-SQL is an unstructured database.
For example, you are a user of CodeNewbie. You may fill all your biodata, or leave some blank. You may post a blog or leave comments, but it's also okay if you don't.
No-SQL is flexible to leave some data out.

For more explanation, you can go to MongoDB website, and read the difference between SQL and No-SQL and the No-SQL explanation :)