0% found this document useful (0 votes)
25 views4 pages

Qn2 Database

Uploaded by

collins JUNIOR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

Qn2 Database

Uploaded by

collins JUNIOR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Open MySQL Workbench

1. Open MySQL Workbench and connect to your database server.


2. Create a new schema (database) by executing:

CREATE DATABASE EmployeeManagement;


USE EmployeeManagement;

2. Create Tables for 3NF

a. employees Table

CREATE TABLE departments (


departmentId INT PRIMARY KEY AUTO_INCREMENT,
departmentName VARCHAR(100) NOT NULL,
managerName VARCHAR(100) NOT NULL,
managerContact VARCHAR(15) NOT NULL
);

Once this table is successfully created, you can proceed to create the employees table that
references it. Here's the updated SQL for the employees table to match this structure:

sql
Copy code
CREATE TABLE employees (
employeeId INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
address VARCHAR(255) NOT NULL,
departmentId INT NOT NULL,
FOREIGN KEY (departmentId) REFERENCES departments(departmentId)
);

c. projects Table

CREATE TABLE projects (


projectId INT PRIMARY KEY AUTO_INCREMENT,
projectCode VARCHAR(10) UNIQUE NOT NULL,
projectName VARCHAR(100) NOT NULL
);

d. employeeProjects Table

sql
Copy code
CREATE TABLE employeeProjects (
employeeId INT NOT NULL,
projectId INT NOT NULL,
hoursWorked INT NOT NULL,
PRIMARY KEY (employeeId, projectId),
FOREIGN KEY (employeeId) REFERENCES employees(employeeId),
FOREIGN KEY (projectId) REFERENCES projects(projectId)
);

3. Insert Data

a. Insert Data into departments

USE employeemanagement;
INSERT INTO departments (departmentName, managerName, managerContact)
VALUES
('HR', 'John White', '123-456-7890'),
('IT', 'Mark Brown', '987-654-3210');

b. Insert Data into employees

USE employeemanagement;
INSERT INTO employees (name, address, departmentId)
VALUES
('Alice Smith', 'NY, USA', 1),
('Bob Johnson', 'LA, USA', 2);

c. Insert Data into projects

USE employeemanagement;
INSERT INTO projects (projectCode, projectName)
VALUES
('P101', 'Recruitment'),
('P102', 'Training'),
('P103', 'System Upgrade');

d. Insert Data into employeeProjects

sql
Copy code
INSERT INTO employeeProjects (employeeId, projectId, hoursWorked)
VALUES
(1, 1, 10),
(1, 2, 15),
(2, 3, 20);

4. Verify the Tables

Run the following queries to check the data in each table:

a. View Employees

sql
Copy code
SELECT * FROM employees;

b. View Departments

sql
Copy code
SELECT * FROM departments;

c. View Projects

sql
Copy code
SELECT * FROM projects;

d. View Employee Projects

sql
Copy code
SELECT * FROM employeeProjects;

5. Join Tables for Analysis

To view employee details along with their projects and hours worked, run:

use employeemanagement;
SELECT
e.name AS EmployeeName,
d.departmentName AS Department,
p.projectName AS Project,
ep.hoursWorked AS HoursWorked
FROM
employeeProjects ep
JOIN
employees e ON ep.employeeId = e.employeeId
JOIN
projects p ON ep.projectId = p.projectId
JOIN
departments d ON e.departmentId = d.departmentId;

Final Result:

Here’s the consolidated output from the query:

EmployeeName Department Project HoursWorked


Alice Smith HR Recruitment 10
Alice Smith HR Training 15
Bob Johnson IT System Upgrade 20

You might also like