The document outlines the SQL schema for an e-commerce database, including tables for users, categories, brands, products, orders, order items, and shopping carts. Each table has defined columns, data types, and constraints, along with indexes for optimized queries. The schema supports user management, product cataloging, order processing, and shopping cart functionality.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9 views2 pages
DB
The document outlines the SQL schema for an e-commerce database, including tables for users, categories, brands, products, orders, order items, and shopping carts. Each table has defined columns, data types, and constraints, along with indexes for optimized queries. The schema supports user management, product cataloging, order processing, and shopping cart functionality.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, first_name VARCHAR(50), last_name VARCHAR(50), phone_number VARCHAR(20), address VARCHAR(255), role VARCHAR(20) DEFAULT 'customer', created_at TIMESTAMP DEFAULT NOW(), updated_at TIMESTAMP, is_active BOOLEAN DEFAULT TRUE );
CREATE INDEX idx_users_email ON users (email);
CREATE INDEX idx_users_username ON users (username);
CREATE TABLE categories (
category_id SERIAL PRIMARY KEY, name VARCHAR(100) UNIQUE NOT NULL, description TEXT );
CREATE INDEX idx_categories_name ON categories (name);
CREATE TABLE brands (
brand_id SERIAL PRIMARY KEY, name VARCHAR(100) UNIQUE NOT NULL, logo_url VARCHAR(255) );
CREATE INDEX idx_brands_name ON brands (name);
CREATE TABLE products (
product_id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, description TEXT, price DECIMAL(10, 2) NOT NULL, image_url VARCHAR(255), stock_quantity INTEGER DEFAULT 0, category_id INTEGER REFERENCES categories(category_id), brand_id INTEGER REFERENCES brands(brand_id), created_at TIMESTAMP DEFAULT NOW(), updated_at TIMESTAMP, is_active BOOLEAN DEFAULT TRUE );
CREATE INDEX idx_products_name ON products (name);
CREATE INDEX idx_products_category_id ON products (category_id);
CREATE INDEX idx_products_brand_id ON products (brand_id); CREATE INDEX idx_products_name_description ON products USING gin (to_tsvector('english', name || ' ' || description));
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY, user_email VARCHAR(100), order_date TIMESTAMP DEFAULT NOW(), shipping_address VARCHAR(255) NOT NULL, total_amount DECIMAL(10, 2) NOT NULL, status VARCHAR(20) DEFAULT 'pending', payment_method VARCHAR(50) );
CREATE INDEX idx_orders_user_email ON orders (user_email);
CREATE INDEX idx_orders_order_date ON orders (order_date);
CREATE TABLE order_items (
order_item_id SERIAL PRIMARY KEY, order_id INTEGER REFERENCES orders(order_id), product_id INTEGER REFERENCES products(product_id), quantity INTEGER NOT NULL, price DECIMAL(10, 2) NOT NULL );
CREATE INDEX idx_order_items_order_id ON order_items (order_id);
CREATE INDEX idx_order_items_product_id ON order_items (product_id);