DBMS Programs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

1

1.A.Consider the following schema WHERE b1.bcolor = 'red' AND b2.bcolor =


Sailors (Sid, sname, rating, age) 'green';
Reserves (Sid, bid, day)
Boat (bid, bname, bcolor) Find the sid of sailors who reserved red
boat but not green boat
Find all sailors with rating above 7 SELECT DISTINCT r1.Sid
SELECT * FROM Sailors WHERE rating > FROM Reserves r1
7; JOIN Boat b1 ON r1.bid = b1.bid
WHERE b1.bcolor = 'red'
AND r1.Sid NOT IN (
Find the color of boats reserved by SELECT DISTINCT r2.Sid
lubber FROM Reserves r2
SELECT DISTINCT b.bcolor JOIN Boat b2 ON r2.bid = b2.bid
FROM Boat b WHERE b2.bcolor = 'green'
JOIN Reserves r ON b.bid = r.bid );
JOIN Sailors s ON r.Sid = s.Sid
WHERE s.sname = 'lubber';
Find the sid of sailors who reserved red
Find the average age of all the sailors or green boat
SELECT AVG(age) AS average_age SELECT DISTINCT r.Sid
FROM Sailors; FROM Reserves r
JOIN Boat b ON r.bid = b.bid
Count the sailors with different name WHERE b.bcolor = 'red' OR b.bcolor =
SELECT COUNT(DISTINCT sname) AS 'green';
different_names FROM Sailors;
Find the names of sailors who reserved
Find the age of sailor who’s name end’s at least one boat
with B and at least 3 characters SELECT DISTINCT s.sname
SELECT age FROM Sailors WHERE FROM Sailors s
sname LIKE '%B' AND LENGTH(sname) JOIN Reserves r ON s.Sid = r.Sid;
>= 3;
Find the names of sailors who reserved
boat no 103 (using nested IN)
B.Consider above schema and Write the SELECT s.sname
Queries for the following operations. FROM Sailors s
Find the sid of sailors who reserved red WHERE s.Sid IN (
and green boat SELECT r.Sid
SELECT DISTINCT r1.Sid FROM Reserves r
FROM Reserves r1 WHERE r.bid = 103
JOIN Boat b1 ON r1.bid = b1.bid );
JOIN Reserves r2 ON r1.Sid = r2.Sid
JOIN Boat b2 ON r2.bid = b2.bid
2
2. A.Consider the following schema
Employee (eid,ename,age,salary,address)

Add the column date of joins(DOJ)


ALTER TABLE Employee ADD COLUMN
DOJ DATE;

Change the name of the employee shiva


into shivaji
UPDATE Employee SET ename = 'shivaji'
WHERE ename = 'shiva';

Remove the column age from a relation


ALTER TABLE Employee DROP COLUMN
age;

Find the employee name starts with


letter ‘M’ and ends with ‘A’
SELECT ename FROM Employee WHERE
ename LIKE 'M%A';

Display the employee relation salaries


high to low
SELECT * FROM Employee ORDER BY
salary DESC;

B.Consider above schema and create a


trigger that stores the details of old
employees
into new schema
old_emp(sno,eid,ename,doj,DOT,salary)
CREATE TRIGGER
before_employee_update
BEFORE UPDATE ON Employee
FOR EACH ROW
BEGIN
INSERT INTO old_emp (eid, ename,
DOJ, DOT, salary)
VALUES (OLD.eid, OLD.ename,
OLD.DOJ, CURRENT_DATE, OLD.salary);
END;
3
3. Consider the following schema
A.Student (Sid, sname, Replace the above multi table view
age,address,branch) without total marks field
Marks (Sid, sub1,sub2,sub3,total) CREATE OR REPLACE VIEW
Find the average marks of sub3 Student_Marks_View AS
SELECT AVG(sub3) AS average_sub3 SELECT Student.Sid, Student.sname,
FROM Marks; Student.age, Student.address,
Student.branch, Marks.sub1, Marks.sub2,
Find the younger student name Marks.sub3
SELECT sname FROM Student WHERE FROM Student
age = (SELECT MIN(age) FROM Student); JOIN Marks ON Student.Sid = Marks.Sid;

Find the names of students who’s name Update the data record in view table
ends with letter ‘A’ UPDATE Student_Marks_View SET sub1 =
SELECT sname FROM Student WHERE 95 WHERE Sid = 1;
sname LIKE '%A';
Drop all the views which you are created
Find the sid of the class toper DROP VIEW IF EXISTS AIML_Students;
SELECT Sid FROM Marks ORDER BY DROP VIEW IF EXISTS
total DESC LIMIT 1; Student_Marks_View;

Add the check constraint for the column


