Databases

Overview

Relational Databases (SQL)

Example Schema

Advantages

Common SQL Databases

Type Description
MySQL Most widely used, open-source, free
MSSQL Microsoft's SQL server for Windows
Oracle Enterprise-grade, highly reliable, expensive
PostgreSQL Open-source, highly extensible
Others SQLite, MariaDB, Amazon Aurora, Azure SQL

Non-Relational Databases (NoSQL)

NoSQL Models

Model Description
Key-Value Each item is stored as a key-value pair (JSON, XML)
Document-Based Complex JSON objects with metadata
Wide-Column Data stored in tables with dynamic columns
Graph Nodes and edges representing relationships

Key-Value Example (JSON)

{
  "100001": {
    "date": "01-01-2021",
    "content": "Welcome to this web application."
  },
  "100002": {
    "date": "02-01-2021",
    "content": "This is the first post on this web app."
  },
  "100003": {
    "date": "02-01-2021",
    "content": "Reminder: Tomorrow is the ..."
  }
}

Pasted image 20250924163751.png|450

The Document-Based model stores data in complex JSON objects and each object has certain meta-data while storing the rest of the data similarly to the Key-Value model.

Common NoSQL Databases

Type Description
MongoDB Free and open-source NoSQL database. Most common NoSQL, document-based, JSON storage
ElasticSearch Free and open-source NoSQL database. Fast search engine, handles big datasets
Apache Cassandra Free and open-source NoSQL database. Scalable and resilient
Others Redis, Neo4j, CouchDB, Amazon DynamoDB

Database Use in Web Applications

PHP Example

For example, within a PHP web application, once MySQL is up and running, we can connect to the database server with:

Connect to DB Server

$conn = new mysqli("localhost", "user", "pass");

Then, we can create a new database with:

$sql = "CREATE DATABASE database1";
$conn->query($sql);

After that, we can connect to our new database, and start using the MySQL database through MySQL syntax, right within PHP, as follows:

$conn = new mysqli("localhost", "user", "pass", "database1");
$query = "select * from table_1";
$result = $conn->query($query);

Search Input Example

Web applications usually use user-input when retrieving data. For example, when a user uses the search function to search for other users, their search input is passed to the web application, which uses the input to search within the database(s).

$searchInput =  $_POST['findUser'];
$query = "select * from users where name like '%$searchInput%'";
$result = $conn->query($query);

while($row = $result->fetch_assoc() ){
    echo $row["name"]."<br>";
}

Exercise

What type of database is Google's Firebase Database?

Google Firebase offers primarily NoSQL databases. Specifically, it provides two main options:

flag: NoSQL