Database Design for a Company Management System
Objective
The purpose of this assignment is to design and implement a relational database for managing a
company's employees, departments, projects, and employee project assignments using MySQL.
By the end of the assignment, you should be able to:
Design tables and relationships between them.
Write SQL queries to create, manipulate, and retrieve data.
Understand and apply primary and foreign keys.
Work with many-to-many relationships using a junction table (optional)
Task 1: Database Design
Design a database named CompanyDB that stores data for employees, departments, projects,
and employee assignments. The following structure outlines the requirements for each table:
Table 1: Employees
This table stores information about company employees. Each employee has the following
attributes:
employee_id: Unique identifier (Primary Key, Auto Increment).
first_name: Employee's first name.
last_name: Employee's last name.
email: Employee's email address.
department_id: The department the employee belongs to (Foreign Key to Departments
table).
hire_date: The date the employee was hired.
Table 2: Departments
This table stores information about the various departments in the company:
department_id: Unique identifier for each department (Primary Key, Auto Increment).
department_name: Name of the department.
location: Location of the department.
Table 3: Projects
This table contains information about company projects:
project_id: Unique identifier for each project (Primary Key, Auto Increment).
project_name: The name of the project.
start_date: The project’s start date.
end_date: The project’s expected or actual end date.
Table 4: Assignments
This table represents the relationship between employees and projects (many-to-many). Each
employee can be assigned to multiple projects, and each project can have multiple employees.
assignment_id: Unique identifier for each assignment (Primary Key, Auto Increment).
employee_id: The employee assigned to the project (Foreign Key to Employees table).
project_id: The project the employee is assigned to (Foreign Key to Projects table).
assigned_date: The date the employee was assigned to the project.
role: The employee's role on the project.
Task 2: Database Implementation
Step 1: Create the Database
Execute the following SQL command to create the database:
CREATE DATABASE CompanyDB;
USE CompanyDB;
Step 2: Create the Tables
Write and execute SQL commands to create the four tables described above. Use appropriate
data types for each field. Include Primary Keys and Foreign Keys where necessary.
-- Create Departments table
CREATE TABLE Departments (
department_id INT AUTO_INCREMENT PRIMARY KEY,
department_name VARCHAR(100) NOT NULL,
location VARCHAR(100)
);
-- Create Employees table
CREATE TABLE Employees (
employee_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100),
department_id INT,
hire_date DATE,
FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);
-- Create Projects table
CREATE TABLE Projects (
project_id INT AUTO_INCREMENT PRIMARY KEY,
project_name VARCHAR(100) NOT NULL,
start_date DATE,
end_date DATE
);
-- Create Assignments table
CREATE TABLE Assignments (
assignment_id INT AUTO_INCREMENT PRIMARY KEY,
employee_id INT,
project_id INT,
assigned_date DATE,
role VARCHAR(50),
FOREIGN KEY (employee_id) REFERENCES Employees(employee_id),
FOREIGN KEY (project_id) REFERENCES Projects(project_id)
);
Task 3: Data Insertion
Insert data into the Departments, Employees, Projects, and Assignments tables. Use INSERT
INTO SQL statements to populate these tables with sample data.
-- Insert sample data into Departments
INSERT INTO Departments (department_name, location)
VALUES ('IT', 'New York'), ('HR', 'Los Angeles');
-- Insert sample data into Employees
INSERT INTO Employees (first_name, last_name, email, department_id, hire_date)
VALUES ('John', 'Doe', '
[email protected]', 1, '2020-06-15'),
('Jane', 'Smith', '
[email protected]', 2, '2019-08-10');
-- Insert sample data into Projects
INSERT INTO Projects (project_name, start_date, end_date)
VALUES ('Project A', '2021-01-01', '2021-12-31'),
('Project B', '2022-01-01', NULL);
-- Insert sample data into Assignments
INSERT INTO Assignments (employee_id, project_id, assigned_date, role)
VALUES (1, 1, '2021-01-01', 'Developer'),
(2, 2, '2022-02-01', 'Manager');
Task 4: SQL Queries
Write SQL queries to retrieve and manipulate data from the database.
1. Retrieve all employees with their department names:
SELECT e.first_name, e.last_name, d.department_name
FROM Employees e
JOIN Departments d ON e.department_id = d.department_id;
2. Find all projects an employee is assigned to:
SELECT p.project_name, a.role
FROM Assignments a
JOIN Projects p ON a.project_id = p.project_id
WHERE a.employee_id = 1; -- Replace with the employee_id to search for
3. List all employees working on a specific project:
SELECT e.first_name, e.last_name, a.role
FROM Assignments a
JOIN Employees e ON a.employee_id = e.employee_id
WHERE a.project_id = 1; -- Replace with the project_id to search for
Task 5: Database Normalization
Ensure the database is normalized to at least the Third Normal Form (3NF):
1NF: Each table has atomic (indivisible) values, with no repeating groups.
2NF: All non-key attributes are fully dependent on the primary key.
3NF: No transitive dependencies (non-key attributes should not depend on other non-key
attributes).