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.
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.
- 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
- 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
# Clone the repository
git clone https://github.com/philbotar/relational-database-rust
cd rust-db/rust-database
# Build the project
cargo build# Run the REPL
cd rust-db/rust-database
cargo runStart the interactive REPL with cargo run. The REPL supports:
- 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
- Comparison:
=,!=,>,<,>=,<= - Logical:
AND,OR
NOT NULLUNIQUEDEFAULTINDEX
STRING- text valuesINTEGER- 64-bit integersBOOLEAN- true/false values
am
EXITorQUIT- exit the REPLHELP- show available commandsTABLESor.TABLES- list tables
-- 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;- 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