This guide shows the correct SQL syntax for MiniDB commands.
List all tables in the database.
SHOW TABLESExample:
minidb> SHOW TABLES
['students', 'courses', 'users']
Show table structure (columns, types, constraints).
DESCRIBE table_name
DESC table_name -- Shorthand aliasExample:
minidb> DESC students
{'columns': ['id', 'name', 'course_id'], 'primary_key': 'id', 'column_types': {'id': 'int', 'name': 'str', 'course_id': 'int'}, 'unique_columns': []}
minidb> DESCRIBE courses
{'columns': ['id', 'title'], 'primary_key': 'id', 'column_types': {'id': 'int', 'title': 'str'}, 'unique_columns': []}
Create a new table with columns and optional types/constraints.
CREATE TABLE table_name (col1 [type] [UNIQUE], col2 [type], ...)Types: int, str
Constraints: UNIQUE
Examples:
-- Basic table
CREATE TABLE users (id, name, email)
-- With types
CREATE TABLE students (id int, name str, course_id int)
-- With unique constraint
CREATE TABLE accounts (id int, email str UNIQUE, username str UNIQUE)Insert a new row into a table.
INSERT INTO table_name VALUES (value1, value2, ...)Examples:
-- String values need quotes
INSERT INTO students VALUES (101, 'Collins', 1)
-- Numbers don't need quotes
INSERT INTO courses VALUES (1, 'Computer Science')
-- Mixed types
INSERT INTO users VALUES (5, 'John Doe', 'john@example.com')Retrieve data from a table.
SELECT column1, column2 FROM table_nameSELECT * FROM table_name WHERE column operator valueOperators: =, !=, >, <, >=, <=, IN
SELECT * FROM table_name [WHERE condition] LIMIT numberExamples:
-- All students
SELECT * FROM students
-- Specific student by ID
SELECT * FROM students WHERE id = 101
-- Students with course_id greater than 1
SELECT * FROM students WHERE course_id > 1
-- Students not in course 2
SELECT * FROM students WHERE course_id != 2
-- String comparison
SELECT * FROM students WHERE name = 'Collins'WHERE keyword!
❌ WRONG:
SELECT id=102 FROM students -- Missing WHERE keyword
SELECT * FROM students id=101 -- Missing WHERE keyword✅ CORRECT:
SELECT * FROM students WHERE id = 102
SELECT name, course_id FROM students WHERE id = 101
SELECT * FROM students LIMIT 5Filter results based on the output of another query using the IN operator.
SELECT * FROM table1 WHERE column IN (SELECT column FROM table2 WHERE condition)Example:
-- Find students enrolled in 'Computer Science'
SELECT * FROM students
WHERE id IN (SELECT student_id FROM enrollments WHERE course = 'CS')Combine data from two tables based on a related column.
SELECT * FROM table1 JOIN table2 ON table1.column = table2.columnExample:
-- Get student names with their course titles
SELECT * FROM students JOIN courses ON students.course_id = courses.idResult:
id | name | course_id | title
----|---------|-----------|------------------
101 | Collins | 1 | Computer Science
102 | John | 2 | Electrical Eng
Modify existing rows in a table.
UPDATE table_name SET column = value WHERE condition_column operator condition_valueExamples:
-- Update student name
UPDATE students SET name = 'Collins Jr.' WHERE id = 101
-- Update course title
UPDATE courses SET title = 'Advanced CS' WHERE id = 1
-- Update with numeric value
UPDATE students SET course_id = 2 WHERE id = 101Remove rows from a table.
DELETE FROM table_name WHERE column operator valueExamples:
-- Delete specific student
DELETE FROM students WHERE id = 101
-- Delete all students in course 1
DELETE FROM students WHERE course_id = 1
-- Delete by name
DELETE FROM students WHERE name = 'John'Modify table schema by adding new columns.
ALTER TABLE table_name ADD column_name data_typeSupported Types: INT, STR
Behavior:
- Adds column to table schema
- Updates all existing rows with default values:
INT→0STR→''(empty string)
- Changes persist across database restarts
Examples:
-- Add email column
ALTER TABLE users ADD email STR
-- Add age column
ALTER TABLE users ADD age INT
-- Add multiple columns (run separately)
ALTER TABLE products ADD price INT
ALTER TABLE products ADD description STRAfter ALTER TABLE:
-- Existing rows have default values
SELECT * FROM users
-- Output: [{'id': 1, 'name': 'Alice', 'email': ''}]
-- Update new column
UPDATE users SET email = 'alice@example.com' WHERE id = 1
-- Insert with new columns
INSERT INTO users VALUES (2, 'Bob', 'bob@example.com')Remove a column from an existing table:
ALTER TABLE table_name DROP COLUMN column_nameExample:
-- Drop email column
ALTER TABLE users DROP COLUMN email
-- Verify
DESCRIBE usersSafety:
- ❌ Cannot drop primary key column
- ✅ Removes column from all rows
- ✅ Removes associated constraints (UNIQUE, FOREIGN KEY)
- ✅ Atomic operation with automatic save
Rename an existing column:
ALTER TABLE table_name RENAME COLUMN old_name TO new_nameExample:
-- Rename column
ALTER TABLE users RENAME COLUMN name TO full_name
-- Can rename primary key
ALTER TABLE users RENAME COLUMN id TO user_id
-- Verify
DESCRIBE usersFeatures:
- ✅ Updates all rows automatically
- ✅ Preserves data
- ✅ Updates constraints (UNIQUE, FOREIGN KEY, PRIMARY KEY)
- ✅ Rebuilds indexes
- ✅ Atomic operation
Group multiple operations together with atomicity guarantees.
Start a new transaction:
BEGIN TRANSACTION
-- or simply:
BEGINSave all changes to disk:
COMMITDiscard all changes:
ROLLBACKBehavior:
- Changes are staged in memory until COMMIT
- ROLLBACK discards all staged changes
- SELECT sees staged changes during transaction
- Auto-commit mode when no transaction active
Example: Money Transfer
-- Start transaction
BEGIN TRANSACTION
-- Transfer $100 from Alice to Bob
UPDATE accounts SET balance = 900 WHERE id = 1
UPDATE accounts SET balance = 600 WHERE id = 2
-- Log the transaction
INSERT INTO transactions VALUES (1, 1, 2, 100)
-- Commit all changes atomically
COMMITExample: Rollback on Error
-- Start transaction
BEGIN
-- Make changes
INSERT INTO accounts VALUES (5, 'Eve', 1500)
UPDATE accounts SET balance = -100 WHERE id = 1 -- Invalid!
-- Detect error, rollback
ROLLBACK
-- Changes discarded, database unchangedUse Cases:
- Multi-table updates
- Batch operations
- Error recovery
- Data consistency
❌ SELECT id=102 FROM students
✅ SELECT * FROM students WHERE id = 102
❌ decribe users (typo)
✅ DESCRIBE users or DESC users
❌ DESCRIBE Users (if table is 'users')
✅ DESCRIBE users
❌ INSERT INTO students VALUES (101, Collins, 1)
✅ INSERT INTO students VALUES (101, 'Collins', 1)
❌ fronm instead of from
✅ SELECT * FROM students
- Commands are case-insensitive:
SELECT,select, andSeLeCtall work - Table names are case-sensitive:
students≠Students - Use DESC for quick table inspection:
DESC table_name - String values need quotes: Use single quotes
'value'or double quotes"value" - Numbers don't need quotes:
101,3.14, etc. - WHERE is required for conditions: Always use
WHERE column = value
minidb> SHOW TABLES
['students', 'courses']
minidb> DESC students
{'columns': ['id', 'name', 'course_id'], 'primary_key': 'id', ...}
minidb> SELECT * FROM students
id | name | course_id
----|---------|----------
101 | Collins | 1
102 | John | 2
minidb> SELECT * FROM students WHERE id = 101
id | name | course_id
----|---------|----------
101 | Collins | 1
minidb> SELECT * FROM students JOIN courses ON students.course_id = courses.id
id | name | course_id | title
----|---------|-----------|------------------
101 | Collins | 1 | Computer Science
102 | John | 2 | Electrical Eng
minidb> UPDATE students SET name = 'Collins Smith' WHERE id = 101
Updated 1 row(s) in 'students'.
minidb> DELETE FROM students WHERE id = 102
Deleted 1 row(s) from 'students'.- Type
SHOW TABLESto see available tables - Type
DESC table_nameto see table structure - Type
exitorquitto leave the REPL
For more information, see the README.md file.