MODULO System Design: Comprehensive Architecture MasterclassLAB

Relational Databases Unleashed: Mastering SQL Basics and ACID Properties

Introduction to Relational Databases

A relational database is a type of database that stores and provides access to data points that are related to each other. It is based on the relational model, which organizes data into one or more tables, with each table having rows and columns. Each row in the table represents a single record, and each column represents a field or attribute of that record.

SQL Basics

SQL (Structured Query Language) is a programming language designed for managing and manipulating data in relational databases. It is a standard language for accessing, managing, and modifying data in relational database management systems.

Basic SQL Syntax

SQL syntax is composed of several elements:

  • Commands: SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, etc.
  • Clauses: WHERE, FROM, GROUP BY, HAVING, ORDER BY, etc.
  • Functions: SUM, COUNT, AVG, MAX, MIN, etc.
  • Operators: AND, OR, NOT, IN, LIKE, BETWEEN, etc.

SQL Data Types

SQL supports various data types, including:

  • Integer: whole numbers, e.g., 1, 2, 3, etc.
  • String: character strings, e.g., 'hello', 'world', etc.
  • Date: date values, e.g., '2022-01-01', '2022-12-31', etc.
  • Time: time values, e.g., '12:00:00', '23:59:59', etc.
  • Boolean: true or false values

SQL Queries

A SQL query is a request to retrieve data from a database. There are several types of SQL queries:

  • SELECT: retrieves data from a database table
  • INSERT: inserts new data into a database table
  • UPDATE: updates existing data in a database table
  • DELETE: deletes data from a database table

SQL Query Examples

-- SELECT query
SELECT * FROM customers WHERE country='USA';

-- INSERT query
INSERT INTO customers (name, email, country) VALUES ('John Doe', 'john.doe@example.com', 'USA');

-- UPDATE query
UPDATE customers SET country='Canada' WHERE name='John Doe';

-- DELETE query
DELETE FROM customers WHERE name='John Doe';

Relational Database Management Systems

There are several popular relational database management systems (RDBMS):

  • MySQL: an open-source RDBMS widely used for web applications
  • PostgreSQL: an open-source RDBMS known for its reliability and data integrity

MySQL vs. PostgreSQL

| Feature | MySQL | PostgreSQL | | --- | --- | --- | | Licensing | Open-source (GPL) | Open-source (permissive) | | Scalability | Horizontal partitioning | Vertical partitioning | | Data Types | Limited support for advanced data types | Extensive support for advanced data types | | ACID Compliance | Partially compliant | Fully compliant |

ACID Properties

ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties that ensure database transactions are processed reliably.

  • Atomicity: ensures that database transactions are executed as a single, indivisible unit
  • Consistency: ensures that database transactions maintain the integrity of the database
  • Isolation: ensures that database transactions are executed independently and concurrently
  • Durability: ensures that database transactions are permanent and survive system failures

ACID Property Examples

-- Atomicity example
BEGIN TRANSACTION;
INSERT INTO customers (name, email, country) VALUES ('John Doe', 'john.doe@example.com', 'USA');
INSERT INTO orders (customer_id, order_date) VALUES (1, '2022-01-01');
COMMIT TRANSACTION;

-- Consistency example
CREATE TABLE customers (
  id INT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) UNIQUE NOT NULL,
  country VARCHAR(255) NOT NULL
);

-- Isolation example
BEGIN TRANSACTION;
UPDATE customers SET country='Canada' WHERE name='John Doe';
COMMIT TRANSACTION;

-- Durability example
CREATE TABLE customers (
  id INT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) UNIQUE NOT NULL,
  country VARCHAR(255) NOT NULL
);

Conclusion

Relational databases and SQL basics are fundamental concepts in database management. Understanding the principles of relational databases, SQL syntax, and ACID properties is crucial for designing and developing robust database systems. MySQL and PostgreSQL are two popular RDBMS that offer a range of features and capabilities for managing relational databases.

⚔️ YOUR MISSION

Design a Relational Database Schema

Design a relational database schema for an e-commerce application that includes the following entities:

  • Customers
  • Orders
  • Products
  • Order Items

Define the tables, columns, data types, and relationships between the entities. Use MySQL or PostgreSQL as the RDBMS.

Design response
Read the exercise prompt
-- Create the customers table
CREATE TABLE customers (
  id INT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) UNIQUE NOT NULL,
  country VARCHAR(255) NOT NULL
);

-- Create the orders table
CREATE TABLE orders (
  id INT PRIMARY KEY,
  customer_id INT NOT NULL,
  order_date DATE NOT NULL,
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

-- Create the products table
CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  price DECIMAL(10, 2) NOT NULL
);

-- Create the order_items table
CREATE TABLE order_items (
  id INT PRIMARY KEY,
  order_id INT NOT NULL,
  product_id INT NOT NULL,
  quantity INT NOT NULL,
  FOREIGN KEY (order_id) REFERENCES orders(id),
  FOREIGN KEY (product_id) REFERENCES products(id)
);
WAITING FOR SIGNAL...
BACKNEXT STAGE