A lightweight, file-based relational database engine implemented in C++ with SQL-like query support. This project provides a simple yet powerful database management system that supports CRUD operations through an interactive SQL command-line interface.
Educational Project: Built as part of the ITI Object-Oriented Programming course to demonstrate database management system concepts, design patterns, and advanced C++ programming techniques.
- SQL-like Command Interface: Interactive CLI with familiar SQL syntax
- Data Types Support:
- Nullable (
NullType) - Integer (
INT) - Double precision floating point (
DOUBLE) - Single character (
CHAR) - Variable-length text (
VARCHAR,TEXT) - Date (
DATE)
- Nullable (
- Core SQL Operations:
CREATE TABLE- Define tables with columns and constraintsINSERT INTO- Add records to tablesSELECT- Query data with filtering and conditionsUPDATE- Modify existing recordsDELETE- Remove records from tables
- Advanced Features:
- Primary key constraints (single and composite)
- WHERE clause filtering with comparison operators (
=,!=,<,>,<=,>=) - Logical operators (
AND,OR) - Persistent file-based storage
- NULL value support
- Automatic table metadata management
- Modern C++ Implementation: Leverages C++17 features including
std::variant,std::filesystem, and lambda expressions for clean, type-safe code - Zero External Dependencies: Completely self-contained using only STL, making it highly portable and easy to deploy
- Robust Parser Design: Hand-crafted SQL parser with comprehensive syntax validation and error handling
- Memory Efficient: Smart use of move semantics and RAII patterns for optimal resource management
- Modular Architecture: Clean separation of concerns with dedicated parsers, models, and utilities
- Full SQL Lifecycle: Complete CRUD operations matching industry-standard SQL syntax
- Type System: Rich data type support including custom Date class with proper comparison operators
- Query Engine: Sophisticated WHERE clause evaluation with complex boolean logic
- Persistence Layer: Reliable file-based storage with automatic serialization/deserialization
- User Experience: Interactive CLI with help system and intuitive error messages
- Data Integrity: Primary key enforcement and NULL value handling
- Demonstrates advanced OOP concepts (encapsulation, abstraction, polymorphism)
- Showcases design patterns (Parser pattern, Catalog/Registry pattern)
- Implements data structures (variant types, AST)
- Explores file I/O and serialization techniques
- Practices string parsing and tokenization algorithms
- Applies software engineering principles (SOLID, DRY)
- C++ compiler with C++17 support or higher (GCC, Clang, MSVC)
- Standard C++ libraries
-
Clone the repository
git clone https://github.com/yourusername/mini_db_engine.git cd mini_db_engine -
Compile the project
Using g++ (Linux/macOS/MinGW)
g++ -std=c++17 src/main.cpp -o mini_db
Using clang++
clang++ -std=c++17 src/main.cpp -o mini_db
Using MSVC (Windows)
cl /EHsc /std:c++17 src/main.cpp /Fe:mini_db.exe
Using CMake (Recommended)
mkdir build && cd build cmake .. cmake --build .
Linux/macOS:
./mini_dbWindows:
mini_db.exeYou'll be greeted with an interactive SQL prompt:
Welcome to mini database engine
Type 'help' for commands
SQL>
Try these commands to get started:
CREATE TABLE students (id INT PRIMARY KEY, name VARCHAR(50), grade DOUBLE);
INSERT INTO students (id, name, grade) VALUES (1, 'Alice', 95.5);
SELECT * FROM students;CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(255),
age INT,
created_at DATE
);CREATE TABLE enrollments (
student_id INT,
course_id INT,
grade DOUBLE,
PRIMARY KEY (student_id, course_id)
);INSERT INTO users (id, name, email, age, created_at)
VALUES (1, 'John Doe', 'john@example.com', 30, '2024-01-15');
INSERT INTO users (id, name, email, age)
VALUES (2, 'Jane Smith', 'jane@example.com', 28);-- Select all records
SELECT * FROM users;
-- Select specific columns
SELECT name, email FROM users;
-- Filtering with WHERE clause
SELECT * FROM users WHERE age > 25;
-- Complex conditions
SELECT name, email FROM users
WHERE age >= 25 AND age <= 35;
-- Multiple conditions
SELECT * FROM users
WHERE age > 20 OR name = 'John Doe';UPDATE users SET age = 31 WHERE id = 1;
UPDATE users SET email = 'newemail@example.com', age = 29
WHERE name = 'Jane Smith';DELETE FROM users WHERE id = 2;
DELETE FROM users WHERE age < 18;mini_db_engine/
βββ README.md # Project documentation
βββ LICENSE # License file
βββ src/
β βββ main.cpp # Entry point and CLI loop
β βββ setup_test_data.cpp # Test data setup utilities
β βββ README.md # Source documentation
βββ include/
β βββ models.cpp # Core data structures (Table, Column, Row, etc.)
β βββ Helper.cpp # Utility functions for parsing and file I/O
β βββ CreateParse.cpp # CREATE TABLE parser
β βββ InsertParser.cpp # INSERT INTO parser
β βββ SelectParser.cpp # SELECT query parser
β βββ UpdateParser.cpp # UPDATE statement parser
β βββ DeleteParser.cpp # DELETE statement parser
β βββ README.md # Include documentation
βββ data/
βββ README.md # Data directory documentation
- Separation of Concerns: Each parser handles one SQL command type
- Object-Oriented Design: Leverages classes, inheritance, and polymorphism
- Type Safety: Uses C++17
std::variantfor type-safe value storage - File-Based Persistence: Simple, portable storage without external dependencies
-
Models (models.cpp)
Value: Variant type supporting multiple data types (Int, Double, Char, Text, Date, Null)Column: Table column definition with name, type, and constraintsRow: Table row representation as a vector of valuesTable: Complete table structure with metadata and rowsCatalog: Database-wide table registry and managementAST: Abstract syntax tree for parsed queries
-
Parsers
- CreateParser: Handles table creation with column definitions and constraints
- InsertParser: Processes INSERT statements with value validation
- SelectParser: Executes SELECT queries with WHERE clause evaluation
- UpdateParser: Modifies existing records based on conditions
- DeleteParser: Removes records matching WHERE criteria
- Each parser validates syntax, converts queries to AST, and executes operations
-
Helper Utilities (Helper.cpp)
- String manipulation (trim, split, case conversion)
- SQL parsing (parentheses matching, quote handling)
- File I/O for persistent storage
- Table serialization/deserialization
- Query validation and formatting
- Help text and error message generation
-
Storage Engine
- File-based persistence in
data/directory - Each table stored in a separate text file
- Automatic loading of existing tables on startup
- Human-readable format for easy debugging
- Atomic writes to prevent data corruption
- File-based persistence in
| Command | Description |
|---|---|
help or ? |
Display available commands and syntax |
exit or quit |
Exit the database engine |
CREATE TABLE ... |
Create a new table |
INSERT INTO ... |
Insert data into a table |
SELECT ... |
Query data from a table |
UPDATE ... |
Update existing records |
DELETE FROM ... |
Delete records from a table |
| Type | Description | Example |
|---|---|---|
NullType |
nullable values | NULL |
INT |
Integer numbers | 42, -100 |
DOUBLE |
Floating-point numbers | 3.14, -0.5 |
CHAR |
Single character | 'A', 'x' |
VARCHAR(n) |
Variable-length string (max n chars) | 'Hello' |
TEXT |
Unlimited text | 'Long text...' |
DATE |
Date in YYYY-MM-DD format | '2024-01-15' |
- Comparison:
=,!=,<,>,<=,>= - Logical:
AND,OR - Value Types: Numbers, strings (quoted), dates (quoted), NULL
- Primary Key: Ensures unique identification of records
- Single column:
id INT PRIMARY KEY - Composite:
PRIMARY KEY (col1, col2)
- Single column:
All data is automatically persisted to files in the data/ directory:
- Automatic Saving: Tables are saved after each modification (INSERT, UPDATE, DELETE)
- Auto-Loading: Existing tables are loaded automatically on startup
- File-Per-Table: Each table is stored in a separate file for isolation
- Human-Readable Format: Data files can be inspected and manually edited if needed
- Metadata Storage: Column definitions and constraints are stored with the data
TableName: users
Columns: id:INT:PK, name:VARCHAR(100), email:VARCHAR(255), age:INT
Rows:
1, "John Doe", "john@example.com", 30
2, "Jane Smith", "jane@example.com", 28
Contributions are welcome! Here are some areas for improvement:
- Add support for JOIN operations
- Implement indexes for faster queries
- Add more constraints (FOREIGN KEY, UNIQUE, NOT NULL)
- Support for aggregate functions (COUNT, SUM, AVG, etc.)
- Transaction support
- Multi-user concurrency control
- Query optimization
This project is licensed under the terms specified in the LICENSE file.
Developed as part of the ITI Object-Oriented Programming course.
![]() Ali Elshorpagi |
![]() Mohamed Wahba |
![]() Hazem Ibrahim |
As an educational project focused on core database concepts, certain advanced features are intentionally simplified:
- Single-User Mode: Designed for learning, not concurrent access
- Immediate Persistence: Changes committed instantly (educational approach to understand persistence)
- Schema Simplicity: Fixed table structures after creation (demonstrates core CREATE operation)
- Query Scope: Focused on fundamental single-table operations for clarity
- Optimization: Emphasis on correctness over performance for educational clarity
These design decisions allow the codebase to remain readable and educational while demonstrating fundamental database concepts effectively.
- SQL Tutorial - Learn SQL basics
- Database Design - Database design principles
- C++ Reference - C++ language reference
If you encounter any issues or have questions:
- Check the Known Limitations section
- Review the Usage Examples
- Open an issue on GitHub with:
- Your SQL command
- Expected behavior
- Actual behavior
- Error messages (if any)


