Lab7
Lab7
-- Create Database
CREATE DATABASE CompanyFlightDB;
USE CompanyFlightDB;
Employee Schema
-- Employee table
CREATE TABLE Employee (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
birthdate DATE,
salary DECIMAL(10, 2),
department_id INT
);
-- Department table
CREATE TABLE Department (
department_id INT PRIMARY KEY,
name VARCHAR(50),
location VARCHAR(50)
);
-- Project table
CREATE TABLE Project (
project_id INT PRIMARY KEY,
project_name VARCHAR(50),
department_id INT,
location VARCHAR(50),
FOREIGN KEY (department_id) REFERENCES Department(department_id)
);
-- Dependent table
CREATE TABLE Dependent (
dependent_id INT PRIMARY KEY,
employee_id INT,
name VARCHAR(50),
birthdate DATE,
FOREIGN KEY (employee_id) REFERENCES Employee(employee_id)
);
Flight Schema
-- Pilot table
CREATE TABLE Pilot (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
salary DECIMAL(10, 2)
);
-- Aircraft table
CREATE TABLE Aircraft (
aircraft_id INT PRIMARY KEY,
cruising_range INT
);
-- Flight table
CREATE TABLE Flight (
flight_id INT PRIMARY KEY,
aircraft_id INT,
distance INT,
FOREIGN KEY (aircraft_id) REFERENCES Aircraft(aircraft_id)
);
Now, insert values into the tables that adhere to the constraints and triggers.
Employee Data
-- Insert employees
INSERT INTO Employee (employee_id, name, birthdate, salary, department_id)
VALUES
(1, 'John Doe', '1980-01-01', 60000, 1),
(2, 'Jane Smith', '1990-02-02', 55000, 1),
(3, 'Mark Johnson', '1975-03-03', 50000, 2);
-- Insert departments
INSERT INTO Department (department_id, name, location) VALUES
(1, 'HR', 'New York'),
(2, 'Finance', 'Los Angeles');
-- Insert projects
INSERT INTO Project (project_id, project_name, department_id, location) VALUES
(1, 'Project Alpha', 1, 'New York'),
(2, 'Project Beta', 2, 'Los Angeles');
-- Insert dependents
INSERT INTO Dependent (dependent_id, employee_id, name, birthdate) VALUES
(1, 1, 'Dependent 1', '2010-05-05'),
(2, 2, 'Dependent 2', '2015-06-06'),
(3, 3, 'Dependent 3', '2008-07-07');
Pilot Data
-- Insert pilots
INSERT INTO Pilot (employee_id, name, salary) VALUES
(4, 'Pilot A', 70000),
(5, 'Pilot B', 80000);
-- Insert certifications
INSERT INTO Certified (employee_id, aircraft_id, certification_date) VALUES
(4, 1, '2022-01-01'),
(5, 2, '2023-02-02');
-- Insert flights
INSERT INTO Flight (flight_id, aircraft_id, distance) VALUES
(1, 1, 4500),
(2, 2, 5500);
a. Assure that deleting details of an employee deletes his dependent records also.
b. When a department with exactly one project is shifted to a new location, ensure
that the project is also shifted to the new location.
c. Assure at all times that there are no departments with more than 3 projects.
e. When a project is dropped, dissociate all the employees from that particular
project.
f. When a new department is inaugurated, ensure that it is not co-located with any
other departments.
g. For every employee, ensure that his dependent's birthdate is less than his own.
i. Create a trigger that handles an update command to find the total salary of all
pilots.
j. Create a trigger to set salary as 30,000 if NULL is present. Ensure the salary of a
pilot is greater than a non-pilot.
l. When inserting a new certification for an employee, check that the aircraft ID
exists in the Aircraft.
m. When making modifications to the Aircraft table, check that the cruising range is
greater than or equal to the distance of flights.
n. When a new certification is inserted into Certified, also insert an employee with
the ID and a NULL salary.
p. Prevent the average salary of employees from dropping below Rs. 50,000.
Here’s the implementation of stored procedures using Cursors and Exception Handling for
the corresponding queries on both Employee Schema and Flight Schema.
q. Stored procedure to insert a new attribute ‘address’ in DEPENDENT and update the
same as that of the employee’s address.
-- Add address column to Dependent table if not exists
ALTER TABLE Dependent ADD address VARCHAR(100);
OPEN cur;
read_loop: LOOP
FETCH cur INTO emp_id, emp_address;
IF done THEN
LEAVE read_loop;
END IF;
r. Stored procedure to display the first name, SSN, salary, and grade of an employee
based on their salary.
DELIMITER $
CREATE PROCEDURE GetEmployeeGrade()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE emp_name VARCHAR(50);
DECLARE emp_ssn INT;
DECLARE emp_salary DECIMAL(10,2);
DECLARE emp_grade VARCHAR(10);
OPEN cur;
read_loop: LOOP
FETCH cur INTO emp_name, emp_ssn, emp_salary;
IF done THEN
LEAVE read_loop;
END IF;
OPEN cur;
read_loop: LOOP
FETCH cur INTO dept_id;
IF done THEN
LEAVE read_loop;
END IF;
t. Stored procedure to update an employee record given the employee id, with
exception handling for an invalid employee id.
DELIMITER $
CREATE PROCEDURE UpdateEmployeeRecord(IN emp_id INT, IN new_salary
DECIMAL(10,2), IN new_name VARCHAR(50))
BEGIN
DECLARE emp_exists INT;
IF emp_exists = 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid employee ID';
ELSE
-- Update the employee record
UPDATE Employee SET salary = new_salary, name = new_name WHERE
employee_id = emp_id;
-- Print success message
SELECT CONCAT('Employee ID ', emp_id, ' updated successfully') AS
message;
END IF;
END$
DELIMITER ;
u. Stored procedure to display the name and salary of each employee, and rank
them as Grade A or B based on salary.
DELIMITER $
CREATE PROCEDURE RankEmployees()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE emp_name VARCHAR(50);
DECLARE emp_salary DECIMAL(10,2);
DECLARE emp_grade CHAR(1);
OPEN cur;
read_loop: LOOP
FETCH cur INTO emp_name, emp_salary;
IF done THEN
LEAVE read_loop;
END IF;
v. Stored procedure to build a name list of employees certified for a Boeing aircraft,
with exception handling.
DELIMITER $
CREATE PROCEDURE GetBoeingCertifiedEmployees()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE emp_name VARCHAR(50);
OPEN cur;
read_loop: LOOP
FETCH cur INTO emp_name;
IF done THEN
LEAVE read_loop;
END IF;
CLOSE cur;
END$
DELIMITER ;
Certainly! Below are the SQL queries separated into individual code blocks for each question:
--a
-- Select employees with odd Ssn
SELECT *
FROM Employee
WHERE MOD(CAST(Ssn AS UNSIGNED), 2) = 1;
--b
-- Select employees with even Ssn
SELECT *
FROM Employee
WHERE MOD(CAST(Ssn AS UNSIGNED), 2) = 0;
--c
-- Extract the year from BirthDate
SELECT SUBSTRING(BirthDate, 1, 4) AS Year
FROM Employee;
--d
-- Extract the first 3 characters of FName
SELECT LEFT(FName, 3)
FROM Employee;
--e
-- Find duplicate FNames
SELECT Fname, COUNT(*)
FROM Employee
GROUP BY Fname
HAVING COUNT(*) > 1;
--f
-- Remove duplicate entries based on FName, keeping the one with the minimum
Ssn
CREATE TEMPORARY TABLE Temp AS
SELECT MIN(Ssn) as Ssn FROM Employee GROUP BY FName;
DELETE FROM Employee WHERE Ssn NOT IN (SELECT Ssn FROM Temp);
--g
-- Remove duplicate entries based on FName, keeping the one with the minimum
Ssn
CREATE TEMPORARY TABLE Temp AS
SELECT MIN(Ssn) as Ssn FROM Employee GROUP BY FName;
DELETE FROM Employee WHERE Ssn NOT IN (SELECT Ssn FROM Temp);
--h
-- Find the 3rd highest unique salary
SELECT DISTINCT salary
FROM Employee
ORDER BY salary DESC
LIMIT 1 OFFSET 2;
-- For nth max salary:
SELECT DISTINCT salary
FROM Employee
ORDER BY salary DESC
LIMIT 1 OFFSET n-1; -- Replace `n` with the desired position
--i
-- Find the top 3 unique salaries
SELECT DISTINCT salary
FROM Employee
ORDER BY salary DESC
LIMIT 3;
--j
-- Extract the year, month, and day from BirthDate
SELECT YEAR(BirthDate) AS Year, MONTH(BirthDate) AS Month, DAY(BirthDate) AS
Day
FROM Employee;
--k
-- Extract date part from BirthDate
SELECT DATE(BirthDate)
FROM Employee;
--l
-- Find the position of 'a' in 'Sundar Pitchai'
SELECT LOCATE('a', 'Sundar Pitchai');
--m
-- Remove leading spaces from FName
SELECT LTRIM(FName)
FROM Employee;
--n
-- Get the length of FName
SELECT LENGTH(FName)
FROM Employee;
--o
-- Replace 'o' with '*' in FName
SELECT REPLACE(FName, 'o', '*')
FROM Employee;
--p
-- Concatenate FName and LName with an underscore
SELECT CONCAT(FName, '_', LName)
FROM Employee;
--q
-- Find employees whose FName contains 'jai' (case-insensitive)
SELECT *
FROM Employee
WHERE LOWER(FName) LIKE '%jai%';
--r
-- Count employees by gender with birth dates between 1980-05-01 and 2024-12-31
SELECT Gender, COUNT(*)
FROM Employee
WHERE BirthDate BETWEEN '1980-05-01' AND '2024-12-31'
GROUP BY Gender;
--s
-- Retrieve user and authentication_string from the mysql.user table
SELECT user, authentication_string
FROM mysql.user;
Each code block is now clearly separated for easy reference and execution.