DBMS1 Lab GT
DBMS1 Lab GT
BOOK_AUTHORS(Book_id, Author_Name)
1. Retrieve details of all books in the library – id, title, name of publisher,
authors,
2. Get the particulars of borrowers who have borrowed more than 3 books,
but
3. Delete a book in BOOK table. Update the contents of other tables to reflect
this
5. Create a view of all books and its number of copies that are currently
available
in the Library.
--Create Table PUBLISHER with Primary Key as NAME
PHONE INTEGER,
ADDRESS VARCHAR(20));
DESC PUBLISHER;
--Create Table BOOK with Primary Key as BOOK_ID and Foreign Key
PUB_NAME referring the PUBLISHER table
TITLE VARCHAR(20),
PUB_YEAR VARCHAR(20),
PUB_NAME VARCHAR(20),
DESC BOOK;
--Create Table BOOK_AUTHORS with Primary Key as BOOK_ID and
AUTHOR_NAME and Foreign Key BOOK_ID referring the BOOK table
(AUTHOR_NAME VARCHAR(20),
BOOK_ID INTEGER,
DESC BOOK_AUTHORS;
PROGRAMME_NAME VARCHAR(50),
ADDRESS VARCHAR(50));
DESC LIBRARY_PROGRAMME;
(NO_OF_COPIES INTEGER,
BOOK_ID INTEGER,
PROGRAMME_ID INTEGER,
DESC BOOK_COPIES;
DESC CARD;
PROGRAMME_ID INTEGER,
CARD_NO INTEGER,
DATE_OUT DATE,
DUE_DATE DATE,
DESC BOOKLENDING;
--------------------------
--Inserting records into BOOK table
--------------------------
-------------------------
--------------------------
--------------------------
--------------------------
--Retrieve details of all books in the library – id, title, name of publisher,
authors,
WHERE B.BOOK_ID=A.BOOK_ID
AND B.BOOK_ID=C.BOOK_ID
AND L.PROGRAMME_ID=C.PROGRAMME_ID;
---------------------------------------------
--Get the particulars of borrowers who have borrowed more than 3 books, but
SELECT CARD_NO
FROM BOOK_LENDING
GROUP BY CARD_NO
HAVING COUNT(*)>3;
---------------------------------------------
--Delete a book in BOOK table. Update the contents of other tables to reflect
this
WHERE BOOK_ID=3;
---------------------------------------------
--Partition the BOOK table based on year of publication. Demonstrate its
working
PUB_YEAR
FROM BOOK;
---------------------------------------------
--Create a view of all books and its number of copies that are currently
available
FROM
WHERE B.BOOK_ID=C.BOOK_ID
AND C.PROGRAMME_ID=L.PROGRAMME_ID;
SELECT * FROM V_BOOKS;
NAME VARCHAR(20),
CITY VARCHAR(20),
COMMISSION VARCHAR(20));
DESC SALESMAN;
--------------------------------------
--Create Table CUSTOMER with Primary Key as CUSTOMER_ID and Foreign Key
SALESMAN_ID referring the SALESMAN table
CUST_NAME VARCHAR(20),
CITY VARCHAR(20),
GRADE INTEGER,
SALESMAN_ID INTEGER,
DESC CUSTOMER;
--------------------------------------
--Create Table ORDERS with Primary Key as ORDER_NO and Foreign Key
CUSTOMER_ID and SALESMAN_ID referring the CUSTOMER and SALESMAN
tables respectively
PURCHASE_AMOUNT DECIMAL(10,2),
ORDER_DATE DATE,
CUSTOMER_ID INTEGER,
SALESMAN_ID INTEGER,
DESC ORDERS;
--Inserting records into SALESMAN table
------------------------------------------
------------------------------------------
--Inserting records into ORDERS table
FROM CUSTOMER
GROUP BY GRADE
FROM CUSTOMER
WHERE CITY='BANGALORE');
----------------------------------
--Find the name and numbers of all salesman who had more than one
customer
SELECT SALESMAN_ID, NAME
FROM SALESMAN S
FROM CUSTOMER C
----------------------------------
--List all the salesman and indicate those who have and don’t have customers
in their cities (Use UNION operation.)
WHERE S.CITY=C.CITY
UNION
FROM SALESMAN S
(SELECT CITY
FROM CUSTOMER)
ORDER BY 1 ASC;
-----------------------------------
--Create a view that finds the salesman who has the customer with the highest
order of a day.
FROM ORDERS C
WHERE C.ORDER_DATE=O.ORDER_DATE);
-----------------------------------
WHERE SALESMAN_ID=1000;
SELECT * FROM SALESMAN;
ACT_NAME VARCHAR(20),
ACT_GENDER CHAR(1));
DESC ACTOR;
----------------------------
DIR_NAME VARCHAR(20),
DIR_PHONE INTEGER);
DESC DIRECTOR;
----------------------------
--Create Table MOVIES with Primary Key as MOV_ID and Foreign Key DIR_ID
referring DIRECTOR table
MOV_TITLE VARCHAR(25),
MOV_YEAR INTEGER,
MOV_LANG VARCHAR(15),
DIR_ID INTEGER,
DESC MOVIES;
----------------------------
--Create Table MOVIE_CAST with Primary Key as MOV_ID and ACT_ID and
Foreign Key ACT_ID and MOV_ID referring ACTOR and MOVIES tables
respectively
MOV_ID INTEGER,
ROLE VARCHAR(10),
DESC MOVIE_CAST;
----------------------------
--Create Table RATING with Primary Key as MOV_ID and Foreign Key MOV_ID
referring MOVIES table
REV_STARS VARCHAR(25),
DESC RATING;
-----------------------------
------------------------------
-----------------------------
-----------------------------
--Inserting records into RATING table
SELECT MOV_TITLE
FROM MOVIES
FROM DIRECTOR
WHERE DIR_NAME='HITCHCOCK');
---------------------------------
--Find the movie names where one or more actors acted in two or more
movies.
SELECT MOV_TITLE
FROM MOVIES M,MOVIE_CAST MC
HAVING COUNT(ACT_ID)>1)
GROUP BY MOV_TITLE
HAVING COUNT(*)>1;
--------------------------------
--List all actors who acted in a movie before 2000 and also in a movie after
2015 (use JOIN operation).
SELECT ACT_NAME
FROM ACTOR A
JOIN MOVIE_CAST C
ON A.ACT_ID=C.ACT_ID
JOIN MOVIES M
ON C.MOV_ID=M.MOV_ID
--------------------------------
--Find the title of movies and number of stars for each movie that has at least
one rating
--and find the highest number of stars that movie received. Sort the result by
--movie title.
SELECT MOV_TITLE,MAX(REV_STARS)
FROM MOVIES
GROUP BY MOV_TITLE
HAVING MAX(REV_STARS)>0
ORDER BY MOV_TITLE;
---------------------------------
UPDATE RATING
SET REV_STARS=5
FROM DIRECTOR
4
--Create table STUDENT with PRIMARY KEY as USN
SNAME VARCHAR(25),
ADDRESS VARCHAR(25),
PHONE INTEGER,
GENDER CHAR(1));
DESC STUDENT;
-----------------------------------
SEM INTEGER,
SEC CHAR(1));
DESC SEMSEC;
-----------------------------------
--Create table CLASS with PRIMARY KEY as USN and FOREIGN KEY USN, SSID
SSID VARCHAR(5),
DESC CLASS;
------------------------------------
TITLE VARCHAR(20),
SEM INTEGER,
CREDITS INTEGER);
DESC SUBJECT;
--------------------------------------
USN VARCHAR(10),
SUBCODE VARCHAR(8),
SSID VARCHAR(5),
TEST1 INTEGER,
TEST2 INTEGER,
TEST3 INTEGER,
FINALIA INTEGER,
PRIMARY KEY(SUBCODE,USN,SSID),
DESC IAMARKS;
---------------------------------------
-------------------------------------
----------------------------------------
INSERT INTO IAMARKS (USN, SUBCODE, SSID, TEST1, TEST2, TEST3) VALUES
('1BI15CS101','10CS82','CSE8C', 12, 19, 14);
INSERT INTO IAMARKS (USN, SUBCODE, SSID, TEST1, TEST2, TEST3) VALUES
('1BI15CS101','10CS83','CSE8C', 19, 15, 20);
INSERT INTO IAMARKS (USN, SUBCODE, SSID, TEST1, TEST2, TEST3) VALUES
('1BI15CS101','10CS84','CSE8C', 20, 16, 19);
INSERT INTO IAMARKS (USN, SUBCODE, SSID, TEST1, TEST2, TEST3) VALUES
('1BI15CS101','10CS85','CSE8C', 15, 15, 12);
--List all the student details studying in fourth semester ‘C’ section.
----------------------------------------
--Compute the total number of male and female students in each semester and
in each section.
SELECT SS.SEM, SS.SEC, S.GENDER, COUNT(S.GENDER) AS COUNT
SS.SSID = C.SSID
ORDER BY SEM;
----------------------------------------
AS
FROM IAMARKS
----------------------------------------
--Calculate the FinalIA (average of best two test marks) and update the
corresponding table for all students.
DELIMITER //
BEGIN
FROM IAMARKS
FOR UPDATE;
OPEN C_IAMARKS;
LOOP
SET C_SUM=C_A+C_B;
ELSE
SET C_SUM=C_A+C_C;
END IF;
SET C_AVG=C_SUM/2;
END LOOP;
CLOSE C_IAMARKS;
END;
//
CALL AVG_MARKS();
SELECT * FROM IAMARKS;
--------------------------------------------
-- Give these details only for 8th semester A, B, and C section students.
(CASE
ELSE 'WEAK'
END) AS CAT
SUB.SEM = 8;
DNAME VARCHAR(20),
MGR_SSN VARCHAR(20),
MGR_START_DATE DATE);
DESC DEPARTMENT;
----------------------------------
NAME VARCHAR(20),
ADDRESS VARCHAR(20),
SEX CHAR(1),
SALARY INTEGER,
SUPERSSN VARCHAR(20),
DNO VARCHAR(20),
----------------------------------
----------------------------------
--Create Table DLOCATION with PRIMARY KEY as DNO and DLOC and FOREIGN
KEY DNO referring DEPARTMENT table
(DLOC VARCHAR(20),
DNO VARCHAR(20),
DESC DLOCATION;
----------------------------------
--Create Table PROJECT with PRIMARY KEY as PNO and FOREIGN KEY DNO
referring DEPARTMENT table
PNAME VARCHAR(20),
PLOCATION VARCHAR(20),
DNO VARCHAR(20),
DESC PROJECT;
----------------------------------
--Create Table WORKS_ON with PRIMARY KEY as PNO and SSN and FOREIGN
KEY SSN and PNO referring EMPLOYEE and PROJECT table
(HOURS INTEGER,
SSN VARCHAR(20),
PNO INTEGER,
FOREIGN KEY (SSN) REFERENCES EMPLOYEE(SSN),
DESC WORKS_ON;
----------------------------------
----------------------------------
SUPERSSN=NULL, DNO='3'
WHERE SSN='ABC01';
SUPERSSN='ABC03', DNO='5'
WHERE SSN='ABC02';
SUPERSSN='ABC04', DNO='5'
WHERE SSN='ABC03';
SUPERSSN='ABC06', DNO='5'
WHERE SSN='ABC04';
DNO='5', SUPERSSN='ABC06'
WHERE SSN='ABC05';
UPDATE EMPLOYEE SET
DNO='5', SUPERSSN='ABC07'
WHERE SSN='ABC06';
DNO='5', SUPERSSN=NULL
WHERE SSN='ABC07';
DNO='1', SUPERSSN='ABC09'
WHERE SSN='ABC08';
DNO='1', SUPERSSN=NULL
WHERE SSN='ABC09';
DNO='4', SUPERSSN=NULL
WHERE SSN='ABC10';
DNO='2', SUPERSSN=NULL
WHERE SSN='ABC11';
-------------------------------
--------------------------------
------------------------------
--Make a list of all project numbers for projects that involve an employee
whose last name is ‘Scott’, either as a worker or as a manager of the
department that controls the project.
WHERE E.DNO=D.DNO
AND D.MGR_SSN=E.SSN
UNION
WHERE P1.PNO=W.PNO
AND E1.SSN=W.SSN
WHERE E.SSN=W.SSN
AND W.PNO=P.PNO
AND P.PNAME='IOT';
--Find the sum of the salaries of all employees of the ‘Accounts’ department, as
well as the maximum salary, the minimum salary, and the average salary in this
department
WHERE E.DNO=D.DNO
AND D.DNAME='ACCOUNTS';
--Retrieve the name of each employee who works on all the projects controlled
by department number 5 (use NOT EXISTS operator).
SELECT E.NAME
FROM EMPLOYEE E
WHERE NOT EXISTS(SELECT PNO FROM PROJECT WHERE DNO='5' AND PNO
NOT IN (SELECT
WHERE E.SSN=SSN));
--For each department that has more than five employees, retrieve the
department number and the number of its employees who are making more
than Rs. 6,00,000.
WHERE D.DNO=E.DNO
FROM EMPLOYEE E1
GROUP BY E1.DNO
HAVING COUNT(*)>5)
GROUP BY D.DNO;