Saving your data
Tags: database, webdev, rdbms, nosql
Published: 2023-03-05 17:27:53

More often than not, you will be in the need to save the information you've gathered in your application, to do so, we have many models, lets list some of them:

1. Relational databases
2. NoSQL databases

## Relational databases (RDBMS)

This database uses the relational data model, which stores data in form of rows and columns which when combined they make a table or relation.
To interact with this kind of databases, you use a language called SQL (Structured Query Language) that is common between all of the different providers like PostgreSQL, MySQL, Oracle, Microsoft SQL Server, etc.

The Relational Database Management System (RDBMS) adheres to th ACID properties, that are as follows:

- **A for Atomicity:** This ensures that the data operation will finish successfully or unsuccessfully, it employs an all-or-nothing approach, meaning if something went wrong in the transaction, none of it will persist
- **C for Consistency:** It means that when we execute an operation on the data, the value before and after the operation should be correct.
- **I for Isolation:** Many users can access the data from the database at the same time, meaning that when many transactions occur at the same time, its effects should not be apparent to the other transactions
- **D for Durability:** It assures that the data modification is persistent once the operation has completed and commited

### Advantages of a RDBMS

Some key advantages of a RDBMS are the following:

- Categorizing data. Data is easily categorized and stored in a relational database that can then be queried and filtered to extract information. After the original database creations, new data categories can be added without having the need to modify the existing application
- Accuracy. Data is stored just ones eliminating data duplication
- Ease of use. Complex queries are easy for users to carry out with SQL, its main language.
- Collaboration. Multiple users can access the same database

### Disadvantages of a RDBMS

Some of the disadvantages of using a RDBMS are the following:

- Structure. RDBMS require a lot of structure and planning, because each table and column must be defined according on what data they will hold
- Inflexibility. RDBMS are not ideal for handling unstructured data, when data changes there must be a change in the database design

## NoSQL databases

NoSQL is a database that may be used to store a wide variaty of dataset, it holds data in a variaty of formats, not only tabular as the RDBMS.

### Types of NoSQL databases

- **Key-value storage:** This is the most basic sort of database storage, where each item is stored as a Key-value pair, example of these are Tyrant, Redis, Voldemort
- **Document-oriented Database:** This is the most common kind of NoSQL database and they store the data in the form of a JSON-like document. It's easy to use by developers because they implement the same document-model format as the application code, example of these are MongoDB, CouchDB
- **Graph databases:** These databases are used to store large volumes of data, they are most typically used by social networking sites, example of these are OrientDB, InfoGrid, FlockDB
- **Column databases:** These databases are similar to RDBMS, but instead of storing data in rows, data is stored in huge columns, example of these are Cassandra, HBase

### Advantages of a NoSQL database

- Flexible data models
- Fast queries
- Esay for developers

### Disadvantages of a NoSQL database

- No support of the ACID transactions
- Data redundancy
- Require large storage

## Comparison table

|                        | SQL Databases                                      | No SQL Databases                                             |
| ---------------------- | -------------------------------------------------- | ------------------------------------------------------------ |
| **Data storage model** | Tables with fixed rows and columns                 | Different structures like JSON, Key-value pairs, Graph, etc. |
| **History**            | Developed in the 1970's to reduce data duplication | Developed in the late 2000's with focus on scaling           |
| **Examples**           | Oracle, MySQL, PostgreSQL                          | MongoDB, Redis, Tyrant, Cassandra, OrientDB                  |
| **Schemas**            | Rigid                                              | Flexible                                                     |
| **Joins**              | Typically required                                 | Typically not requred                                        |

## Which to use

Depending on your need you should be able to choose between each of the models described, you will just need to be aware of their adavantages and disadvantages so you can know what to expect.

In general, I personally prefer the datastructure that Relational Databases provide, and in almost all of my projects I tend to use this kind of model, however having the ability to change your schema easily can be very useful in certain scenarios.