# Relational vs non-relational databases Antonio, Evgeny, Rosie, Saki --- ## Relational Databases - What is a database? "A structured collection of data" - A Database Management System (DBMS): A piece of software that manages access to a pile of structured data Examples of Relational Databases: Oracle, MySQL, Microsoft SQL Server, and PostgreSQL --- ## How is data structured in a SQL database? ![](https://i.imgur.com/On6HU4C.png) ![](https://i.imgur.com/nmPbvlf.png) --- ![](https://i.imgur.com/xJKkSYM.png) --- ## A bit of history ![](https://media.giphy.com/media/xT5LMtHmmaTZgm9ELC/giphy-downsized.gif) --- ## What is NoSQL? --- When people use the term “NoSQL database”, they typically use it to refer to any non-relational database. Some say the term “NoSQL” stands for “non SQL” while others say it stands for “not only SQL.” Either way, most agree that NoSQL databases are databases that store data in a format other than relational tables. --- A common misconception is that NoSQL databases or non-relational databases don’t store relationship data well. NoSQL databases can store relationship data—they just store it differently than relational databases do. In fact, when compared with SQL databases, many find modeling relationship data in NoSQL databases to be easier than in SQL databases, because related data doesn’t have to be split between tables. NoSQL data models allow related data to be nested within a single data structure. --- ## What are the Types of NoSQL Databases? ## Most used NoSQL Databases --- ## Document-oriented databases: Mixed Structure - Store data in documents similar to JSON (JavaScript Object Notation) objects. - The values can typically be a variety of types including things like strings, numbers, booleans, arrays, or objects, and their structures typically align with objects developers are working with in code. --- ## Document-oriented databases: Mixed Structure - Because of their variety of field value types and powerful query languages, document databases are great for a wide variety of use cases and can be used as a general purpose database. - They can horizontally scale-out to accomodate large data volumes. **MongoDB** is an example of a document database. --- ![document model](https://i.imgur.com/tHlSoE5.png) --- ## Key-value databases - Simpler type of database where each item contains keys and values. - Key-value databases are great for use cases where you need to store large amounts of data but you don’t need to perform complex queries to retrieve it. - Common use cases include storing user preferences or caching. - **Redis** and **DynanoDB** are popular key-value databases. --- ![](https://miro.medium.com/max/700/1*jqaGa_wm33d5d4PCUDXIIg.png) --- ## Wide-column stores: - Hold data in tables that have a very large number of columns. - Each storage block contains data from a single column and each column is treated separately. --- ## Graph databases: A graph-based database is a network database that stores data elements in “graph” structures and make it possible to create associations between the nodes, ultimately serving as a basis for recommendation engines or social networks. <img src="https://miro.medium.com/max/700/0*pTNeaEcncpeUa_8n.png" width="300px"> --- **What are some advantages of relational data?** ![](https://i.imgur.com/CQ7B2kY.jpg) --- ***Top Advantages of Relational Database*** ![](https://i.imgur.com/GhvRaES.jpg) --- ***1. Simple Model*** A Relational Database system is the most simple model, as it does not require any complex structuring or querying processes. As the structure is simple, it is sufficient to be handled with simple SQL queries and does not require complex queries to be designed. --- ***2. Data Accuracy*** In the relational database system, there can be multiple tables related to one another with the use of a primary key and foreign key concepts. This makes the data to be non-repetitive. There is no chance for duplication of data. --- ***3. Easy Access to Data*** In the Relational Database System, there is no pattern or pathway for accessing the data. Anyone who accesses the data can query any table in the relational database. --- ***4. Data Integrity*** Relational reliability amongst the tables in the database helps in avoiding the records from being imperfect, isolated or unrelated. --- ***5. Flexibility*** You can insert, update or delete tables, columns or individual data in the given database system promptly and easily. There is no boundary on the number of rows, columns or tables a relational database can hold. --- ***6. High Security*** As the data is divided amongst the tables of the relational database system, it is possible to make a few tables to be tagged as confidential and others not. --- ***7. Feasible for Future Modifications*** As the relational database system holds records in separate tables based on their categories, it is straightforward to insert, delete or update records that are subjected to the latest requirements. --- ## Are there disadvantages to SQL databases? Why would we use a noSQL database instead? ###### - noSQL databases keep everything in memory which is very fast, not on disks, which is a lot slower. ###### - an SQL database requires creating fixed schema. A noSQL has flexible schema, so you don't need to plan in advance what data will added in. SQL databases are more rigid in structure, and the original schema need to be edited to add in new types of information. --- - noSQL databases keep information together, rather than separating it into into separate tables, and 'joining' it together. This can be a bit easier to view. - a docutment database (like Mongo) - stores your data in collections made out of individual documents - so it stores all of the data about one thing together, rather than spreading it out. --- A test case - sometimes it can be beneficial ![](https://i.imgur.com/IFyKaie.png) --- How SQL would hold the data: In a typical relational store, each of these boxes would be a table. You’d have ###### - a tv_shows table, ###### - a seasons table with a foreign key into tv_shows, ###### - an episodes table with a foreign key into seasons ###### - reviews and ###### - cast_members tables with foreign keys into episodes. So to get all the information for a TV show, you’re looking at a five-table join. --- How noSQL would hold the data: We could also model this data as a set of nested hashes. The set of information about a particular TV show is one big nested key/value data structure. Inside a TV show, there’s an array of seasons, each of which is also a hash. Within each season, an array of episodes, each of which is a hash, and so on. This is how MongoDB models the data. Each TV show is a document that contains all the information we need for one show. --- ![](https://i.imgur.com/OkBxFsW.png) --- Key takeaway: SQL and noSQL seem to be appropriate for different things, but SQL databases seem to more consistent and better in most cases. --- ![](https://media.giphy.com/media/3oz8xIsloV7zOmt81G/giphy.gif)
{"metaMigratedAt":"2023-06-15T23:08:29.815Z","metaMigratedFrom":"Content","title":"Relational vs non-relational databases","breaks":true,"contributors":"[{\"id\":\"7da4162a-2657-4e16-a79c-aef961d4b0dc\",\"add\":2226,\"del\":19},{\"id\":\"e766c134-70b5-4e90-ae12-2b83144c7e0c\",\"add\":794,\"del\":292},{\"id\":\"7bfddbf1-39cc-46b1-8474-80064974de82\",\"add\":4188,\"del\":1513},{\"id\":\"ed85db99-8527-44b2-9bd7-ec1d8e4a4689\",\"add\":2142,\"del\":358}]"}
    354 views