Databases 19 min read

Building a Simple NoSQL Database in Python: Concepts, Schemas, and Code

This article explains what NoSQL means, contrasts it with SQL and relational databases, walks through schema design, primary keys, indexing, and then demonstrates how to implement a lightweight key‑value NoSQL store in pure Python with full command parsing and handling.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Building a Simple NoSQL Database in Python: Concepts, Schemas, and Code

What Is NoSQL?

NoSQL has become a buzzword, but what does it actually refer to? It denotes databases that do not rely on the traditional relational model and often provide flexible schemas. The article uses pure Python – described as "lightly structured pseudo‑code" – to build a simple NoSQL database that illustrates these ideas.

OldSQL

SQL (Structured Query Language) is the language used to retrieve data from relational database management systems (RDBMS) such as MySQL, MS SQL Server, and Oracle. An RDBMS stores data in tables composed of rows and columns, each column having a type. A table’s schema defines the column names and types, and a primary key uniquely identifies each row (e.g., VIN in a Car table).

Querying

SQL queries let us ask questions of the database, for example selecting Make and Model from the Car table. The article shows how to translate basic SQL clauses into Chinese and demonstrates filtering by year and color.

Relations

When tracking service history for cars, storing redundant vehicle information in each service record is inefficient. The solution is to split data into two tables – Vehicle (VIN, Make, Model, Year, Color) and ServiceHistory (VIN, Service Performed, Mechanic, Price, Date) – and join them on the VIN primary key.

Schemas

A table’s schema describes column names, data types, nullability, and uniqueness constraints. Changing a schema (e.g., adding an Age column) requires careful planning, possible data migration, and rollback strategies.

Key/Value Stores

Before NoSQL, key/value stores like memcached provided schema‑less storage using hash tables. A simple Python dictionary can serve as the data store, supporting string keys and integer, string, or list values. An ASCII‑based TCP/IP interface lets clients interact via telnet.

Commands Supported

PUT

– insert a new entry (Key, Value) GET – retrieve a stored value (Key) PUTLIST – insert a new list entry (Key, Value) APPEND – add an element to an existing list (Key, Value) INCREMENT – increase an integer value (Key) DELETE – remove an entry (Key) STATS – request command success/failure statistics

Message Structure

Request messages consist of a command, key, value, and value type, separated by semicolons. Even if optional fields are omitted, three semicolons must appear. Response messages contain a success flag (True/False) and either a result value or an error message, also separated by a semicolon.

Show Me The Code!

The article presents the full source (about 180 lines) of a minimal TCP/IP server that implements the key/value store. It includes import statements, data initialization, a COMMAND_HANDLERS lookup table that maps commands to handler functions, and a simple socket loop.

Command Parser

The parser interprets incoming messages, performs type conversion (e.g., splitting comma‑separated strings into lists, casting strings to integers), and dispatches to the appropriate handler.

Command Handlers

Handlers such as handle_get, handle_put, and handle_append contain straightforward logic with error checking. The code demonstrates multiple assignment and code reuse to keep the implementation clear.

How Is This a Database?

Although not a full RDBMS, the program qualifies as a NoSQL database because it stores arbitrary data without a fixed schema, performs minimal type checking, and can be extended with a schema for more structured queries.

Querying Data

Using VIN as the key, values are stored as lists (e.g., ['Lexus', 'RX350', 2013, 'Black']). Retrieving specific fields requires scanning the entire data set, which is less efficient than indexed table scans. The article discusses trade‑offs between flexibility and query performance, and mentions namespace concepts and JSON‑shaped data to improve queryability.

Summary

By the end, readers should understand what NoSQL means, how SQL and relational databases work, how to retrieve data with SQL queries, and how to build a toy NoSQL database in Python. The discussion highlights the balance between simplicity and searchability, and encourages further exploration of database concepts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonSQLindexingdatabaseNoSQLschemakey-value store
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.