Skip to content

philbotar/relational-database-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Relational Database in Rust

Relational Database in Rust is a from‑scratch implementation of a relational database engine written in pure Rust, using only the Rust standard library. It's a project designed to explore how relational database management systems (RDBMS) work under the hood. From schemas and row storage to indexing, constraints, and persistence.

This is not a wrapper over SQLite or an ORM. This is a ground‑up exploration of database internals.

How Relational Databases work under the hood

What do we generally know about Relational Database systems such as MySQL?

  • A database is a collection of Tables
  • Data is presented as a collection of Tables, each of which have rows and columns
  • Users have the ability to manipulate data in tabular form.

Core Components

Storage Layer

  • database.rs
    • create_table
    • update_table_name
    • delete_table
    • get_table
    • get_table_mut
  • table.rs
    • new
    • add_row
    • delete_row
    • edit_row
    • get_row
  • schema.rs
    • new
    • get_column_by_name
    • get_column_by_index
    • get_column_index
  • row.rs
    • new
  • column.rs
    • new
    • not_null
    • unique
    • default
    • index
    • build
  • constraint_state.rs
    • ConstraintKind (enum)
    • Constraint (enum)
    • ConstraintState (struct)
    • from_schema

SQL Pipeline

  • tokenizer.rs - converts SQL string to tokens
  • parser.rs - builds Abstract Syntax Tree from tokens
  • executor.rs - executes queries against the database
  • repl.rs - interactive SQL shell

Installation

# Clone the repository
git clone https://github.com/philbotar/relational-database-rust
cd rust-db/rust-database

# Build the project
cargo build

How to Run

# Run the REPL
cd rust-db/rust-database
cargo run

REPL

Start the interactive REPL with cargo run. The REPL supports:

SQL Statements

  • SELECT: Query data with column projection, WHERE clauses, ORDER BY, and LIMIT
  • INSERT INTO: Add new rows to tables
  • UPDATE ... SET ... WHERE: Modify existing rows
  • DELETE FROM ... WHERE: Remove rows from tables
  • CREATE TABLE: Define new tables with schemas

WHERE Clause Operators

  • Comparison: =, !=, >, <, >=, <=
  • Logical: AND, OR

Column Constraints

  • NOT NULL
  • UNIQUE
  • DEFAULT
  • INDEX

Data Types

  • STRING - text values
  • INTEGER - 64-bit integers
  • BOOLEAN - true/false values

Special Commands

am

  • EXIT or QUIT - exit the REPL
  • HELP - show available commands
  • TABLES or .TABLES - list tables

Examples

-- Create a table with constraints
CREATE TABLE users (id INTEGER NOT NULL UNIQUE, name STRING, age INTEGER);

-- Insert data
INSERT INTO users VALUES (1, 'Alice', 30);
INSERT INTO users VALUES (2, 'Bob', 25);

-- Query data
SELECT * FROM users;
SELECT name, age FROM users WHERE age > 20;
SELECT * FROM users ORDER BY age LIMIT 1;

-- Update data
UPDATE users SET age = 31 WHERE name = 'Alice';

-- Delete data
DELETE FROM users WHERE id = 1;

To-Do

  • Be able to create a schema
  • Be able to create a database
  • Be able to create Tables. Each table has a name, schema and metadata against it.
  • Be able to add rows to a table
  • Add ability to have column constraints
  • Apply Constraints to each row.
  • Add "Check" Constraint (Expressions)
  • Add the ability to have foreign_keys.
  • Persist the data locally.
  • Create a Tokenizer to split Queries into tokens
  • Create a Parser to convert tokens to an Abstract Syntax Tree
  • Be able to perform queries on the data
  • Add ability to have indexes against the data

About

A hobby project of building a database from Scratch in rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages