Document based databases+
SQL vs NoSQL: Critical Differences you need to know!
NoSQL or SQL is probably a question that you'll come across many times throughout your development carrier. In this blog post, I would like to take a closer look at what the crucial differences are between the two and how you select the right one for your next project.
Here's all you need to know about SQL and NoSQL
To begin with, I would like to clarify a few of the most essential differences between a SQL and NoSql database. The core fundamentals of how both databases work clearly show a lot of contrast and how you set up and use each database.
What is SQL
So in theory SQL is not a database itself, since it is an abbreviation of Structured Query Language. What is most important to notice is, that SQL a language you will use to fetch data. A typical use case for this would look something like this:
This is just one SQL command of thousands, but this is how you create and fetch data in SQL. The word SELECT as well as FROM are syntax features often referred to as keywords, which are used to define what you want to do. In this case from above, we want to select something from a table somewhere. id, name, and price, as well as products, are all parameters telling the query what fields you would like to get and from what table they should be fetched out of.
The principles of a relational database are what really define the distinction between SQL and NoSQL. Here's a look at, what a relational database could look like:
All SQL databases consist of tables, which consist of rows or fields. These fields are defined by a general schema and there are a lot of rules on how these should be defined. The most important rule is, that this schema should be determined before you start setting up your database - especially due to the fact that it can be a difficult process to deconstruct or rearrange a table after it's been loaded with data. Another important rule is, that all the fields have to follow the schema, and should in general not be empty. There are however certain exceptions to the rule, but I won't go into details about this in this post.
What is NoSQL
When we talk about NoSQL databases, what is most significant compared to SQL, is the fact that there are no schemas. Essentially a NoSQL database can be interpreted as one giant and advanced JSON-like structure, that can be defined end any way you want to. Let's have a look at what a NoSQL database could look like:
As you can see, there are no longer any schemas. Instead we have multiple documents, that are defined similarly to JSON objects. These documents live inside the collections, which can be compared to the tables from the relational database.
So in contrast to the Relational database, a NoSQL database is also called a document database. Often developers find it more accurate to describe a NoSQL database as being a non-relational or NoRel database since there usually are no (or few relations) between collections.
If you have an order from a customer buying a certain product, you will create a collection called orders, and within this collection, you will create a document for each order. This document will not relate to the product and the customer who bought the product - instead it will contain and store all the information itself.
Advantages and disadvantages
So before we talk about which, whether you should go for SQL or NoSQL I want to mention some of the advantages and disadvantages that each solution has.
The advantages of SQL
1. Schema-based tables
One thing I have yet to mention is the fact that SQL databases favor the principle of normalization. One key aspect of normalization is, that a write-request should update one table and one table only in your database. Let's say you want to update the product name from the previous example, and since you have only stored this name in one place throughout your entire database, it will update within milliseconds.
2. Total control
Since everything is defined within the schema, you know that fields cannot be empty. You have determined how the data-structure within your database, and therefore you are more in control of what type of data is being stored in your database.
The disadvantages of SQL
1. Schema-based tables
Schema-based tables can be very beneficial, but they really put a limit to your flexibility if you want to expand your datasets. The more advanced your database is the harder it will become to redefine a schema type since it could result in thousands of empty fields in your table.
2. scalability limitations
There are generally two types of scaling - horizontal and vertical. With horizontal scaling, you distribute your data across multiple servers giving you the freedom of having nearly no limitations to your scaling needs. If you need more power, you can get more servers. Since all the data in a SQL database is related, horizontal scaling is simply not an option. This leaves you with vertical scaling, where you have to invest money in improving your current server capacity and hardware (which can be a costly affair).
Speed is a major concern in many companies. You should really only consider this disadvantage valid if you're planning to build an application with thousands of complicated read-requests. But compared to a document database, many relations can result in a slower application, since the database will go through multiple tables to query the data.
The advantages of NoSQL
In general, people often say that the biggest advantage of NoSQL is the flexibility that you gain with a document database. If you're a startup that hasn't defined all aspects of your database, a NoSQL database is maybe where you want to start, since you can update and scale your data structure as you progress in your company.
2. No scalability limitations
As mentioned before, it's very beneficial to be able to do horizontal scaling. With a NoSQL database, you can easily distribute your data across many servers due to its flexible and non-relational structure.
3. Mass Read speed
Another huge advantage of using NoSQL is the fact that it's read-performance really good. Since there are no relations between the documents inside your database, the document can fetch the data much quicker than a SQL database would, since it doesn't have to join tables together to fetch data.
The disadvantages of NoSQL
1. Frequent writes
If you're planning to build an application, where your data is often updated, you might want to consider a SQL database, due to the normalization principle. If data is updated in a relational database it will typically only be updated and stored in one place and one place only, which is a huge benefit for databases with frequent write requests.
2. You can't rely on your data
Since you can have empty fields in a document, it can be harder to figure out what data your database actually consists of. This can occasionally lead to some errors.
So who will win - SQL or NoSQL?
Let me start off by saying, that there is no clear winner in this competition. It heavily depends on both the application you're are building as well as the data that you want to store. Actually, you will often find big companies who typically use both SQL and NoSQL if they have multiple types of data and are working on several applications. So it depends on your needs as a company.
You have to ask yourself whether your project would benefit from having a clear and predetermined schema with a lot of relations - and if you will be working with data that is changing frequently. If that's the case you should probably reach out for a SQL database. If you want a more flexible data structure, that can fetch a lot of data really fast - and you want to have the ability to scale your data in the long run? Then a NoSQL database is probably more beneficial for your needs.
That being said, you can build any type of application with either SQL and NoSQL. You'll most likely find yourself having issues with your database if your company becomes really famous - which is why many big companies use both SQL and NoSQL.
A professional web development workflow in a small team?
Published at: 2020-11-16
Gatsby.js vs. Next.js | First impressions with the best react framework tools currently!
Published at: 2020-11-12
What can you do to get a faster website? Tips for beginners and advanced developers.
Published at: 2020-04-20
What is the Pomodoro technique?
Published at: 2020-04-13
Four Day work week
Published at: 2020-04-11
Published at: 2020-04-10
Gatsby.js - Is static generated websites a technology of the future?
Published at: 2020-04-09
The basics of statically generated websites.
Published at: 2020-04-09
What is Netlify
Published at: 2020-04-08
First impressions of Sanity as a content management system
Published at: 2020-04-07