Complete reference for SQL syntax supported by MiniDB.
- Overview
- Data Types
- SELECT Statement
- INSERT Statement
- UPDATE Statement
- DELETE Statement
- Limitations
- Examples
MiniDB supports a simplified subset of SQL focused on key-value operations. This makes it easy to learn while maintaining the familiar SQL syntax.
Supported Operations:
- ✅ SELECT (point queries and full table scans)
- ✅ INSERT (single row)
- ✅ UPDATE (single row by key)
- ✅ DELETE (single row by key)
Not Yet Supported:
- ❌ CREATE/DROP TABLE (tables created automatically)
- ❌ JOINs
- ❌ Aggregations (COUNT, SUM, AVG)
- ❌ GROUP BY / ORDER BY
- ❌ Subqueries
- ❌ Multiple columns
- ❌ Complex WHERE clauses (only
key = value)
MiniDB uses a simplified key-value model:
- Type: 64-bit signed integer (
int64_t) - Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- Usage: Primary key for each row
- Must be: Unique within a table
Examples:
-- Valid keys
1
42
-100
9999999999
-- Invalid (will cause parse error)
'abc' -- strings not allowed as keys
3.14 -- floats not allowed
NULL -- NULL not allowed- Type: Variable-length string
- Max Length: Limited by available memory
- Encoding: UTF-8
- Can contain: Any characters including spaces
Examples:
-- Valid values
'Alice'
'Hello World'
'Special chars: !@#$%^&*()'
'Multi-line
text'
'' -- empty string is valid
-- Must be quoted
Alice -- ERROR: missing quotesRetrieve data from the database.
-- Point query (specific key)
SELECT * FROM table_name WHERE key = value
-- Full table scan (all rows)
SELECT * FROM table_name- table_name: Name of the table (e.g.,
users,products) - key: Integer key to search for
- value: Integer value to match
Returns all matching rows with their key-value pairs.
-- Get single row by key
SELECT * FROM users WHERE key = 1
-- Returns: 1 -> Alice
-- Get another row
SELECT * FROM users WHERE key = 42
-- Returns: 42 -> Bob
-- Get all rows (table scan)
SELECT * FROM users
-- Returns:
-- 1 -> Alice
-- 42 -> Bob
-- 100 -> Charlie
-- Query non-existent key
SELECT * FROM users WHERE key = 999
-- Returns: (empty result set)- Point query (
WHERE key = value): O(log n) - uses B+ tree index - Full scan (no WHERE): O(n) - reads all rows
- Recommendation: Use point queries when possible for better performance
-- Missing WHERE keyword
SELECT * FROM users key = 1
-- ERROR: Invalid SELECT syntax
-- Invalid key type
SELECT * FROM users WHERE key = 'abc'
-- ERROR: Cannot parse WHERE key
-- Missing FROM
SELECT * users WHERE key = 1
-- ERROR: Invalid SELECT: missing FROM clauseAdd a new row to the table.
INSERT INTO table_name VALUES (key, value)- table_name: Name of the table
- key: Integer primary key (must be unique)
- value: String value (must be quoted)
- Success: 1 row affected
- Failure: Error message (e.g., duplicate key)
-- Insert a single row
INSERT INTO users VALUES (1, 'Alice')
-- Returns: 1 row affected
-- Insert with different types of values
INSERT INTO users VALUES (2, 'Bob Smith')
INSERT INTO users VALUES (3, 'charlie@example.com')
INSERT INTO users VALUES (4, 'Value with spaces')
INSERT INTO users VALUES (5, '') -- empty string
-- Insert with negative key
INSERT INTO users VALUES (-1, 'Negative key allowed')
-- Attempt duplicate key
INSERT INTO users VALUES (1, 'Different Alice')
-- ERROR: Duplicate key: 1- Unique Keys: Each key must be unique within table
- Key Type: Must be integer
- Value Format: Must be quoted string
-- Missing VALUES keyword
INSERT INTO users (1, 'Alice')
-- ERROR: Invalid INSERT: missing VALUES
-- Missing parentheses
INSERT INTO users VALUES 1, 'Alice'
-- ERROR: malformed VALUES clause
-- Unquoted value
INSERT INTO users VALUES (1, Alice)
-- ERROR: malformed VALUES clause
-- Duplicate key
INSERT INTO users VALUES (1, 'Alice')
INSERT INTO users VALUES (1, 'Bob')
-- ERROR: Duplicate key: 1Modify an existing row.
UPDATE table_name SET value = new_value WHERE key = key_value- table_name: Name of the table
- new_value: New value to set (quoted string)
- key_value: Key of row to update (integer)
- Success: 1 row affected
- Not Found: 0 rows affected
- Failure: Error message
-- Update existing row
UPDATE users SET value = 'Alice Smith' WHERE key = 1
-- Returns: 1 row affected
-- Update to empty string
UPDATE users SET value = '' WHERE key = 2
-- Update non-existent key
UPDATE users SET value = 'Test' WHERE key = 999
-- ERROR: Key not found: 999
-- Verify update worked
SELECT * FROM users WHERE key = 1
-- Returns: 1 -> Alice SmithInternally, UPDATE is implemented as:
- Look up row by key
- If exists: Remove old value, insert new value
- If not exists: Return error
This ensures atomicity within a transaction.
-- Missing SET keyword
UPDATE users value = 'Alice' WHERE key = 1
-- ERROR: Invalid UPDATE: missing SET
-- Missing WHERE clause
UPDATE users SET value = 'Alice'
-- ERROR: Invalid UPDATE: missing WHERE
-- Invalid key type
UPDATE users SET value = 'Alice' WHERE key = 'abc'
-- ERROR: cannot parse WHERE keyRemove a row from the table.
DELETE FROM table_name WHERE key = key_value- table_name: Name of the table
- key_value: Key of row to delete (integer)
- Success: 1 row affected
- Not Found: 0 rows affected (error)
- Failure: Error message
-- Delete existing row
DELETE FROM users WHERE key = 1
-- Returns: 1 row affected
-- Verify deletion
SELECT * FROM users WHERE key = 1
-- Returns: (empty result set)
-- Delete non-existent key
DELETE FROM users WHERE key = 999
-- ERROR: Key not found: 999Warning: DELETE is permanent. There is no built-in way to recover deleted data unless you have backups or the operation was in a transaction that was rolled back.
-- Missing FROM keyword
DELETE users WHERE key = 1
-- ERROR: Invalid DELETE: missing FROM
-- Missing WHERE clause
DELETE FROM users
-- ERROR: Invalid DELETE: missing WHERE
-- (Safety feature: prevents accidental table truncation)
-- Invalid key type
DELETE FROM users WHERE key = 'abc'
-- ERROR: cannot parse WHERE key-
Single Column Data Model
- Only key-value pairs (can't have multiple columns)
- No
SELECT name, email FROM users
-
Simple WHERE Clauses
- Only
key = valuesupported - No
AND,OR,NOToperators - No comparison operators (
>,<,>=,<=,BETWEEN)
- Only
-
No Aggregations
- No
COUNT,SUM,AVG,MIN,MAX - No
GROUP BYorHAVING
- No
-
No Joins
- Can't combine data from multiple tables
- No
INNER JOIN,LEFT JOIN, etc.
-
No Sorting/Ordering
- No
ORDER BYclause - Results returned in B+ tree order (sorted by key)
- No
-
Single Row Operations
- Can't insert multiple rows at once
- No
INSERT INTO ... SELECT FROM - No bulk updates or deletes
-
Auto-Created Tables
- No
CREATE TABLEorDROP TABLE - Tables created automatically on first use
- No ALTER TABLE
- No
These limitations keep MiniDB simple and focused on core database concepts:
- Educational: Easier to understand implementation
- Performance: Simpler operations are faster
- Reliability: Less complexity = fewer bugs
See Architecture Overview for extension possibilities.
-- Start with empty database
-- 1. Insert some data
INSERT INTO users VALUES (1, 'Alice')
INSERT INTO users VALUES (2, 'Bob')
INSERT INTO users VALUES (3, 'Charlie')
INSERT INTO users VALUES (4, 'Diana')
-- 2. Query individual rows
SELECT * FROM users WHERE key = 1
-- Output: 1 -> Alice
SELECT * FROM users WHERE key = 3
-- Output: 3 -> Charlie
-- 3. Full table scan
SELECT * FROM users
-- Output:
-- 1 -> Alice
-- 2 -> Bob
-- 3 -> Charlie
-- 4 -> Diana
-- 4. Update a row
UPDATE users SET value = 'Alice Smith' WHERE key = 1
-- Verify update
SELECT * FROM users WHERE key = 1
-- Output: 1 -> Alice Smith
-- 5. Delete a row
DELETE FROM users WHERE key = 4
-- Verify deletion
SELECT * FROM users
-- Output:
-- 1 -> Alice Smith
-- 2 -> Bob
-- 3 -> Charlie
-- (Diana is gone)
-- 6. Try to update non-existent row
UPDATE users SET value = 'Test' WHERE key = 99
-- ERROR: Key not found: 99
-- 7. Insert duplicate key
INSERT INTO users VALUES (1, 'Another Alice')
-- ERROR: Duplicate key: 1User Directory:
INSERT INTO directory VALUES (1001, 'john@example.com')
INSERT INTO directory VALUES (1002, 'jane@example.com')
SELECT * FROM directory WHERE key = 1001Configuration Storage:
INSERT INTO config VALUES (1, 'max_connections=100')
INSERT INTO config VALUES (2, 'timeout=30')
UPDATE config SET value = 'max_connections=200' WHERE key = 1
SELECT * FROM configCache:
-- Store cached data
INSERT INTO cache VALUES (12345, '{"name":"Alice","age":30}')
-- Retrieve from cache
SELECT * FROM cache WHERE key = 12345
-- Invalidate cache entry
DELETE FROM cache WHERE key = 12345- Client Usage: How to connect
- Configuration: Server options
- Examples: More examples
- Performance: Tuning guide
For implementation details, see Query Processing.