-- Create ERP database schema CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; -- Create tables for ERP system CREATE TABLE IF NOT EXISTS employees ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), first_name VARCHAR(100) NOT NULL, last_name VARCHAR(100) NOT NULL, email VARCHAR(255) UNIQUE NOT NULL, phone VARCHAR(50), department VARCHAR(100), position VARCHAR(100), salary DECIMAL(10,2), hire_date DATE DEFAULT CURRENT_DATE, is_active BOOLEAN DEFAULT true, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS departments ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), name VARCHAR(100) NOT NULL UNIQUE, description TEXT, manager_id UUID REFERENCES employees(id), budget DECIMAL(12,2), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS projects ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), name VARCHAR(255) NOT NULL, description TEXT, start_date DATE, end_date DATE, budget DECIMAL(12,2), status VARCHAR(50) DEFAULT 'planning', manager_id UUID REFERENCES employees(id), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS project_assignments ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), project_id UUID REFERENCES projects(id) ON DELETE CASCADE, employee_id UUID REFERENCES employees(id) ON DELETE CASCADE, role VARCHAR(100), assigned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UNIQUE(project_id, employee_id) ); -- Insert sample data INSERT INTO departments (name, description, budget) VALUES ('Engineering', 'Software development and IT infrastructure', 500000.00), ('Sales', 'Sales and customer relations', 300000.00), ('Human Resources', 'Employee management and recruitment', 150000.00), ('Finance', 'Financial planning and accounting', 200000.00); INSERT INTO employees (first_name, last_name, email, phone, department, position, salary) VALUES ('John', 'Doe', 'john.doe@company.com', '+1-555-0101', 'Engineering', 'Senior Developer', 95000.00), ('Jane', 'Smith', 'jane.smith@company.com', '+1-555-0102', 'Sales', 'Sales Manager', 85000.00), ('Michael', 'Johnson', 'michael.johnson@company.com', '+1-555-0103', 'HR', 'HR Manager', 75000.00), ('Sarah', 'Williams', 'sarah.williams@company.com', '+1-555-0104', 'Finance', 'CFO', 120000.00); INSERT INTO projects (name, description, start_date, end_date, budget, status) VALUES ('ERP System Upgrade', 'Upgrade the existing ERP system to latest version', '2024-01-01', '2024-06-30', 150000.00, 'in_progress'), ('Website Redesign', 'Complete redesign of company website', '2024-02-01', '2024-04-30', 50000.00, 'planning'), ('Mobile App Development', 'Develop mobile application for customers', '2024-03-01', '2024-12-31', 200000.00, 'planning'); -- Create indexes for better performance CREATE INDEX idx_employees_name ON employees(last_name, first_name); CREATE INDEX idx_employees_department ON employees(department); CREATE INDEX idx_projects_status ON projects(status); CREATE INDEX idx_projects_dates ON projects(start_date, end_date); -- Create views for common queries CREATE VIEW employee_summary AS SELECT e.id, e.first_name, e.last_name, e.email, e.department, e.position, e.salary, d.name as department_name FROM employees e LEFT JOIN departments d ON e.department = d.name; CREATE VIEW project_summary AS SELECT p.id, p.name, p.status, p.budget, p.start_date, p.end_date, e.first_name || ' ' || e.last_name as manager_name FROM projects p LEFT JOIN employees e ON p.manager_id = e.id;