0% found this document useful (0 votes)
19 views

Project File

1. The document discusses various SQL queries on relational database schemas for sailors, boats, customers, products, invoices, employees, departments, and jobs tables. It includes queries to find sailors by reservations, customers by purchases, and employees by department. 2. Triggers are created to update customer balances on invoice inserts, check employee ages on inserts, and copy customer IDs to a new table. Views are created to show employees in a location. 3. Tables are altered by adding new columns and indexes are created to improve query performance.

Uploaded by

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

Project File

1. The document discusses various SQL queries on relational database schemas for sailors, boats, customers, products, invoices, employees, departments, and jobs tables. It includes queries to find sailors by reservations, customers by purchases, and employees by department. 2. Triggers are created to update customer balances on invoice inserts, check employee ages on inserts, and copy customer IDs to a new table. Views are created to show employees in a location. 3. Tables are altered by adding new columns and indexes are created to improve query performance.

Uploaded by

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

Data Base Management

System

PROJECT

FILE

NAME: SHREYA VERMA


ROLL NO.: 2022UCS1683
CLASS: CSE SECTION 3
Q1) Consider the following relational schema
SAILORS(sid, sname, rating, date_of_birth)
BOATS(bid, bname, color)
RESERVES(sid, bid, date, time_slot)

-- Create SAILORS table


CREATE TABLE SAILORS (
sid INT PRIMARY KEY,
sname TEXT NOT NULL,
rating INT,
date_of_birth DATE NOT NULL
);

-- Create BOATS table


CREATE TABLE BOATS (
bid INT PRIMARY KEY,
bname TEXT NOT NULL,
color TEXT NOT NULL
);

-- Create RESERVES table


CREATE TABLE RESERVES (
sid INT,
bid INT,
date DATE,
time_slot TIME,
PRIMARY KEY (sid, bid, date, time_slot),
FOREIGN KEY (sid) REFERENCES SAILORS(sid),
FOREIGN KEY (bid) REFERENCES BOATS(bid)
);

-- Insert values into SAILORS table


INSERT INTO SAILORS (sid, sname, rating, date_of_birth)
VALUES
(101, 'John Doe', 8, '1990-05-15'),
(102, 'Jane Smith', 7, '1988-11-23'),
(103, 'Bob Johnson', 6, '1995-03-07');

-- Insert values into BOATS table


INSERT INTO BOATS (bid, bname, color)
VALUES
(201, 'Speedboat 1', 'Blue'),
(202, 'Sailboat 1', 'White'),
(203, 'Fishing Boat 1', 'Green');

-- Insert values into RESERVES table


INSERT INTO RESERVES (sid, bid, date, time_slot)
VALUES
(101, 201, '2023-01-10', '10:00:00'),
(102, 202, '2023-02-15', '14:30:00'),
(103, 203, '2023-03-20', '12:00:00');
1)Find Sailors who have reserved atleast one boat
SELECT DISTINCT S.sid, S.sname FROM SAILORS S
JOIN RESERVES R ON S.sid = R.sid;

2)Find names of sailors who have reserverd a red or green boat in the month of march
SELECT DISTINCT S.sname
FROM SAILORS S
JOIN RESERVES R ON S.sid = R.sid
JOIN BOATS B ON R.bid = B.bid
WHERE B.color IN ('Red', 'Green') AND MONTH(R.date) = 3;

3)Find the Name of Sailors who have reserved a red and a green boat
SELECT DISTINCT S.sname
FROM SAILORS S
JOIN RESERVES R ON S.sid = R.sid
JOIN BOATS B ON R.bid = B.bid
WHERE B.color = 'Red' AND S.sid IN (
SELECT S2.sid
FROM SAILORS S2
JOIN RESERVES R2 ON S2.sid = R2.sid
JOIN BOATS B2 ON R2.bid = B2.bid
WHERE B2.color = 'Green'
);

4)Find sid of sailors who have not reseverd a boat after jan 2018
SELECT DISTINCT S.sid
FROM SAILORS S
WHERE NOT EXISTS (
SELECT 1
FROM RESERVES R
WHERE S.sid = R.sid AND R.date > '2018-01-31'
);
5)Find Sailors whose rating is greater than of all the sailors named “john”
SELECT *
FROM SAILORS
WHERE rating > (
SELECT MAX(rating)
FROM SAILORS
WHERE sname = 'John'
);

6)Find Sailors who have reserved all boats


