|
| 1 | +-- Drop the database if it exists |
| 2 | +DROP DATABASE IF EXISTS employeedb; |
| 3 | + |
| 4 | +-- Create the database |
| 5 | +CREATE DATABASE employeedb; |
| 6 | + |
| 7 | +-- Switch to the created database |
| 8 | +USE employeedb; |
| 9 | + |
| 10 | + |
| 11 | +CREATE TABLE departments ( |
| 12 | + dep_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, |
| 13 | + department_name VARCHAR(100) NOT NULL, |
| 14 | + manager_id INT, -- Manager will be added as a foreign key later |
| 15 | + location VARCHAR(100) |
| 16 | +); |
| 17 | + |
| 18 | +CREATE TABLE employees ( |
| 19 | + emp_id INT PRIMARY KEY AUTO_INCREMENT, |
| 20 | + first_name VARCHAR(100) NOT NULL, |
| 21 | + last_name VARCHAR(100) NOT NULL, |
| 22 | + date_of_birth DATE NOT NULL, |
| 23 | + gender VARCHAR(10), |
| 24 | + joining_date DATE NOT NULL, |
| 25 | + department_id INT, -- Links to departments table |
| 26 | + salary DECIMAL(10 , 2 ) NOT NULL, |
| 27 | + job_title VARCHAR(100), |
| 28 | + location VARCHAR(100), |
| 29 | + CONSTRAINT fk_department_id FOREIGN KEY (department_id) |
| 30 | + REFERENCES departments (dep_id) |
| 31 | +); |
| 32 | + |
| 33 | +-- Once both departments and employees tables are created, add the foreign key for manager_id: |
| 34 | +ALTER TABLE departments |
| 35 | +ADD CONSTRAINT fk_manager_id FOREIGN KEY (manager_id) |
| 36 | +REFERENCES employees (emp_id); |
| 37 | + |
| 38 | +CREATE TABLE salaries ( |
| 39 | + salary_id INT PRIMARY KEY AUTO_INCREMENT, |
| 40 | + employee_id INT, -- Links to employees table |
| 41 | + salary DECIMAL(10 , 2 ) NOT NULL, |
| 42 | + effective_date DATE NOT NULL, |
| 43 | + CONSTRAINT fk_employee_id FOREIGN KEY (employee_id) |
| 44 | + REFERENCES employees (emp_id) |
| 45 | +); |
| 46 | + |
| 47 | + |
| 48 | +CREATE TABLE employee_performance ( |
| 49 | + performance_id INT PRIMARY KEY AUTO_INCREMENT, |
| 50 | + employee_id INT NOT NULL, -- Links to employees table |
| 51 | + performance_score INT, |
| 52 | + review_date DATE NOT NULL, |
| 53 | + manager_id INT NOT NULL, -- Links to employees table |
| 54 | + CONSTRAINT score_between_1_and_5 CHECK (performance_score BETWEEN 1 AND 5), |
| 55 | + CONSTRAINT fk_emp_performance_manager_id FOREIGN KEY (manager_id) |
| 56 | + REFERENCES employees (emp_id), |
| 57 | + CONSTRAINT fk_emp_performance_employee_id FOREIGN KEY (employee_id) |
| 58 | + REFERENCES employees (emp_id) |
| 59 | +); |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +CREATE TABLE employee_leave ( |
| 64 | + leave_id INT PRIMARY KEY AUTO_INCREMENT, |
| 65 | + employee_id INT NOT NULL, -- Links to employees table |
| 66 | + leave_type VARCHAR(50) NOT NULL, |
| 67 | + start_date DATE NOT NULL, |
| 68 | + end_date DATE NOT NULL, |
| 69 | + CONSTRAINT fk_emp_leave_employee_id FOREIGN KEY (employee_id) |
| 70 | + REFERENCES employees (emp_id) |
| 71 | +); |
| 72 | + |
| 73 | + |
| 74 | +INSERT INTO Departments (dep_id, department_name, manager_id, location) |
| 75 | +VALUES |
| 76 | +(1, 'Human Resources', NULL, 'New York'), |
| 77 | +(2, 'Engineering', NULL, 'San Francisco'), |
| 78 | +(3, 'Sales', NULL, 'Chicago'), |
| 79 | +(4, 'Marketing', NULL, 'Boston'), |
| 80 | +(5, 'Finance', NULL, 'Los Angeles'); |
| 81 | + |
| 82 | +INSERT INTO Employees (emp_id, first_name, last_name, date_of_birth, gender, joining_date, department_id, salary, job_title, location) |
| 83 | +VALUES |
| 84 | +(1, 'Alice', 'Smith', '1985-02-14', 'Female', '2010-06-15', 2, 95000.00, 'Senior Engineer', 'San Francisco'), |
| 85 | +(2, 'Bob', 'Johnson', '1990-08-22', 'Male', '2015-09-01', 2, 85000.00, 'Software Engineer', 'San Francisco'), |
| 86 | +(3, 'Charlie', 'Brown', '1980-12-12', 'Male', '2005-01-20', 3, 75000.00, 'Sales Manager', 'Chicago'), |
| 87 | +(4, 'Diana', 'Williams', '1992-03-28', 'Female', '2018-07-19', 1, 65000.00, 'HR Specialist', 'New York'), |
| 88 | +(5, 'Evan', 'Taylor', '1987-11-10', 'Male', '2012-10-05', 4, 72000.00, 'Marketing Analyst', 'Boston'), |
| 89 | +(6, 'Grace', 'Lee', '1995-06-05', 'Female', '2021-01-12', 5, 78000.00, 'Financial Analyst', 'Los Angeles'), |
| 90 | +(7, 'John', 'Doe', '1989-03-22', 'Male', '2017-05-23', 4, 70000.00, 'Marketing Manager', 'Boston'); |
| 91 | + |
| 92 | + |
| 93 | +-- Setting the manager_id for each department |
| 94 | +UPDATE Departments |
| 95 | +SET manager_id = 1 |
| 96 | +WHERE dep_id = 2; -- Engineering department manager is Alice Smith |
| 97 | + |
| 98 | +UPDATE Departments |
| 99 | +SET manager_id = 3 |
| 100 | +WHERE dep_id = 3; -- Sales department manager is Charlie Brown |
| 101 | + |
| 102 | +UPDATE Departments |
| 103 | +SET manager_id = 4 |
| 104 | +WHERE dep_id = 1; -- HR department manager is Diana Williams |
| 105 | + |
| 106 | +UPDATE Departments |
| 107 | +SET manager_id = 5 |
| 108 | +WHERE dep_id = 4; -- Marketing department manager is Evan Taylor |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +INSERT INTO Salaries (salary_id, employee_id, salary, effective_date) |
| 113 | +VALUES |
| 114 | +(1, 1, 95000.00, '2020-01-01'), |
| 115 | +(2, 1, 90000.00, '2018-01-01'), |
| 116 | +(3, 2, 85000.00, '2021-06-01'), |
| 117 | +(4, 3, 75000.00, '2019-09-01'), |
| 118 | +(5, 4, 65000.00, '2022-03-01'), |
| 119 | +(6, 5, 72000.00, '2020-10-01'), |
| 120 | +(7, 6, 78000.00, '2021-01-12'); |
| 121 | + |
| 122 | + |
| 123 | +INSERT INTO Employee_Performance (performance_id, employee_id, performance_score, review_date, manager_id) |
| 124 | +VALUES |
| 125 | +(1, 1, 5, '2023-06-01', 1), |
| 126 | +(2, 2, 4, '2023-06-01', 1), |
| 127 | +(3, 3, 3, '2022-12-15', 3), |
| 128 | +(4, 4, 4, '2023-03-20', 4), |
| 129 | +(5, 5, 5, '2023-05-10', 5), |
| 130 | +(6, 6, 4, '2023-04-15', 5), |
| 131 | +(7, 7, 3, '2023-07-01', 4); |
| 132 | + |
| 133 | + |
| 134 | +INSERT INTO Employee_Leave (leave_id, employee_id, leave_type, start_date, end_date) |
| 135 | +VALUES |
| 136 | +(1, 1, 'Vacation', '2023-08-01', '2023-08-15'), |
| 137 | +(2, 2, 'Sick Leave', '2023-09-10', '2023-09-12'), |
| 138 | +(3, 3, 'Vacation', '2023-07-05', '2023-07-15'), |
| 139 | +(4, 4, 'Maternity Leave', '2023-01-10', '2023-03-10'), |
| 140 | +(5, 5, 'Sick Leave', '2023-06-20', '2023-06-22'), |
| 141 | +(6, 6, 'Vacation', '2023-05-01', '2023-05-10'), |
| 142 | +(7, 7, 'Personal Leave', '2023-09-01', '2023-09-05'); |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
0 commit comments