age
ALTER TABLE Student ADD
CONSTRAINT chk_age CHECK (age >=
18);

B.Consider above schema and Write the


Queries for the following operations.
Create a view for student table who’s
branch is AIML(single table view)
CREATE VIEW AIML_Students AS
SELECT * FROM Student WHERE branch
= 'AIML';

Create a multi table view which display


all the fields of student and marks
CREATE VIEW Student_Marks_View AS
SELECT Student.*, Marks.*
FROM Student
JOIN Marks ON Student.Sid = Marks.Sid;
4
CREATE TRIGGER no_delete BEFORE
4. A.Consider the following schema DELETE ON Library
Books FOR EACH ROW
(sno,ISBN,Author,Title,Volume,Price) BEGIN
Sort the books based on price higher to SIGNAL SQLSTATE '45000' SET
lower MESSAGE_TEXT = 'Deletes are not
SELECT * FROM Books ORDER BY Price allowed';
DESC; END;

Add the column Publisher


ALTER TABLE Books ADD COLUMN
Publisher VARCHAR(255); 6.A. Consider the following schema
Student (Sid, sname, age,branch,address
Update the prices of books less than ,fee)
300 are set to 350 Count the no of students from various
UPDATE Books SET Price = 350 WHERE branches(Group by)
Price < 300; SELECT branch, COUNT(*) AS
student_count
Find the total amount of all the books FROM Student
present in relation GROUP BY branch;
SELECT SUM(Price) AS total_amount
FROM Books; Find the total fee amount collected from
various branches
Rename the table books into Library SELECT branch, SUM(fee) AS total_fee
ALTER TABLE Books RENAME TO FROM Student
Library; GROUP BY branch;

Find the count of students from various


B.Consider above schema and create a location having count > 2
triggers that are not allowed to insert SELECT address, COUNT(*) AS
the records and delete the records from student_count
the relation FROM Student
CREATE TRIGGER no_insert BEFORE GROUP BY address
INSERT ON Library HAVING student_count > 2;
FOR EACH ROW
BEGIN Change the data type of column sid
SIGNAL SQLSTATE '45000' SET integer to varchar
MESSAGE_TEXT = 'Inserts are not ALTER TABLE Student MODIFY COLUMN
allowed'; Sid VARCHAR(255);
END;
5
Find the name of the student from aiml
branch and fee is 95000 Create a procedure with OUT
SELECT sname FROM Student WHERE DELIMITER //
branch = 'AIML' AND fee = 95000; CREATE PROCEDURE
GetTotalFeeByBranch(OUT totalFee
DECIMAL(10, 2))
B.Consider above schema and Write the BEGIN
Queries for the following operations. SELECT SUM(fee) INTO totalFee FROM
Create a procedure for executing all the Student;
queries of above END
DELIMITER // //DELIMITER ;
CREATE PROCEDURE ExecuteQueries()
BEGIN Create a procedure with INOUT
SELECT branch, COUNT(*) AS DELIMITER //
student_count CREATE PROCEDURE
FROM Student UpdateStudentFee(INOUT studentId
GROUP BY branch; VARCHAR(255), IN newFee DECIMAL(10,
SELECT branch, SUM(fee) AS total_fee 2))
FROM Student BEGIN
GROUP BY branch; UPDATE Student SET fee = newFee
SELECT address, COUNT(*) AS WHERE Sid = studentId;
student_count END
FROM Student //DELIMITER ;
GROUP BY address
HAVING student_count > 2; Drop the procedure and list the procedures
ALTER TABLE Student MODIFY present in DB
COLUMN Sid VARCHAR(255); DROP PROCEDURE IF EXISTS
SELECT sname FROM Student WHERE ExecuteQueries;
branch = 'AIML' AND fee = 95000; SHOW PROCEDURE STATUS WHERE
END Db = 'your_database_name';
//DELIMITER ;

Create a procedure with IN