SELECT DISTINCT S.sid, S.sname
FROM SAILORS S
WHERE NOT EXISTS (
SELECT B.bid
FROM BOATS B
WHERE NOT EXISTS (
SELECT 1
FROM RESERVES R
WHERE R.sid = S.sid AND R.bid = B.bid
)
);

7)Find name and age of oldest Sailor


SELECT sname, DATEDIFF(CURRENT_DATE,
date_of_birth) AS age
FROM SAILORS
WHERE date_of_birth = (
SELECT MAX(date_of_birth)
FROM SAILORS
);

8)Find the age of the youngest Sailor


SELECT DATEDIFF(CURRENT_DATE,
MIN(date_of_birth)) AS age_of_youngest_sailor
FROM SAILORS;

Q2) Consider consider the following relational schemas


CUSTOMERS(cust_num, cust_lname, cust_fname, cust_balance)
PRODUCT(prod_num, prod_name, price)
INVOICE(inv_num, prod_num, cust_num, inv_date, unit_sold, inv_amount)
-- Create CUSTOMERS table
CREATE TABLE CUSTOMERS (
cust_num INT PRIMARY KEY,
cust_lname VARCHAR(255),
cust_fname VARCHAR(255),
cust_balance DECIMAL(10, 2) DEFAULT 0
);

-- Create PRODUCT table


CREATE TABLE PRODUCT (
prod_num INT PRIMARY KEY,
prod_name VARCHAR(255),
price DECIMAL(10, 2)
);

-- Create INVOICE table


CREATE TABLE INVOICE (
inv_num INT PRIMARY KEY,
prod_num INT,
cust_num INT,
inv_date DATE,
unit_sold INT,
inv_amount DECIMAL(10, 2),
FOREIGN KEY (prod_num) REFERENCES
PRODUCT(prod_num),
FOREIGN KEY (cust_num) REFERENCES
CUSTOMERS(cust_num)
);
-- Insert into CUSTOMERS table
INSERT INTO CUSTOMERS (cust_num, cust_lname,
cust_fname, cust_balance)
VALUES
(1, 'Smith', 'John', 50000),
(2, 'Doe', 'Jane', 75000),
(3, 'Johnson', 'Bob', 100000);

-- Insert into PRODUCT table


INSERT INTO PRODUCT (prod_num, prod_name, price)
VALUES
(101, 'ProductA', 20.00),
(102, 'ProductB', 30.00),
(103, 'ProductC', 25.00);

-- Insert into INVOICE table


INSERT INTO INVOICE (inv_num, prod_num, cust_num,
inv_date, unit_sold, inv_amount)
VALUES
(1, 101, 1, '2023-01-15', 2, 40.00),
(2, 102, 2, '2023-01-15', 5, 150.00),
(3, 103, 3, '2023-01-16', 3, 75.00);

1) find the names of the customer who have purchased no item. set default value of
cust_balance as 0 for such customer
SELECT cust_fname, cust_lname
FROM CUSTOMERS
WHERE cust_num NOT IN (SELECT cust_num FROM
INVOICE);

2) Write the trigger to update the cust_balance in customer table when a new
invoice record is entered for the customer
DELIMITER //
CREATE TRIGGER update_cust_balance
AFTER INSERT ON INVOICE
FOR EACH ROW
BEGIN
UPDATE CUSTOMERS
SET cust_balance = cust_balance + NEW.inv_amount
WHERE cust_num = NEW.cust_num;
END;
//
DELIMITER ;

3) Find the customers who have purchased more than 3 units of a product on a day
SELECT DISTINCT C.cust_fname, C.cust_lname
FROM CUSTOMERS C
JOIN INVOICE I ON C.cust_num = I.cust_num
WHERE I.unit_sold > 3;

4) write a query to illustrate left outer ,right outer and full outer join
-- Left outer join
SELECT *
FROM CUSTOMERS
LEFT JOIN INVOICE ON CUSTOMERS.cust_num =
INVOICE.cust_num;

-- Right outer join


SELECT *
FROM CUSTOMERS
RIGHT JOIN INVOICE ON CUSTOMERS.cust_num =
INVOICE.cust_num;

-- Full outer join (may not be supported in all database


systems)
SELECT *
FROM CUSTOMERS
FULL OUTER JOIN INVOICE ON
CUSTOMERS.cust_num = INVOICE.cust_num;

5) count number of products sold on each date


