Skip to content
On this page

Database

When a program runs, data is stored in memory. Typically, when the program terminates, it must save this data to disk, whether to a local disk or a server over the network, ultimately writing it to a disk file.

Defining the storage format for data is a significant challenge. For example, to save the scores of all students in a class:

NameScore
Michael99
Bob85
Bart59
Lisa87

We could save this in a text file, with each student on a new line, separated by a comma:

Michael,99
Bob,85
Bart,59
Lisa,87

Alternatively, we can use JSON format:

json
[
    {"name":"Michael","score":99},
    {"name":"Bob","score":85},
    {"name":"Bart","score":59},
    {"name":"Lisa","score":87}
]

While we can define various storage formats, issues arise:

  • You must implement your own storage and retrieval, while JSON is standardized; custom formats can vary widely.
  • Quick querying isn't possible; you must load all data into memory for traversal, which may not be feasible for large datasets.

To simplify data storage and retrieval while enabling quick queries based on conditions, databases (Database) were developed specifically for centralized storage and querying.

The history of database software dates back to the 1950s. After going through network and hierarchical databases, relational databases emerged in the 1970s based on the relational model.

The relational model is grounded in complex mathematical theory but is conceptually straightforward. For instance, consider a primary school with three grades, represented in a table:

grade

Each grade has multiple classes, which can be represented in another table:

class

These two tables have a mapping relationship: using Grade_ID, you can find all corresponding classes in the class table. In relational databases, this one-to-many relationship between tables is fundamental.

Using a grade ID allows you to query all class records, written as an SQL statement:

sql
SELECT * FROM classes WHERE grade_id = '1';

The result is another table:

grade_idclass_idname
111Grade 1 Class 1
112Grade 1 Class 2
113Grade 1 Class 3

Similarly, a record in the Class table can link to multiple records in the Student table.

NoSQL

You may have heard of NoSQL databases, which are often marketed as faster and more scalable than relational databases. However, understanding SQL is crucial before delving into NoSQL; don’t be misled by marketing.

Types of Databases

If you're using a relational database, you must choose one. Common options include:

Commercial Databases:

  • Oracle: High-end and widely recognized.
  • SQL Server: Microsoft’s product tailored for Windows.
  • DB2: IBM's product, known for its robustness.
  • Sybase: Once a close partner with Microsoft, now struggling.

These databases are proprietary and paid, which provides support for issues but is costly for widespread deployment. Hence, many companies like Google and Facebook opt for free open-source databases:

  • MySQL: Widely used and reliable.
  • PostgreSQL: Academically inclined but very capable.
  • SQLite: An embedded database suitable for desktop and mobile applications.

As a full-stack JavaScript engineer, you might choose SQLite for its ease of use without installation. However, for production environments, MySQL or PostgreSQL are advisable.

Database has loaded