SQL Operations
Data Definition Language (DDL)
Section titled “Data Definition Language (DDL)”CREATE TABLE
Section titled “CREATE TABLE”Create tables with custom schemas. Column types are inferred from data.
-- Basic table creationCREATE TABLE users (id, name, email, age);
-- Table with more columnsCREATE TABLE products (id, name, price, category, description, created_at);
-- Table for ordersCREATE TABLE orders (id, user_id, product_id, quantity, total, order_date);
Notes:
- Column names are case-sensitive
- No explicit data types required (HaruDB infers types from values)
- Tables are stored as JSON files (
.harudb
format)
DROP TABLE
Section titled “DROP TABLE”Remove tables and all associated data permanently.
-- Drop a tableDROP TABLE users;
-- Drop multiple tablesDROP TABLE products;DROP TABLE orders;
Warning: This operation is irreversible and will delete all data in the table.
Data Manipulation Language (DML)
Section titled “Data Manipulation Language (DML)”INSERT
Section titled “INSERT”Add new rows to tables.
-- Insert single rowINSERT INTO users VALUES (1, 'Alice', 'alice@example.com', '25');
-- Insert multiple rowsINSERT INTO users VALUES (2, 'Bob', 'bob@example.com', '30');INSERT INTO users VALUES (3, 'Charlie', 'charlie@example.com', '35');
-- Insert with different data typesINSERT INTO products VALUES (1, 'Laptop', '999.99', 'Electronics', 'High-performance laptop', '2024-01-15');INSERT INTO products VALUES (2, 'Mouse', '29.99', 'Electronics', 'Wireless mouse', '2024-01-15');
Notes:
- Values must match the number of columns
- String values should be quoted
- Numeric values can be quoted or unquoted
SELECT
Section titled “SELECT”Query and display table data with powerful filtering capabilities.
Basic SELECT Operations
Section titled “Basic SELECT Operations”-- Select all columnsSELECT * FROM users;
-- Select specific columnsSELECT name, email FROM users;
-- Select with aliasesSELECT name AS user_name, email AS user_email FROM users;
-- Count recordsSELECT COUNT(*) FROM users;
-- Count with conditionSELECT COUNT(*) FROM users WHERE age > 25;
Advanced WHERE Clauses
Section titled “Advanced WHERE Clauses”HaruDB supports comprehensive WHERE clause operations:
Comparison Operators
Section titled “Comparison Operators”-- EqualitySELECT * FROM users WHERE age = '25';SELECT * FROM products WHERE category = 'Electronics';
-- InequalitySELECT * FROM users WHERE age != '25';SELECT * FROM products WHERE price <> '999.99';
-- Greater thanSELECT * FROM users WHERE age > '25';SELECT * FROM products WHERE price > '100';
-- Less thanSELECT * FROM users WHERE age < '30';SELECT * FROM products WHERE price < '500';
-- Greater than or equalSELECT * FROM users WHERE age >= '25';SELECT * FROM products WHERE price >= '100';
-- Less than or equalSELECT * FROM users WHERE age <= '30';SELECT * FROM products WHERE price <= '500';
Pattern Matching with LIKE
Section titled “Pattern Matching with LIKE”-- Starts with patternSELECT * FROM users WHERE name LIKE 'A%';SELECT * FROM products WHERE name LIKE 'Laptop%';
-- Ends with patternSELECT * FROM users WHERE email LIKE '%@example.com';SELECT * FROM products WHERE category LIKE '%Electronics';
-- Contains patternSELECT * FROM users WHERE name LIKE '%Alice%';SELECT * FROM products WHERE description LIKE '%wireless%';
-- Single character wildcardSELECT * FROM users WHERE name LIKE 'A_ice';SELECT * FROM products WHERE name LIKE 'L_ptop';
Logical Operators
Section titled “Logical Operators”-- AND operatorSELECT * FROM users WHERE age > '25' AND name LIKE 'A%';SELECT * FROM products WHERE category = 'Electronics' AND price < '100';
-- OR operatorSELECT * FROM users WHERE age < '25' OR age > '35';SELECT * FROM products WHERE category = 'Electronics' OR category = 'Furniture';
-- NOT operatorSELECT * FROM users WHERE NOT (age = '25');SELECT * FROM products WHERE NOT (category = 'Electronics');
-- Complex combinations with parenthesesSELECT * FROM users WHERE (age > '25' AND name LIKE 'A%') OR (age < '20' AND name LIKE 'B%');SELECT * FROM products WHERE category = 'Electronics' AND (price > '100' OR name LIKE '%Pro%');
NULL Handling
Section titled “NULL Handling”-- Check for NULL valuesSELECT * FROM users WHERE email IS NULL;SELECT * FROM products WHERE description IS NULL;
-- Check for non-NULL valuesSELECT * FROM users WHERE email IS NOT NULL;SELECT * FROM products WHERE description IS NOT NULL;
UPDATE
Section titled “UPDATE”Modify existing rows by index.
-- Update single columnUPDATE users SET age = '26' ROW 0;
-- Update multiple columnsUPDATE users SET name = 'Alice Updated', email = 'alice.updated@example.com' ROW 0;
-- Update with WHERE-like conditions (using row index)UPDATE products SET price = '1099.99' ROW 0;UPDATE products SET category = 'Premium Electronics' ROW 0;
Notes:
- Row indices start from 0
- Use
ROW <index>
to specify which row to update - Multiple columns can be updated in a single statement
DELETE
Section titled “DELETE”Remove rows by index.
-- Delete single rowDELETE FROM users ROW 0;
-- Delete multiple rows (one at a time)DELETE FROM users ROW 1;DELETE FROM users ROW 2;
-- Delete all rows (by dropping and recreating table)DROP TABLE users;CREATE TABLE users (id, name, email, age);
Notes:
- Row indices start from 0
- Use
ROW <index>
to specify which row to delete - Deleted rows cannot be recovered
Indexes and Query Optimization
Section titled “Indexes and Query Optimization”CREATE INDEX
Section titled “CREATE INDEX”Build in-memory hash indexes for faster lookups.
-- Create index on single columnCREATE INDEX ON users (email);CREATE INDEX ON products (category);
-- Create multiple indexesCREATE INDEX ON users (name);CREATE INDEX ON products (price);CREATE INDEX ON orders (user_id);
Indexed Queries
Section titled “Indexed Queries”Queries with equality conditions automatically use indexes when available:
-- These queries will use indexes if createdSELECT * FROM users WHERE email = 'alice@example.com';SELECT * FROM products WHERE category = 'Electronics';SELECT * FROM orders WHERE user_id = '1';
-- These queries will not use indexes (no index on age)SELECT * FROM users WHERE age > '25';SELECT * FROM products WHERE price < '100';
Complete Examples
Section titled “Complete Examples”E-commerce Database Example
Section titled “E-commerce Database Example”-- Create tablesCREATE TABLE customers (id, name, email, phone);CREATE TABLE products (id, name, price, category, stock);CREATE TABLE orders (id, customer_id, product_id, quantity, total, order_date);
-- Insert sample dataINSERT INTO customers VALUES (1, 'Alice Johnson', 'alice@example.com', '555-0101');INSERT INTO customers VALUES (2, 'Bob Smith', 'bob@example.com', '555-0102');INSERT INTO customers VALUES (3, 'Charlie Brown', 'charlie@example.com', '555-0103');
INSERT INTO products VALUES (1, 'Laptop Pro', '1299.99', 'Electronics', '10');INSERT INTO products VALUES (2, 'Wireless Mouse', '29.99', 'Electronics', '50');INSERT INTO products VALUES (3, 'Office Chair', '199.99', 'Furniture', '15');
INSERT INTO orders VALUES (1, 1, 1, 1, '1299.99', '2024-01-15');INSERT INTO orders VALUES (2, 1, 2, 2, '59.98', '2024-01-15');INSERT INTO orders VALUES (3, 2, 3, 1, '199.99', '2024-01-16');
-- Create indexes for performanceCREATE INDEX ON customers (email);CREATE INDEX ON products (category);CREATE INDEX ON orders (customer_id);
-- Complex queries-- Find all electronics products under $100SELECT * FROM products WHERE category = 'Electronics' AND price < '100';
-- Find customers with email addresses from example.comSELECT * FROM customers WHERE email LIKE '%@example.com';
-- Find orders from a specific customerSELECT * FROM orders WHERE customer_id = '1';
-- Find products with low stockSELECT * FROM products WHERE stock < '20';
User Management Example
Section titled “User Management Example”-- Create user tableCREATE TABLE users (id, username, email, role, created_at, last_login);
-- Insert usersINSERT INTO users VALUES (1, 'admin', 'admin@company.com', 'admin', '2024-01-01', '2024-01-15');INSERT INTO users VALUES (2, 'john', 'john@company.com', 'user', '2024-01-02', '2024-01-14');INSERT INTO users VALUES (3, 'jane', 'jane@company.com', 'user', '2024-01-03', '2024-01-13');INSERT INTO users VALUES (4, 'bob', 'bob@company.com', 'readonly', '2024-01-04', '2024-01-12');
-- Create indexesCREATE INDEX ON users (email);CREATE INDEX ON users (role);
-- Query examples-- Find all admin usersSELECT * FROM users WHERE role = 'admin';
-- Find users who haven't logged in recentlySELECT * FROM users WHERE last_login < '2024-01-14';
-- Find users with specific email domainSELECT * FROM users WHERE email LIKE '%@company.com';
-- Count users by roleSELECT role, COUNT(*) FROM users GROUP BY role;
Best Practices
Section titled “Best Practices”Performance Optimization
Section titled “Performance Optimization”-
Create indexes on frequently queried columns:
CREATE INDEX ON users (email);CREATE INDEX ON products (category); -
Use specific column names in SELECT:
-- GoodSELECT name, email FROM users WHERE age > 25;-- Less efficientSELECT * FROM users WHERE age > 25; -
Use appropriate WHERE conditions:
-- Use indexed columns when possibleSELECT * FROM users WHERE email = 'alice@example.com';
Data Integrity
Section titled “Data Integrity”-
Always use transactions for multiple operations:
BEGIN TRANSACTION;INSERT INTO orders VALUES (1, 1, 1, 1, '1299.99', '2024-01-15');UPDATE products SET stock = '9' ROW 0;COMMIT; -
Validate data before insertion:
-- Check if user exists before creating orderSELECT * FROM users WHERE id = '1';
Error Handling
Section titled “Error Handling”HaruDB provides clear error messages for common issues:
- Table not found:
Table users not found
- Invalid row index:
Row index out of range
- Syntax errors:
Invalid SQL syntax
- Constraint violations:
Duplicate key value
For more advanced features, see the Transactions Guide and Indexes Guide.