π A Complete Guide to SYNAPCORES SQLv2 Data Types and Creating Tables
Hey everyone! I've been working with SYNAPCORES SQLv2 for a while now, and I wanted to share a comprehensive guide on data types and table creation. This database is pretty unique because it combines traditional SQL with AI-native capabilities, so buckle up!
π Standard Data Types
SYNAPCORES supports all the SQL data types you'd expect, plus some exciting AI-specific ones. Let's break them down:
Numeric Types
- BOOLEAN - True/false values
- SMALLINT - 16-bit signed integer (-32,768 to 32,767)
- INTEGER - 32-bit signed integer
- BIGINT - 64-bit signed integer (for those really big numbers)
- REAL - 32-bit floating point
- DOUBLE - 64-bit floating point (more precision)
- DECIMAL(p, s) - Fixed-point decimal (p = precision, s = scale)
String Types
- TEXT - Variable-length text (unlimited)
- VARCHAR(n) - Variable character with max length n
- CHAR(n) - Fixed-length character (always n characters)
Binary & Structured Data
- BYTEA - Binary data
- JSON - JSON documents (stored as text)
- JSONB - Binary JSON (faster queries, optimized storage)
- UUID - Universal Unique Identifiers
Date & Time Types
- TIMESTAMP - Date and time together
- DATE - Date only (no time component)
- TIME - Time only (no date component)
π€ AI-Native Data Types (This is where it gets cool!)
Here's what makes SYNAPCORES really special:
- VECTOR(n) - Dense vectors for embeddings (n = dimensions)
- AUDIO - Audio file storage
- VIDEO - Video file storage
- IMAGE - Image file storage
- PDF - PDF document storage
These types are perfect for ML workloads, RAG systems, and multimedia applications!
π οΈ Creating Tables: The Basics
Here's the basic syntax:
CREATE TABLE [IF NOT EXISTS] table_name (
column_name data_type [column_constraint],
...
[table_constraint]
);
Simple Example: A Users Table
CREATE TABLE users (
id INTEGER PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_active BOOLEAN DEFAULT true
);
π Column Constraints
You can add constraints to individual columns:
- PRIMARY KEY - Unique identifier for the row
- UNIQUE - No duplicate values allowed
- NOT NULL - Value must be provided
- CHECK (expression) - Custom validation rule
- DEFAULT expression - Default value if none provided
- REFERENCES foreign_table (foreign_column) - Foreign key
Example with Multiple Constraints
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
price DECIMAL(10, 2) NOT NULL CHECK (price > 0),
stock_quantity INTEGER DEFAULT 0 CHECK (stock_quantity >= 0),
category VARCHAR(50) NOT NULL,
created_date DATE DEFAULT CURRENT_DATE
);
π Table-Level Constraints
For more complex constraints, use table-level constraints:
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0),
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
total_amount DECIMAL(12, 2) NOT NULL,
-- Table constraints
FOREIGN KEY (customer_id) REFERENCES users (id),
FOREIGN KEY (product_id) REFERENCES products (product_id),
CHECK (total_amount >= 0)
);
π― Real-World Example: E-Commerce Schema
Here's a complete example with multiple related tables:
-- Customers table
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
phone VARCHAR(20),
registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Products table with JSON metadata
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(200) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL CHECK (price >= 0),
inventory_count INTEGER DEFAULT 0 CHECK (inventory_count >= 0),
category VARCHAR(50) NOT NULL,
metadata JSONB,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Orders table
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status VARCHAR(20) DEFAULT 'pending',
total_amount DECIMAL(12, 2) NOT NULL CHECK (total_amount >= 0),
FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
CHECK (status IN ('pending', 'processing', 'shipped', 'delivered', 'cancelled'))
);
-- Order items table
CREATE TABLE order_items (
item_id INTEGER PRIMARY KEY,
order_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0),
unit_price DECIMAL(10, 2) NOT NULL,
subtotal DECIMAL(12, 2) NOT NULL,
FOREIGN KEY (order_id) REFERENCES orders (order_id),
FOREIGN KEY (product_id) REFERENCES products (product_id)
);
π AI-Powered Tables Example
Now for the fun part - using those AI-native types!
-- Product catalog with images and embeddings
CREATE TABLE product_catalog (
product_id INTEGER PRIMARY KEY,
product_name VARCHAR(200) NOT NULL,
description TEXT,
product_image IMAGE,
description_embedding VECTOR(768),
price DECIMAL(10, 2) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Document repository with PDFs
CREATE TABLE documents (
doc_id INTEGER PRIMARY KEY,
title VARCHAR(255) NOT NULL,
document_file PDF,
document_embedding VECTOR(1536),
upload_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
category VARCHAR(50)
);
-- Media library
CREATE TABLE media_library (
media_id INTEGER PRIMARY KEY,
title VARCHAR(255) NOT NULL,
media_type VARCHAR(20) NOT NULL,
audio_file AUDIO,
video_file VIDEO,
thumbnail IMAGE,
duration_seconds INTEGER,
uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CHECK (media_type IN ('audio', 'video', 'mixed'))
);
π‘ Pro Tips
- Use IF NOT EXISTS to make your scripts idempotent:
- Always define PRIMARY KEYS for better query performance
- Use CHECK constraints to enforce business rules at the database level
- JSONB over JSON when you need to query JSON fields frequently
- Foreign keys maintain referential integrity automatically
- DEFAULT values reduce application logic and ensure consistency
π Complex Composite Primary Key Example
CREATE TABLE user_permissions (
user_id INTEGER NOT NULL,
resource_id INTEGER NOT NULL,
permission_type VARCHAR(50) NOT NULL,
granted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
granted_by INTEGER,
PRIMARY KEY (user_id, resource_id, permission_type),
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (granted_by) REFERENCES users (id),
CHECK (permission_type IN ('read', 'write', 'delete', 'admin'))
);
π Table with UUID as Primary Key
CREATE TABLE sessions (
session_id UUID PRIMARY KEY,
user_id INTEGER NOT NULL,
ip_address VARCHAR(45),
user_agent TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL,
is_active BOOLEAN DEFAULT true,
FOREIGN KEY (user_id) REFERENCES users (id)
);
π Best Practices Checklist
β
Always define primary keys
β
Use NOT NULL for required fields
β
Add CHECK constraints for data validation
β
Use appropriate data types (don't use TEXT for everything!)
β
Name your constraints explicitly when possible
β
Consider foreign keys for referential integrity
β
Use UNIQUE constraints to prevent duplicates
β
Add DEFAULT values for optional fields
β
Use JSONB for flexible schema parts
β
Use VECTOR types for ML/AI features
π€ Common Pitfalls to Avoid
β Using TEXT when VARCHAR(n) would be better
β Forgetting NOT NULL on important fields
β Not using foreign keys (leads to orphaned records)
β Over-normalizing (sometimes JSON is okay!)
β Not planning for future schema changes
β Using wrong numeric types (INTEGER vs BIGINT)
β Forgetting to add indexes on foreign keys
π¬ Full Example: Blog Platform Schema
Here's everything together in a realistic scenario:
-- Users table
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
full_name VARCHAR(100),
bio TEXT,
profile_image IMAGE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_login TIMESTAMP,
is_verified BOOLEAN DEFAULT false,
is_active BOOLEAN DEFAULT true,
CHECK (email LIKE '%@%.%')
);
-- Blog posts table
CREATE TABLE IF NOT EXISTS posts (
post_id INTEGER PRIMARY KEY,
author_id INTEGER NOT NULL,
title VARCHAR(255) NOT NULL,
slug VARCHAR(255) NOT NULL UNIQUE,
content TEXT NOT NULL,
excerpt VARCHAR(500),
featured_image IMAGE,
content_embedding VECTOR(768),
status VARCHAR(20) DEFAULT 'draft',
published_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
view_count INTEGER DEFAULT 0,
metadata JSONB,
FOREIGN KEY (author_id) REFERENCES users (user_id),
CHECK (status IN ('draft', 'published', 'archived')),
CHECK (view_count >= 0)
);
-- Comments table
CREATE TABLE IF NOT EXISTS comments (
comment_id INTEGER PRIMARY KEY,
post_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
parent_comment_id INTEGER,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP,
is_deleted BOOLEAN DEFAULT false,
FOREIGN KEY (post_id) REFERENCES posts (post_id),
FOREIGN KEY (user_id) REFERENCES users (user_id),
FOREIGN KEY (parent_comment_id) REFERENCES comments (comment_id)
);
-- Tags table
CREATE TABLE IF NOT EXISTS tags (
tag_id INTEGER PRIMARY KEY,
tag_name VARCHAR(50) NOT NULL UNIQUE,
slug VARCHAR(50) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Post tags junction table
CREATE TABLE IF NOT EXISTS post_tags (
post_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
PRIMARY KEY (post_id, tag_id),
FOREIGN KEY (post_id) REFERENCES posts (post_id),
FOREIGN KEY (tag_id) REFERENCES tags (tag_id)
);
-- Likes table
CREATE TABLE IF NOT EXISTS likes (
user_id INTEGER NOT NULL,
post_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, post_id),
FOREIGN KEY (user_id) REFERENCES users (user_id),
FOREIGN KEY (post_id) REFERENCES posts (post_id)
);
π Conclusion
SYNAPCORES SQLv2 gives you the power of traditional SQL plus modern AI capabilities. The key is understanding your data types and using constraints effectively. Start simple, then add AI features when you need them!
Questions? Drop them in the comments! Happy coding! π
Note: Remember to check the official SYNAPCORES documentation for the latest features and updates!
https://synapcores.com/sqlv2