SELECT inv_date, COUNT(DISTINCT prod_num) AS
num_products_sold
FROM INVOICE
GROUP BY inv_date;

6) as soon as customer balance becomes greater than RS 1,00,000 copy the


customer number in the table called “gold customer”
DELIMITER //
CREATE TRIGGER gold_customer_trigger
AFTER UPDATE ON CUSTOMERS
FOR EACH ROW
BEGIN
IF NEW.cust_balance > 100000 AND
OLD.cust_balance <= 100000 THEN
INSERT INTO gold_customer (cust_num) VALUES
(NEW.cust_num);
END IF;
END;
//
DELIMITER ;

7) Add a new attribute cust_D its skinob in customer table


ALTER TABLE CUSTOMERS
ADD COLUMN cust_D VARCHAR(255);
Q3) Consider the following Relational schema
DEPARTMENT(department_id, name, Location_id)
Job(Job_id, Function)
EMPLOYEE(Employee_id, name, DOB, Job_id, Manager_id, Hire_date, Salary,
department_id)
-- Create DEPARTMENT table
CREATE TABLE DEPARTMENT (
department_id INT PRIMARY KEY,
name VARCHAR(255),
Location_id INT
);

-- Create Job table


CREATE TABLE Job (
Job_id INT PRIMARY KEY,
Function VARCHAR(255)
);

-- Create EMPLOYEE table


CREATE TABLE EMPLOYEE (
Employee_id INT PRIMARY KEY,
name VARCHAR(255),
DOB DATE,
Job_id INT,
Manager_id INT,
Hire_date DATE,
Salary DECIMAL(10, 2),
department_id INT,
FOREIGN KEY (Job_id) REFERENCES Job(Job_id),
FOREIGN KEY (Manager_id) REFERENCES
EMPLOYEE(Employee_id),
FOREIGN KEY (department_id) REFERENCES
DEPARTMENT(department_id)
);
-- Insert into DEPARTMENT table
INSERT INTO DEPARTMENT (department_id, name,
Location_id)
VALUES
(1, 'HR', 101),
(2, 'IT', 102),
(3, 'Finance', 103);

-- Insert into Job table


INSERT INTO Job (Job_id, Function)
VALUES
(1, 'Manager'),
(2, 'Developer'),
(3, 'Analyst');

-- Insert into EMPLOYEE table


INSERT INTO EMPLOYEE (Employee_id, name, DOB,
Job_id, Manager_id, Hire_date, Salary, department_id)
VALUES
(101, 'John Doe', '1990-05-15', 1, NULL, '2015-03-01',
60000.00, 1),
(102, 'Jane Smith', '1988-11-23', 2, 101, '2016-01-10',
70000.00, 2),
(103, 'Bob Johnson', '1995-03-07', 3, 101, '2017-05-20',
50000.00, 3);

1) write a query to count number of employees who joined in March 2015


SELECT COUNT(*) AS
num_employees_joined_march2015
FROM EMPLOYEE
WHERE YEAR(Hire_date) = 2015 AND
MONTH(Hire_date) = 3;

2) display the nth highest salary drawing employee details


SELECT *
FROM EMPLOYEE
ORDER BY Salary DESC
LIMIT 1 OFFSET 1;

3) find the budget (total salary) of each department


SELECT department_id, SUM(Salary) AS
department_budget
FROM EMPLOYEE
GROUP BY department_id;
4) find the department with maximum budget
SELECT department_id, SUM(Salary) AS
department_budget
FROM EMPLOYEE
GROUP BY department_id
ORDER BY department_budget DESC
LIMIT 1;

5) create a view to show number of employees working in Delhi and update it


automatically when the database is modified
CREATE VIEW EmployeesInDelhi AS
SELECT e.Employee_id, e.name, e.DOB, e.Job_id,
e.Manager_id, e.Hire_date, e.Salary, e.department_id,
d.Location_id
FROM EMPLOYEE e
JOIN DEPARTMENT d ON e.department_id =
d.department_id
WHERE d.Location_id = 101;

6) Write a trigger to ensure that no employee of age less than 25 can be inserted in
the database
DELIMITER //
CREATE TRIGGER check_employee_age
BEFORE INSERT ON EMPLOYEE
FOR EACH ROW
BEGIN
IF DATEDIFF(CURDATE(), NEW.DOB) < (25 * 365)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Employee must be at least
25 years old';
END IF;
END;
//
DELIMITER ;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy