DEV Community

Cover image for What is Database, Difference between SQL and NoSQL
geeksread
geeksread

Posted on

What is Database, Difference between SQL and NoSQL

In this knowledge bit, we are going to introduce the Database. We will discuss what is database?. Then, we will introduce two broad categories of databases which are SQL database and NoSQL databases.

WHAT IS DATABASE?
The database is a collection of information and data. It helps to organize the data items and help us to maintain that data. It is used by the different organization to organize the relationship between the data which help to share effective information across the organization.

Let suppose we have a text file which has the bunch of information that’s mean it is a database. The database also have the interface which helps us interacting with data you can add new information like add the new user to the database, or you remove some user or update the user whatever it is, it helps us to interact with the data.

What is Database?

WHY WE NEED DATABASE?
Let suppose we have created a TO-DO list application which saves to do list item in an array on the server. So when we turn off the server then we lost every item that we have saved in an array on the server. So the problem that we usually face is that our data does not persist if our server stops for any reason whether it is because we turned it off to make changes in code, or it turned off because of the power outage, or the server could be turned off for any freak reason. If the server stops we would lose all the data which is obviously a worst case for any application.

If Facebook, Twitter or any other application lost our data every time we logged out or the server stopped then it would be really big failure for that application. So to solve this problem we use the database which stores our data, no matter we logged out of Facebook, Twitter or any other application, or the server stop for any reason, the data will still exist.

THE DIFFERENCES BETWEEN SQL AND NOSQL DATABASES:
Now the last thing we are going to discuss here is the difference between SQL and NoSQL database. Almost every company out there has some database usually lots of databases with lots of information in it, whether it is the database for the payroll, data for the employees or it has the bunch of data for it clients and users information. So there are two types of databases one is SQL and other is NoSQL database.

SQL OR RELATIONAL DATABASE:
The SQL database also called relational database. It is the tabular database which is created using structure Query language and used for defining and manipulating data. SQL is the lightweight and extremely powerful database where we can retrieve data, executes queries, and edits data by updating, deleting, or creating new records. In SQL we can use JOIN clause which allows us to retrieve the relative data which is stored in different tables with the single command. The SQL naturally fit in many software stacks like the LAMP and Ruby-based stacks and this database can easily understand and supported by many operating systems.

In SQL we stored data in form of tables which consist of n number of rows and column. This database is scalable and has the predefined schema.

HOW SQL DATABASE WORKS:
Let suppose we want to create a relational or SQL database for a user information then a user must have a column for ID, name, age, and city. Here is how the SQL database for that person would look like:

User Information

id | name | age | city
———————————–
1 | Alan | 18 | Amsterdam
2 | Matt | 20 | London
3 | Alexa | 17 | Montevideo
4 | Rojo | 23 | London

In the relational database, this is how the information of the user looks like, so one user must have ID, Name, Age, and a city and then every single user that we add must follow this pattern. Let suppose we have another table which contains the country name for the users.

Users Country

id | country
———————————–
1 | Netherland
2 | England
3 | Uruguay
4 | United States

If we want to create the relationship between these two tables then we will create another table called join table. In this join table, we join userID with the country.

JOIN Table

userID | countryID
———————————

1 3
2 2
3 1
4 2

So join table join the user id with the countryid, in our case a user with id 1 owns country with id 3 that’s mean Alan owns country Uruguay.

The most famous relational database are Oracle, MySQL, SQL Server, Sybase, and MariaDB.

NOSQL OR NON-RELATIONAL DATABASE:
The other type of database we are going to discuss there is non-relational databases or NoSQL database and it is also called distributed database. In the Non-relational database, we don’t need to define the data in the tabular form or define any type of pattern. This type of database is way more flexible and easier to manage and it provides high-level flexibility. In NoSQL database, we do not have any standard schema which allows us to stored data in form of key-value pairs, column, document, or graph forms. It does not limit the storable data type and it also allows us to add new type as our requirement change.

There is some classification of NoSQL Database:

We do not need to define the schema or structure of the data to create the Document-oriented database.
In NoSQL, every document-oriented database can have its own database structure.
We can add the field in NoSQL database at any time and the syntax varies from database to database.
It is the scalable database which means we can handle traffic through sharding like we can add more servers in our NoSQL database while SQL database we only can achieve by increasing the CPU, RAM, or SSD.
HOW NOSQL DATABASE WORKS:
Now let’s see how the non-relational or NoSQL database works. In the non-relational database there are no tables, so we don’t have to define the tubular structure and NoSQL database things can be nested.

{
name: “Alan”,
age: 18,
city: Amsterdam,
country: [
{countryname: “Netherland”},
{countryname: “England”}
]
}

So you can notice that it’s basically JavaScript object and you can also we have the bunch of key-value pairs by name, age, city. You can see that we have also a nested data like country and can add an object inside the object. So in NoSQL database can be stored in a collection. The example of NoSQL database is MongoDB, CouchDB, BigTable, Apache’s Cassandra DB, HBase, and Riak.

SQL database has been in the market for a long time, and it is tabular type database in which we have to relate data, we have to have multiple tables to relate that data. In SQL we often use primary keys or iD’s and we must need to have schema before creating the database. While in the Non-relational database, we don’t have to define any sort of table instead we have the flexible structure.

Both databases do the same thing in different ways. If you select one database and then in future, you want to switch to another then you can migrate to another in future too. But research and planning can save a lot of time and efforts.

WHEN TO USE SQL DATABASE?
If we want our date to be stored in logically structured form and must be consistent and the integrity of data is essential then we required to use SQL database or relational database.

WHEN TO USE NOSQL DATABASE?
If we want to store data in the unrelated form and want data to be stored in an indeterminate way and where the data is inconsistent and the structure of the date is variable.

In the knowledge bit, we have learned what is database. we have also learned different types of databases. We have discussed relational and non-relational database in detail and discussed when we should use the relational and non-relational database.

You can have a look at our whole article library from here.

Top comments (0)