Introduction to Databases and Persistence
Introduction to Databases and Persistence
Every program that saves data for later use relies on databases. Whether it’s a game saving high scores, a social media app storing user posts, or an e-commerce site managing orders, databases allow programs to store, retrieve, and manage information efficiently.
In this lesson, we’ll explore what databases are, how they work, and how to interact with them using Python and JavaScript.
What is a Database?
A database is an organized collection of data that allows for efficient storage, retrieval, and management. Unlike simple text files, databases provide:
- Structured storage: Data is stored in an organized way.
- Fast retrieval: Queries allow searching and filtering.
- Data persistence: Information remains after the program exits.
Types of Databases
- SQL (Structured Query Language) – Data is stored in tables with predefined structures (like spreadsheets).
- NoSQL (Not Only SQL) – Data is stored in documents, key-value pairs, or graphs, making it more flexible.
SQL Databases: Structured Data Management
SQL databases use tables, rows, and columns to organize data. Examples include SQLite, MySQL, and PostgreSQL.
Creating and Using an SQLite Database in Python
SQLite is a lightweight database that comes built into Python.
import sqlite3
# Connect to a database (or create one if it doesn’t exist)
conn = sqlite3.connect("my_database.db")
cursor = conn.cursor()
# Create a table
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER
)
""")
# Insert data
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Alice", 25))
conn.commit()
# Retrieve data
cursor.execute("SELECT * FROM users")
print(cursor.fetchall())
conn.close()
Key SQL Commands
CREATE TABLE
– Defines the structure of the data.INSERT INTO
– Adds new records.SELECT
– Retrieves data.UPDATE
– Modifies existing records.DELETE
– Removes records.
NoSQL Databases: Flexible Data Storage
NoSQL databases store data in a more flexible, scalable way. Common NoSQL databases include MongoDB, Firebase, and Redis.
Storing and Retrieving Data with MongoDB in JavaScript
MongoDB stores data as JSON-like documents, making it easy to work with in JavaScript.
const { MongoClient } = require("mongodb");
async function run() {
const client = new MongoClient("mongodb://localhost:27017");
await client.connect();
const db = client.db("my_database");
const users = db.collection("users");
// Insert data
await users.insertOne({ name: "Alice", age: 25 });
// Retrieve data
const userList = await users.find().toArray();
console.log(userList);
client.close();
}
run();
SQL vs NoSQL: When to Use What?
Feature | SQL (Relational) | NoSQL (Non-Relational) |
---|---|---|
Structure | Fixed schema (tables, columns) | Flexible schema (documents, key-value, graphs) |
Scalability | Vertical scaling | Horizontal scaling |
Best For | Structured data, transactions | Unstructured data, big data, fast-growing apps |
Why Use a Database Instead of a File?
Feature | Database | File Storage |
---|---|---|
Data Organization | Structured tables/documents | Flat text or binary files |
Performance | Optimized for queries | Slower for large data sets |
Security | Access control, encryption | Basic protection |
Scalability | Can handle millions of records | Limited |
Summary
✅ Databases store and manage data efficiently.
✅ SQL databases use structured tables, while NoSQL databases offer flexibility.
✅ Python works well with SQLite for small projects.
✅ JavaScript integrates well with MongoDB for flexible data storage.
✅ Databases are better than files for large or structured data storage.
Next Steps
Try setting up a small database project! For example:
- Store user scores in a game.
- Save contact information for a mini address book.
What databases have you worked with before? Share your experiences in the comments!
Happy coding!