Skip to content
Notifications You must be signed in to change notification settings

ryukgod26/Database-from-scratch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

c1c7804 · · Sep 18, 2025

History

29 Commits
Sep 18, 2025
Sep 18, 2025
Sep 18, 2025
Sep 18, 2025
Sep 13, 2025
Sep 18, 2025

Repository files navigation

Database-from-scratch

This project is a simple, persistent, single-file database implemented from scratch in C, inspired by SQLite. It features a command-line interface (REPL) and uses a B-Tree data structure for efficient data storage and retrieval.

Features

  • Persistent Storage: The database is saved to a single file.
  • B-Tree Implementation: Data is organized in a B-Tree to allow for efficient insertions and lookups, even as the database grows.
  • REPL Interface: Interact with the database through a simple command-line shell.
  • Basic SQL Commands:
    • insert > <email>: Inserts a new record.
    • select: Retrieves and displays all records.
  • Meta-Commands:
    • .exit: Exits the database shell, saving all changes to the file.
    • .constants: Displays the size of various data structures.
    • .visualize: Prints a visual representation of the B-Tree structure.

How to Build and Run

Prerequisites

  • A C compiler, such as GCC.

Compilation

Navigate to the project directory and compile the source files using the following command:

gcc main.c -o db_executable

This will create an executable file named db_executable.

Running the Database

To start the database shell, run the compiled executable and provide a name for your database file. If the file does not exist, it will be created.

./db_executable my_database.db

You will then see the db >gt; prompt, where you can enter commands.

Usage Example

Here is a sample session demonstrating the basic functionality:

$ ./db_executable test.db
db > insert 1 john [email protected]
db > insert 2 jane [email protected]
db > insert 3 cstack [email protected]
db > select
id:1 ,Username:john, Email:[email protected] 
id:2 ,Username:jane, Email:[email protected] 
id:3 ,Username:cstack, Email:[email protected] 
db > .visualize
Tree: 
- Internal (size 1)
  - leaf (size 2)
   - 1
   - 2
  - key 2
  - leaf (size 1)
   - 3
db > .exit
Database Closed Successfully.
sed Successfully.
sed Successfully.

Project Structure

  • main.c: Contains the main function, the REPL (Read-Eval-Print Loop), and handles user input parsing.
  • inputBuffer.h: The core of the database. It defines all data structures (Row, Pager, Table, Cursor), constants for node layouts, and implements all the logic for:
    • Serialization/Deserialization of rows.
    • Page management and file I/O (Pager).
    • B-Tree operations (insertion, searching, node splitting).
    • Statement preparation and execution.
  • README.md: This documentation file.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages