# Database Management/NoSQL

This lesson introduces NoSQL databases.

## Objectives and Skills

Objectives and skills for this lesson include:
* Understand the features and uses of document databases
* Understand the features and uses of key-value databases
* Understand the features and uses of graph databases
* Practice using a variety of NoSQL databases

## Readings

* Wikipedia: NoSQL
* Wikipedia: Document-oriented database
* Wikipedia: Key–value database
* Wikipedia: Graph database

## Multimedia

* YouTube: An Introduction To NoSQL Databases
* YouTube: SQL vs NoSQL
* YouTube: An introduction to Wikidata

## Activities

* Review Wikipedia: MongoDB. Identify the main features and uses of MongoDB.
* Practice using MongoDB.
** Review MongoDB.
** Use Docker Playground to implement a MongoDB database environment.
** Use the mongo command to connect to MongoDB.
** Create and query a MongoDB collection.
** Insert, update, and remove documents.
* Review Wikipedia: Redis. Identify the main features and uses of Redis.
* Practice using Redis.
** Review Redis.
** Use Docker Playground to implement a Redis database environment.
** Use the redis-cli command to connect to Redis.
** Create and query a Redis database.
** Create, update, and delete Redis keys.
* Review Wikipedia: Wikidata. Identify the main features and uses of Wikidata.
* Practice using Wikidata.
** Review Research in programming Wikidata.
** Use the Wikidata Query Service to query the Wikidata database. Execute the linked SPARQL query and view the results.

## Lesson Summary
### NoSQL

* A NoSQL database provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases.
* NoSQL systems are also sometimes called "Not only SQL" to emphasize that they may support SQL-like query languages or sit alongside SQL databases.
* NoSQL databases are increasingly used in big data and real-time web applications.
* NoSQL databases support simplicity of design, simpler "horizontal" scaling to clusters of machines, finer control over availability and limiting the object-relational impedance mismatch.
* The data structures used by NoSQL databases (e.g. key–value pair, wide column, graph, or document) are different from those used by default in relational databases, making some operations faster in NoSQL. The particular suitability of a given NoSQL database depends on the problem it must solve.
* Many NoSQL stores compromise consistency (in the sense of the CAP theorem) in favor of availability, partition tolerance, and speed.
* Barriers to the greater adoption of NoSQL stores include the use of low-level query languages (instead of SQL, for instance), lack of ability to perform ad hoc joins across tables, lack of standardized interfaces, and huge previous investments in existing relational databases.
* Most NoSQL stores lack true ACID transactions. Instead, most NoSQL databases offer a concept of "eventual consistency", in which database changes are propagated to all nodes "eventually" (typically within milliseconds), so queries for data might not return updated data immediately or might result in reading data that is not accurate, a problem known as stale reads. Additionally, some NoSQL systems may exhibit lost writes and other forms of data loss.

### Document-Oriented Database

* A document-oriented database, or document store, is a computer program and data storage system designed for storing, retrieving and managing document-oriented information, also known as semi-structured data.
* Document-oriented databases are one of the main categories of NoSQL databases. Graph databases are similar, but add another layer, the relationship, which allows them to link documents for rapid traversal.
* Document-oriented databases are inherently a subclass of the key-value store, another NoSQL database concept. The difference lies in the way the data is processed; in a key-value store, the data is considered to be inherently opaque to the database, whereas a document-oriented system relies on internal structure in the document in order to extract metadata that the database engine uses for further optimization.
* Document databases contrast strongly with the traditional relational database (RDB). Relational databases generally store data in separate tables that are defined by the programmer, and a single object may be spread across several tables. Document databases store all information for a given object in a single instance in the database, and every stored object can be different from every other. This eliminates the need for object-relational mapping while loading data into the database.

### Key-Value Database

* A key–value database, or key–value store, is a data storage paradigm designed for storing, retrieving, and managing associative arrays, and a data structure more commonly known today as a dictionary or hash table. Dictionaries contain a collection of objects, or records, which in turn have many different fields within them, each containing data. These records are stored and retrieved using a key that uniquely identifies the record, and is used to find the data within the database.
* Key–value databases work in a very different fashion from the better known relational databases (RDB). RDBs predefine the data structure in the database as a series of tables containing fields with well defined data types. Exposing the data types to the database program allows it to apply a number of optimizations. In contrast, key–value systems treat the data as a single opaque collection, which may have different fields for every record.

### Graph Database

* A graph database (GDB) is a database that uses graph structures for semantic queries with nodes, edges, and properties to represent and store data.
* A key concept of the system is the graph (or edge or relationship). The graph relates the data items in the store to a collection of nodes and edges, the edges representing the relationships between the nodes. The relationships allow data in the store to be linked together directly and, in many cases, retrieved with one operation.
* Graph databases hold the relationships between data as a priority. Querying relationships is fast because they are perpetually stored in the database. Relationships can be intuitively visualized using graph databases, making them useful for heavily inter-connected data.

## Key Terms
### ACID (Atomicity, Consistency, Isolation and Durability)

    A set of RDBMS properties of database transactions intended to guarantee validity even in the event of errors.

### BASE model (Basically Available, Soft state, Eventual consistency)

    Used by NoSQL databases to achieve improved scalability using larger amounts of data.

### CAP theorem (Brewer's theorem)

    States that it is impossible for a distributed data store to simultaneously provide more than two out of the following three guarantees: Consistency, Availability of resources and Partition tolerance.

### NoSQL

    Provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases.

## See Also

* MongoDB
* Redis
* Wikidata
* FreeCodeCamp: The basics of NoSQL databases
* Geeks for Geeks: SQL vs NoSQL: Which one is better to use?
* Guru99: NoSQL Tutorial: Learn NoSQL Features, Types, What is, Advantages