DELIMITER //
CREATE PROCEDURE
GetStudentsByBranch(IN branchName
VARCHAR(255))
BEGIN
SELECT * FROM Student WHERE
branch = branchName;
END
//DELIMITER ;
6
7.A 3NF (Third Normal Form): Ensures that
Normalization and Types of Normal Forms: the table is in 2NF and that all attributes are
Normalization: Normalization is a not transitively dependent on the primary
database design technique that reduces key.
data redundancy and eliminates Example: CREATE TABLE Student
undesirable characteristics like Insertion, (StudentID INT PRIMARY KEY, Name
Update, and Deletion Anomalies. VARCHAR(50), BranchID INT);
Normalization rules divide larger tables into 4. Boyce-Codd Normal Form (BCNF):
smaller tables and define relationships BCNF is a stricter version of 3NF. A table is
among them to increase clarity in in BCNF if it is in 3NF and for every
organizing data. Types of Normal Forms: functional dependency (A → B), A should
be a super key.
1NF (First Normal Example: CREATE TABLE Course
Form): Ensures that the table has a (CourseID INT PRIMARY KEY,
primary key and that all values in a column CourseName VARCHAR(50), InstructorID
are atomic. INT, UNIQUE (CourseName, InstructorID));
Example: CREATE TABLE Student 5. Fourth Normal Form (4NF): 4NF
(StudentID INT PRIMARY KEY, Name ensures that the table is in BCNF and that it
VARCHAR(50), Age INT); has no multi-valued dependencies. A multi-
2NF (Second Normal Form): Ensures that valued dependency occurs when one
the table is in 1NF and that all non-key attribute in a table uniquely determines
attributes are fully functional dependent on another attribute in the same table.
the primary key. Example: CREATE TABLE StudentSkills
Example: CREATE TABLE Enrollment (StudentID INT, Skill VARCHAR(50),
(StudentID INT, CourseID INT, Grade PRIMARY KEY (StudentID, Skill));
CHAR(1), PRIMARY KEY (StudentID, 6. Fifth Normal Form (5NF): 5NF ensures
CourseID)); that the table is in 4NF and that it has no
join dependencies or lossless
decomposition. This means the table
should be broken down into smaller tables
without losing any data.
Example: CREATE TABLE
ProjectAssignment (ProjectID INT,
EmployeeID INT, PRIMARY KEY
(ProjectID, EmployeeID));

B.Create a procedure with cursors


1.cursor with select operation.
DELIMITER //
CREATE PROCEDURE SelectStudents()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE sid VARCHAR(255);
7
DECLARE sname VARCHAR(255); END LOOP;
DECLARE cur CURSOR FOR SELECT CLOSE cur;
Sid, sname FROM Student; END //
DECLARE CONTINUE HANDLER FOR
DELIMITER ;
NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP 3. cursor with parameter.
FETCH cur INTO sid, sname; DELIMITER //
IF done THEN CREATE PROCEDURE
LEAVE read_loop; SelectStudentsByBranch(IN branchName
END IF; VARCHAR(255))
SELECT sid, sname;
BEGIN
END LOOP;
CLOSE cur; DECLARE done INT DEFAULT FALSE;
END // DECLARE sid VARCHAR(255);
DELIMITER ; DECLARE sname VARCHAR(255);
DECLARE cur CURSOR FOR SELECT
2. cursor with insert. Sid, sname FROM Student WHERE branch
DELIMITER // = branchName;
CREATE PROCEDURE DECLARE CONTINUE HANDLER FOR
InsertStudentFromCursor() NOT FOUND SET done = TRUE;
BEGIN OPEN cur;
DECLARE done INT DEFAULT FALSE; read_loop: LOOP
DECLARE sid VARCHAR(255); FETCH cur INTO sid, sname;
DECLARE sname VARCHAR(255); IF done THEN
DECLARE age INT; LEAVE read_loop;
DECLARE branch VARCHAR(255); END IF;
DECLARE address VARCHAR(255); -- Process each row here
DECLARE fee DECIMAL(10, 2); SELECT sid, sname;
DECLARE cur CURSOR FOR SELECT END LOOP;
Sid, sname, age, branch, address, fee CLOSE cur;
FROM Student; END //
DECLARE CONTINUE HANDLER FOR DELIMITER ;
NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO sid, sname, age,
branch, address, fee;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO NewStudent (Sid, sname,
age, branch, address, fee) VALUES (sid,
sname, age, branch, address, fee);
8

Convert Attributes:
Simple attributes become columns in the
corresponding table.
ER model to a relational model: Composite attributes are broken down into
Convert Entities to Tables: simple attributes.
Create a table for each strong entity with its Multi-valued attributes are converted to a
attributes. separate table with a foreign key.
Create a table for each weak entity with a Set Primary Keys:
foreign key referencing the strong entity it Assign primary keys to each table based on
depends on. the entity’s unique identifier.
Convert Relationships to Tables: Set Foreign Keys:
One-to-One Relationships: Add a foreign Establish foreign keys to maintain
key to one of the tables involved in the referential integrity between related tables.
relationship. Handle Derived Attributes:
One-to-Many Relationships: Add a foreign Determine whether derived attributes
key to the table on the “many” side of the should be stored or calculated as needed.
relationship. Normalize the Tables:
Many-to-Many Relationships: Create a new Ensure tables are normalized to reduce
table to represent the relationship, including redundancy and improve data integrity.
foreign keys referencing the primary keys
of the related tables.

You might also like