DEV Community

Ankur Yadav
Ankur Yadav

Posted on • Edited on

SQL vs NoSQL databases

SQL vs NoSQL

There are two main types of databases: relational databases and non-relational databases. Both of them are different in the way they were built, the kind of information they store, and the storage method they use.

Relational databases are structured and have predefined schemas. Non-relational databases are unstructured, distributed, and have a dynamic schema.

SQL

A SQL database supports structured query language (SQL) — a domain-specific programming language for querying and manipulating data in a relational database.

Relational databases store data in rows and columns. Some of the most popular relational databases are MySQL, Oracle, Postgres, and MariaDB.

NoSQL

The most common types of NoSQL databases are:

Key-Value Databases: Data is stored as key-value pairs. Some popular key-value databases include Redis and Dynamo.

Document Databases: In these databases, data is stored as JSON documents and these documents are grouped together into collections. Each document can have a different structure. Document databases include CouchDB and MongoDB.

Wide-Column Databases: In these databases, the names and format of the columns can vary across rows, even within the same table. Wide-column databases are also known as column family databases. Columnar databases are best suited for analyzing large datasets. Some popular Wide-Column databases are Cassandra and HBase.

Graph Databases: These databases are used to store data whose relations are best represented in a graph. Data is stored as graph structures with nodes, properties, and lines. Examples of graph databases are Neo4J and InfiniteGraph.

Differences between SQL and NoSQL

Storage: SQL stores data in tables where each row represents an entity and each column represents a data point about that entity.

NoSQL databases can have different data storage models. The main ones are key-value, document, graph, and columnar.

Schema: In SQL, each column must be decided and chosen before data entry and each row must have data for each column. The schema can be altered later, but it involves modifying the whole database and going offline.

In NoSQL, schemas are dynamic. Columns can be added anytime and each row doesn’t have to contain data for each column.

Querying: SQL databases use SQL (structured query language) for defining and manipulating the data.

In a NoSQL database, queries are focused on a collection of documents. Sometimes it is also called UnQL (Unstructured Query Language). Different databases have different syntax for using UnQL.

Scalability: SQL databases are vertically scalable, i.e. by increasing the power (Memory, CPU, etc.) of the hardware, which can get very expensive. It is possible to scale a relational database across multiple servers, but this is a challenging and time-consuming process.

NoSQL databases are horizontally scalable, meaning we can add more servers easily in our NoSQL database infrastructure to handle a lot of traffic. It can be a lot more cost-effective than vertical scaling.

When to use SQL databases

When your data is structured and unchanging.

SQL databases are best when you need ACID compliance. ACID compliances protect the integrity of your data by defining exactly how transaction interacts with your database.

When to use NoSQL databases

When you need rapid development. NoSQL is extremely useful for rapid development as it doesn’t need to be planned ahead of time.

Storing large volumes of data that have little to no structure. A NoSQL database has no limits on the types of data we can store together.

Top comments (0)