CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
phone VARCHAR(20),
address VARCHAR(255),
referral_id INT,
FOREIGN KEY (referral_id) REFERENCES employees(id)
);
CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(100),
description TEXT,
price DECIMAL(10,2)
);
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
phone VARCHAR(20),
address VARCHAR(255),
referred_by INT,
FOREIGN KEY (referred_by) REFERENCES employees(id)
);
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT,
product_id INT,
employee_id INT,
quantity INT,
total_price DECIMAL(10,2),
date DATE,
FOREIGN KEY (customer_id) REFERENCES customers(id),
FOREIGN KEY (product_id) REFERENCES products(id),
FOREIGN KEY (employee_id) REFERENCES employees(id)
);
CREATE TABLE ledger (
id INT PRIMARY KEY,
order_id INT,
employee_id INT,
commission DECIMAL(10,2),
date DATE,
FOREIGN KEY (order_id) REFERENCES orders(id),
FOREIGN KEY (employee_id) REFERENCES employees(id)
);
This schema allows for tracking employees, customers, products, orders, and commissions. The referral and multi-level commission tracking is handled through the referral_id column in employees and referred_by column in customers, which reference back to the employees table. The ledger table stores each commission transaction per order per employee. Additional attributes can be added as needed to support more complex commission rules.