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

DBMS LAB MANUAL

The DBMS Lab Manual for Siddhartha Academy of Higher Education provides an overview of SQL, including its commands categorized into DDL, DML, TCL, and DCL. It includes practical exercises for creating and manipulating a Library Database and an Order Database using SQL queries. The manual is intended for students in the Artificial Intelligence & Machine Learning department during the 2024-2025 academic year.

Uploaded by

mjbhuvan97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DBMS LAB MANUAL

The DBMS Lab Manual for Siddhartha Academy of Higher Education provides an overview of SQL, including its commands categorized into DDL, DML, TCL, and DCL. It includes practical exercises for creating and manipulating a Library Database and an Order Database using SQL queries. The manual is intended for students in the Artificial Intelligence & Machine Learning department during the 2024-2025 academic year.

Uploaded by

mjbhuvan97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

DBMS LAB MANUAL

SIDDHARTHA ACADEMY OF HIGHER EDUCATION


(DEEMED TO BE UNIVERSITY)
Accredited A+ Grade by NAAC
SRI SIDDHARTHA INSTITUTE OF TECHNOLOGY
(A Constituent College of SSAHE)
Maralur, Kunigal Road, TUMAKURU-572105

Department: Artificial Intelligence & Machine Learning


Subject: Database Management Systems Laboratory
Subject Code: 22AM502
Semester: 5
Academic Year-2024-2025

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

BASIC CONCEPTS OF SQL


1.Introduction to SQL
SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel
– (Structured English Query Language)”. It is a query language used for accessing and
modifying information in the database.

2.SQL Commands
SQL commands are instructions used to communicate with the database to perform specific
task that work with data. SQL commands can be used not only for searching the database but
also to perform various other functions like, for example, you can create tables, add data to
tables, or modify data, drop the table, set permissions for users.
SQL commands are grouped into four major categories depending on their functionality:
• Data Definition Language (DDL) - These SQL commands are used for creating,
modifying, and dropping the structure of database objects. The commands are
CREATE, ALTER, DROP, RENAME, and TRUNCATE.
• Data Manipulation Language (DML) - These SQL commands are used for storing,
retrieving, modifying, and deleting data. These commands are SELECT, INSERT,
UPDATE, and DELETE.
• Transaction Control Language (TCL) - These SQL commands are used for managing
changes affecting the data. These commands are COMMIT, ROLLBACK, and
SAVEPOINT.
• Data Control Language (DCL) - These SQL commands are used for providing
security to database objects. These commands are GRANT and REVOKE.

2.1 Data Definition Language (DDL)

1. CREATE TABLE Statement

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

1) Primary key:

This constraint defines a column or combination of columns which uniquely identifies each
row in the table.

2) Foreign key or Referential Integrity:

This constraint identifies any column referencing the PRIMARY KEY in another table. It
establishes a relationship between two columns in the same table or between different
tables. For a column to be defined as a Foreign Key, it should be a defined as a Primary
Key in the table which it is referring. One or more columns can be defined as Foreign key.

Syntax to define a Foreign key at column level:

2.ALTER TABLE
Statement The SQL ALTER TABLE command is used to modify the definition structure) of a
table by modifying the definition of its columns.

The ALTER command is used to perform the following functions.


1) Add, drop, modify table columns
2) Add and drop constraints
3) Enable and Disable constraints

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

3 The DROP TABLE Statement


The DROP TABLE statement is used to delete a table.
DROP TABLE table_name;
4 TRUNCATE TABLE Statement
What if we only want to delete the data inside the table, and not the table itself?
Then, use the TRUNCATE TABLE statement:
TRUNCATE TABLE table_name;

2.2Data Manipulation Language (DML):


1. The SELECT Statement
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

SELECT Syntax:
SELECT * FROM table_name;

2.The ORDER BY Clause


• The ORDER BY clause is used to sort the result-set by a specified column.
• The ORDER BY clausesort the records in ascending order by default.
• If you want to sort the records in a descending order, you can use the DESC
Keyword

The UPDATE Statement


The UPDATE statement is used to update existing records in a table.

SQL UPDATE Syntax:

The DELETE Statement


The DELETE statement is used to delete rows in a table.
SQL DELETE Syntax:

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

PROGRAME – 1
LIBRARY DATABASE

1. Consider the following schema for a Library Database:


BOOK (Book_id, Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS (Book_id, Author_Name)
PUBLISHER (Name, Address, Phone)
BOOK_COPIES (Book_id, Programme_id, No-of_Copies)
BOOK_LENDING (Book_id, Programme_id, Card_No, Date_Out, Due_Date)
LIBRARY_PROGRAMME (Programme_id, Programme_Name, Address)
Write SQL queries to
i) Retrieve details of all books in the library – id, title, name of publisher, authors,
number of copies in each Programme, etc.
ii) Get the particulars of borrowers who have borrowed more than 3 books, but from
Jan 2017 to Jun 2017.
iii) Delete a book in BOOK table. Update the contents of other tables to reflect this
data Manipulation operation.
iv) Partition the BOOK table based on year of publication. Demonstrate its working
with a simple query.
v) Create a view of all books and its number of copies that are currently available in
the Library.

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

Table Creation:

1.PUBLISHER
SQL> CREATE TABLE PUBLISHER(
NAME VARCHAR(18) PRIMARY KEY,
ADDRESS VARCHAR(10),
PHONE VARCHAR(10));

Table created.

2.BOOK
SQL> create table BOOK
(
Book_id int primary key,
Title varchar(20),
Publisher_Name varchar(20) references PUBLISHER(Name) ON DELETE
CASCADE,
Pub_Year int
);
Table created.

3.BOOK_AUTHORS
SQL> CREATE TABLE BOOK_AUTHORS(

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

BOOK_ID INTEGER REFERENCES BOOK(BOOK_ID) ON DELETE


CASCADE, AUTHOR_NAME VARCHAR(20), PRIMARY KEY(BOOK_ID));

Table created.

4.LIBRARY_BRANCH
SQL> CREATE TABLE LIBRARY_BRANCH(
BRANCH_ID INTEGER PRIMARY KEY,
BRANCH_NAME VARCHAR(18),
ADDRESS VARCHAR(15));

Table created.

5.BOOK_COPIES
SQL> CREATE TABLE BOOK_COPIES(
BOOK_ID INTEGER REFERENCES BOOK(BOOK_ID) ON DELETE
CASCADE, BRANCH_ID INTEGER REFERENCES
LIBRARY_BRANCH(BRANCH_ID) ON DELETE CASCADE, NO_OF_COPIES
INTEGER, PRIMARY KEY(BOOK_ID,BRANCH_ID));

Table created.

6.BOOK_LENDING
SQL> CREATE TABLE BOOK_LENDING(
BOOK_ID INTEGER REFERENCES BOOK(BOOK_ID) ON DELETE
CASCADE, BRANCH_ID INTEGER REFERENCES
LIBRARY_BRANCH(BRANCH_ID) ON DELETE CASCADE, CARD_NO
INTEGER, DATE_OUT VARCHAR(20), DUE_DATE VARCHAR(20), PRIMARY
KEY(BOOK_ID,BRANCH_ID,CARD_NO));

Table created.

Values for tables:


PUBLISHER
SQL>INSERT INTO PUBLISHER VALUES('PEARSON','BANGALORE','9875462530');
SQL>INSERT INTO PUBLISHER VALUES('MCGRAW','NEWDELHI','7845691234');
SQL> INSERT INTO PUBLISHER VALUES('SAPNA','BANGALORE','7845963210');

BOOK
SQL> INSERT INTO BOOK VALUES(1111,'SE','PEARSON',2005);
SQL> INSERT INTO BOOK VALUES(2222,'DBMS','MCGRAW',2004);
SQL> INSERT INTO BOOK VALUES(3333,'ANOTOMY','PEARSON',2010);
SQL> INSERT INTO BOOK VALUES(4444,'ENCYCLOPEDIA','SAPNA',2010);

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

BOOK_AUTHORS
SQL> INSERT INTO BOOK_AUTHORS VALUES(1111,'SOMMERVILLE');
SQL> INSERT INTO BOOK_AUTHORS VALUES(2222,'NAVATHE');
SQL> INSERT INTO BOOK_AUTHORS VALUES(3333,'HENRY GRAY');
SQL> INSERT INTO BOOK_AUTHORS VALUES(4444,'THOMAS');

LIBRARY_BRANCH
SQL>INSERT INTO LIBRARY_BRANCH VALUES(11,'CENTRAL TECHNICAL','MG
ROAD');
SQL>INSERT INTO LIBRARY_BRANCH VALUES(22,'MEDICAL','BH ROAD');
SQL> INSERT INTO LIBRARY_BRANCH VALUES(33,'CHILDREN','SS PURAM');
SQL>INSERT INTO LIBRARY_BRANCH VALUES(44,'SECRETARIAT','SIRAGATE');
SQL>INSERT INTO LIBRARY_BRANCH VALUES(55,'GENERAL','JAYANAGAR');

BOOK_COPIES
SQL> INSERT INTO BOOK_COPIES VALUES(1111,11,5);
SQL> INSERT INTO BOOK_COPIES VALUES(3333,22,6);
SQL> INSERT INTO BOOK_COPIES VALUES(4444,33,10);
SQL> INSERT INTO BOOK_COPIES VALUES(2222,11,12);
SQL> INSERT INTO BOOK_COPIES VALUES(4444,55,3);

BOOK_LENDING
SQL> INSERT INTO BOOK_LENDING VALUES(2222,11,1,'10-01-17','20-08-17');
SQL> INSERT INTO BOOK_LENDING VALUES(3333,22,2,'09-07-17','12-08-17');
SQL> INSERT INTO BOOK_LENDING VALUES(4444,55,1,'11-04-17','09-08-17');
SQL> INSERT INTO BOOK_LENDING VALUES(2222,11,5,'09-08-17','19-08-17');
SQL> INSERT INTO BOOK_LENDING VALUES(4444,33,1,'10-06-17','15-08-17');
SQL> INSERT INTO BOOK_LENDING VALUES(1111,11,1,'12-05-17','10-06-17');
SQL> INSERT INTO BOOK_LENDING VALUES(3333,22,1,'10-07-17','15-07-17');

SQL> SELECT * FROM BOOK;

SQL> SELECT * FROM BOOK_AUTHORS;

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

SQL> SELECT * FROM PUBLISHER;

SQL> SELECT * FROM BOOK_COPIES;

SQL> SELECT * FROM BOOK_LENDING;

SQL> SELECT * FROM LIBRARY_BRANCH;

Queries:

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

1) Retrieve details of all books in the library – id, title, name of publisher, authors,
number of copies in each branch, etc

SELECT LB.BRANCH_NAME, B.BOOK_ID,TITLE,


PUBLISHER_NAME,AUTHOR_NAME, NO_OF_COPIES
FROM BOOK B, BOOK_AUTHORS BA, BOOK_COPIES BC, LIBRARY_BRANCH LB
WHERE B.BOOK_ID = BA.BOOK_ID AND
BA.BOOK_ID = BC.BOOK_ID AND
BC.BRANCH_ID = LB.BRANCH_ID
GROUP BY LB.BRANCH_NAME, B.BOOK_ID, TITLE, PUBLISHER_NAME,
AUTHOR_NAME, NO_OF_COPIES;

5 rows in set (0.01 sec)

2) Get the particulars of borrowers who have borrowed more than 3 books, but from Jan
2017 to Jun 2017.

SELECT CARD_NO
FROM BOOK_LENDING
WHERE DATE_OUT BETWEEN '01-01-17' AND '30-06-17'
GROUP BY CARD_NO
HAVING COUNT(*) > 3;

3) Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.

DELETE FROM BOOK


WHERE BOOK_ID = '3333';

1 row deleted.

SELECT*FROM BOOK;

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

SQL> SELECT * FROM BOOK_COPIES;

SQL> SELECT * FROM BOOK_LENDING;

4) Partition the BOOK table based on year of publication. Demonstrate its working with a
simple query.

SELECT BOOK_ID, TITLE, PUBLISHER_NAME, PUB_YEAR


FROM BOOK
GROUP BY PUB_YEAR, BOOK_ID, TITLE, PUBLISHER_NAME;

5) Create a view of all books and its number of copies that are currently available in the
Library.

CREATE VIEW BOOKS_AVAIL AS


SELECT B.BOOK_ID, B.TITLE, C.NO_OF_COPIES
FROM LIBRARY_BRANCH L, BOOK B, BOOK_COPIES C
WHERE B.BOOK_ID = C.BOOK_ID AND L.BRANCH_ID=C.BRANCH_ID;
View created.

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

PROGRAME – 2
ORDER DATABASE

2. Consider the following schema for Order Database:


SALESMAN (Salesman_id, Name, City, Commission)
CUSTOMER (Customer_id, Cust_Name, City, Grade, Salesman_id)
ORDERS (Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id)

Write SQL queries to


i) Count the customers with grades above Bangalore’s average.
ii) Find the name and numbers of all salesman who had more than one customer.
iii) List all the salesman and indicate those who have and do not have customers
in their cities (Use UNION operation.)
iv) Create a view that finds the salesman who has the customer with the highest
order of a day.
v) Demonstrate the DELETE operation by removing salesman with id 1000. All
his orders must also be deleted.

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

Table Creation:

1.SALESMAN

CREATE TABLE SALESMAN(SALESMAN_ID NUMBER(5) CONSTRAINT


SALESMAN_SALID PRIMARY KEY, NAME VARCHAR(10) CONSTRAINT
SALESMAN_NAME_NN NOT NULL, CITY VARCHAR(15) CONSTRAINT
SALESMAN_CITY_NN NOT NULL, COMMISSION NUMBER(5));

Table created.

2.CUSTOMER

CREATE TABLE CUSTOMER( CUSTOMER_ID NUMBER(5) CONSTRAINT


CUSTOMER_CUSTID_PK PRIMARY KEY, CUST_NAME VARCHAR(10)
CONSTRAINT CUSTOMER_CUSTNAME_NN NOT NULL, CITY VARCHAR(10)
CONSTRAINT CUSTOMER_CITY_NN NOT NULL, GRADE NUMBER(5)
CONSTRAINT CUSTOMER_GRADE_NN NOT NULL, SALESMAN_ID NUMBER(5)
CONSTRAINT CUSTOMER_SALEID_FK REFERENCES
SALESMAN(SALESMAN_ID) ON DELETE SET NULL);

Table created.

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

3.ORDERS

CREATE TABLE ORDERS( ORD_NO NUMBER(5) CONSTRAINT


ORDERS_ODNO_PK PRIMARY KEY, PURCHASE_AMT INTEGER CONSTRAINT
ORDERS_PAMT_NN NOT NULL, ORD_DATE DATE CONSTRAINT
ORDERS_ODATE_NN NOT NULL, CUSTOMER_ID NUMBER(5)
CUSTOMER(CUSTOMER_ID), SALESMAN_ID NUMBER(5) CONSTRAINT
CONSTRAINT ORDERS_CUSTID_FK REFERENCES ORDERS_SALEID_FK
REFERENCES SALESMAN(SALESMAN_ID) ON DELETE CASCADE);

Table created.

Values for tables


SQL> INSERT INTO SALESMAN
VALUES(&SALESMAN_ID,'&NAME','&CITY',&COMMISSION);
SQL> INSERT INTO CUSTOMER
VALUES(&CUSTOMER_ID,'&CUST_NAME','&CITY','&GRADE',&SALESMAN_ID);
SQL> INSERT INTO ORDERS
VALUES(&ORD_NO,&PURCHASE_AMT,'&ORD_DATE',&CUSTOMER_ID,&SALES
AN_ID);

SELECT * FROM SALESMAN;

SELECT * FROM CUSTOMER;

SELECT * FROM ORDERS;

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

1. Count the customers with grades above Bangalore’s average.


SELECT COUNT(CUSTOMER_ID)
FROM CUSTOMER
WHERE GRADE>(SELECT AVG(GRADE) FROM CUSTOMER WHERE CITY
LIKE '%BENGALURU');

2. Find the name and numbers of all salesmen who had more than one customer.

SELECT NAME, COUNT(CUSTOMER_ID)


FROM SALESMAN S, CUSTOMER C
WHERE S.SALESMAN_ID=C.SALESMAN_ID
GROUP BY NAME
HAVING COUNT(CUSTOMER_ID)>1;

3. List all salesmen and indicate those who have and don’t have customers in their cities
(Use UNION operation.)

(SELECT NAME
FROM SALESMAN S, CUSTOMER C
WHERE S.SALESMAN_ID=C.SALESMAN_ID AND S.CITY=C.CITY)
UNION
(SELECT NAME
FROM SALESMAN
WHERE SALESMAN_ID NOT IN(SELECT S1.SALESMAN_ID FROM
SALESMAN S1, CUSTOMER C1 WHERE
S1.SALESMAN_ID=C1.SALESMAN_ID AND S1.CITY=C1.CITY));

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

4. Create a view that finds the salesman who has the customer with the highest order of a
day
CREATE VIEW SALES_HIGHERODER AS
SELECT SALESMAN_ID, PURCHASE_AMT
FROM ORDERS
WHERE PURCHASE_AMT=(SELECT MAX(O.PURCHASE_AMT) FROM
ORDERS O WHERE O.ORD_DATE='12-APR-16');

View created.

SELECT * FROM SALES_HIGHERODER;

5. Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also be deleted.
DELETE from salesman
WHERE salesman_id = 1000;

1 row deleted.

SELECT * FROM SALESMAN;

SELECT * FROM CUSTOMER;

SELECT * FROM ORDERS;

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

PROGRAME– 3
MOVIE DATABASE

3. Consider the schema for Movie Database:


ACTOR (Act_id, Act_Name, Act_Gender)
DIRECTOR (Dir_id, Dir_Name, Dir_Phone)
MOVIES (Mov_id,Mov_ Title, Mov_Year, Mov_Lang, Dir_id) MOVIE CAST (Act_id,
Mov_id, Role)
RATING (Mov_id, Rev_Stars)

Write SQL queries to


i) List the titles of all movies directed by ‘Hitchcock’.
ii) Find the movie names where one or more actors acted in two or more movies.
iii) List all actors who acted in a movie before 2000 and in a movie after 2015 (use
JOIN operation).
iv) 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. 5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

Table Creation:
ACTOR
CREATE TABLE ACTOR( ACT_ID NUMBER(5) CONSTRAINT ACTOR_ACTID_PK
PRIMARY KEY, ACT_NAME VARCHAR(18) CONSTRAINT ACTOR_ACTNAME_NN
NOT NULL, ACT_GENDER VARCHAR(2) CONSTRAINT ACTOR_ACTGENDER_NN
NOT NULL);

Table created.

DIRECTOR
CREATE TABLE DIRECTOR( DIR_ID NUMBER(5) CONSTRAINT
DIRECTOR_DIRID_PK PRIMARY KEY, DIR_NAME VARCHAR(18) CONSTRAINT
DIRECTOR_DIRNAME_NN NOT NULL, DIR_PHONE VARCHAR(10) CONSTRAINT
DIRECTOR_DIRPHONE_NN NOT NULL);

Table created.

MOVIES
CREATE TABLE MOVIES( MOV_ID NUMBER(5) CONSTRAINT
MOVIES_MOVID_PK PRIMARY KEY, MOV_TITLE VARCHAR(10) CONSTRAINT
MOVIES_MOVTITLE_NN NOT NULL, MOV_YEAR NUMBER(5) CONSTRAINT
MOVIES_MOVYEAR_NN NOT NULL, MOV_LANG VARCHAR(10) CONSTRAINT
MOVIES_MOVLANG_NN NOT NULL, DIR_ID NUMBER(5) CONSTRAINT
MOVIES_DIRID_FK REFERENCES DIRECTOR(DIR_ID));

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

Table created.

MOVIE_CAST
CREATE TABLE MOVIE_CAST (ACT_ID NUMBER(5) CONSTRAINT
MOVIECAST_ACTID_FK REFERENCES ACTOR(ACT_ID), MOV_ID NUMBER(5)
CONSTRAINT MOVIECAST_MOVID_FK REFERENCES MOVIES(MOV_ID), ROLE
VARCHAR(10), CONSTRAINT MOVIECAST_ACTID_MOVID_PK PRIMARY
KEY(ACT_ID,MOV_ID));

Table created.

RATING
CREATE TABLE RATING( MOV_ID NUMBER(5) CONSTRAINT RATING_MOVID_FK
REFERENCES MOVIES(MOV_ID), REV_STARS NUMBER(1) CONSTRAINT
RATING_REVSTARS_NN NOT NULL, CONSTRAINT RATING_MOVID_PK PRIMARY
KEY(MOV_ID))

Table created.

Description of Schema:

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

Values for tables:


SQL> INSERT INTO ACTOR VALUES(&ACT_ID,'&ACT_NAME','&ACT_GENDER');
SQL> INSERT INTO DIRECTOR VALUES(&DIR_ID,'&DIR_NAME',&DIR_PHONE);
SQL> INSERT INTO MOVIES VALUES
(&MOV_ID,'&MOV_TITLE','&MOV_YEAR','&MOV_LANG',&DIR_ID);
SQL> INSERT INTO MOVIE_CAST VALUES(&ACT_ID,&MOV_ID,'&ROLE');
SQL> INSERT INTO RATING VALUES(&MOV_ID,&REV_STARS);

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

1. List the titles of all movies directed by ‘Hitchcock’.


SELECT MOV_TITLE
FROM MOVIES M, DIRECTOR D
WHERE D.DIR_ID=M.DIR_ID AND
DIR_NAME='HITCHCOCK';

2. Find the movie names where one or more actors acted in two or more movies.
SELECT MOV_TITLE FROM MOVIES M, MOVIE_CAST MC WHERE
M.MOV_ID=MC.MOV_ID AND MC.ACT_ID IN (SELECT ACT_ID FROM
MOVIE_CAST GROUP BY ACT_ID HAVING COUNT(MOV_ID)>=2);

3. 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 4.
A.ACT_ID=C.ACT_ID JOIN MOVIES M ON C.MOV_ID=M.MOV_ID WHERE
M.MOV_YEAR < 2000) INTERSECT (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 WHERE M.MOV_YEAR > 2015);

4. 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, REV_STARS FROM MOVIES M, RATING R WHERE
M.MOV_ID=R.MOV_ID AND REV_STARS>=1 ORDER BY MOV_TITLE

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

6. Update rating of all movies directed by ‘Steven Spielberg’ to 5.


UPDATE RATING SET REV_STARS=5 WHERE MOV_ID IN (SELECT MOV_ID
FROM MOVIES M, DIRECTOR D WHERE M.DIR_ID=D.DIR_ID AND
DIR_NAME='STEVEN SPIELBERG');

1 row updated.

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

PROGRAME - 4
COLLEGE DATABASE

4. Consider the schema for College Database:


STUDENT (USN, SName, Address, Phone, Gender)
SEMSEC (SSID, Sem, Sec) CLASS(USN, SSID)
COURSE (Subcode, Title, Sem, Credits)
IAMARKS (USN, Subcode, SSID, Test1, Test2, Test3, Final-IA)
Write SQL queries to
i) List all the student details studying in fourth semester ‘C’ section.
ii) Compute the total number of male and female students in each semester and in each section.
iii) Create a view of Test1 marks of student USN ‘1BI15CS101’ in all Courses.
iv) Calculate the FinalIA (average of best two test marks) and update the corresponding table
for all students.
v) Categorize students based on the following criterion:
If FinalIA = 17 to 20 then CAT = ‘Outstanding’
If FinalIA = 12 to 16 then CAT = ‘Average’ If FinalIA< 12 then CAT = ‘Weak’
Give these details only for 8th semester A, B, and C section students.

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

Table Creation:

STUDENT
CREATE TABLE STUDENT
(USN VARCHAR(10) PRIMARY KEY,
SNAME VARCHAR(25),
ADDRESS VARCHAR(25),
PHONE VARCHAR(10),
GENDER CHAR(1));

Table created.

SEMSEC

CREATE TABLE SEMSEC


SSID VARCHAR(5) PRIMARY KEY,
SEM NUMBER(2),
SEC CHAR(1));

Table created.

CLASS

CREATE TABLE CLASS


(USN VARCHAR(10),
SSID VARCHAR(5), PRIMARY

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

KEY(USN,SSID),
FOREIGN KEY(USN) REFERENCES STUDENT(USN),
FOREIGN KEY(SSID) REFERENCES SEMSEC(SSID));

Table created.

SUBJECT

CREATE TABLE SUBJECT


(SUBCODE VARCHAR(8) PRIMARY KEY,
TITLE VARCHAR(20),
SEM NUMBER(2), CREDITS
NUMBER(2));

Table created.

IAMARKS

CREATE TABLE IAMARKS


(USN VARCHAR(10),
SUBCODE VARCHAR(8),
SSID VARCHAR(5), TEST1
NUMBER(2), TEST2
NUMBER(2), FINALIA NUMBER(3), PRIMARY KEY(USN,SUBCODE,SSID),
FOREIGN KEY(USN) REFERENCES STUDENT(USN),
FOREIGN KEY(SUBCODE) REFERENCES SUBJECT(SUBCODE),
FOREIGN KEY(SSID) REFERENCES SEMSEC(SSID));

Table created.

Values for tables:


STUDENT:
INSERT INTO STUDENT VALUES ('&USN','&sname','&address',’&phone’,'&gender');
select * from student;

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

SEMSEC:

INSERT INTO SEMSEC VALUES ('&SSID', ‘&sem’,'&sec');

select * from semsec;

CLASS:

INSERT INTO CLASS VALUES (‘&USN’,’&SSID’);

select * from class;

SUBJECT:

INSERT INTO SUBJECT VALUES ('10CS81','ACA', 8, 4);

select * from subject;

IAMARKS:

INSERT INTO IAMARKS VALUES


(‘&USN’,‘&SUBCODE’,’&SSID’,’&TEST1’,’&TEST2’,’&TEST3’);

select * from iamarks;

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

Queries: 1.

List all the student details studying in fourth semester ‘C’ section.

select s.usn,sname,address,phone,gender from student s, class c, semsec ss where sem=4 and


sec='c' and ss.ssid=c.ssid and c.usn=s.usn;

2. Compute the total number of male and female students in each semester and in each
section.

SELECT SEM,SEC,GENDER,COUNT(*) FROM STUDENT S, SEMSEC SS,CLASS C


WHERE S.USN=C.USN AND C.SSID=SS.SSID GROUP BY SEM,SEC,GENDER ORDER
BY SEM;

3. Create a view of Test1 marks of student USN ‘1BI15CS101’ in all subjects.

CREATE VIEW TEST1 AS SELECT SUBCODE,TEST1 FROM IAMARKS WHERE


USN='1cg15ee065';

View created.
SQL> select * from test1;

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

4. Calculate the FinalIA (average of best two test marks) and update the corresponding table
for all students.

CREATE OR REPLACE PROCEDURE AVG IS CURSOR C_IAMARKS IS SELECT


GREATEST(TEST1,TEST2) AS A,GREATEST(TEST1,TEST3) AS B,
GREATEST(TEST3,TEST2) AS C FROM IAMARKS WHERE FINALIA IS NULL

FOR UPDATE;
C_A NUMBER;
C_B NUMBER;
C_C NUMBER;
C_SM NUMBER;
C_AV NUMBER;
BEGIN
OPEN C_IAMARKS;
LOOP
FETCH C_IAMARKS INTO C_A,C_B,C_C;
EXIT WHEN C_IAMARKS%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(C_A||' '||C_B||' '||C_C);
IF(C_A!=C_B) THEN
C_SM:=C_A+C_B;
ELSE
C_SM:=C_A+C_C;
END IF;
C_AV:=C_SM/2; DBMS_OUTPUT.PUT_LINE('SUM='||C_SM);
DBMS_OUTPUT.PUT_LINE('AVERAGE='||C_AV);
UPDATE IAMARKS
SET FINALIA=C_AV
WHERE CURRENT OF C_IAMARKS;
END LOOP;
CLOSE C_IAMARKS;
END AVG;

Procedure created.
SQL> BEGIN
1 AVG;
2 END;
PL/SQL procedure successfully completed.

SQL> SELECT * FROM IAMARKS;

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

5. Categorize students based on the following criterion:


If FinalIA = 17 to 20 then CAT = ‘Outstanding’
If FinalIA = 12 to 16 then CAT = ‘Average’
If FinalIA< 12 then CAT = ‘Weak’
Give these details only for 8th semester A, B, and C section students.

SELECT S.USN,S.SNAME,S.ADDRESS,S.PHONE,S.GENDER,
CASE WHEN IA.FINALIA BETWEEN 17 AND 20 THEN 'OUTSTANDING'
WHEN IA.FINALIA BETWEEN 12 AND 16 THEN 'AVERAGE'
ELSE 'WEAK'
END AS CAT
FROM STUDENT S,SEMSEC SS,IAMARKS IA,SUBJECT SUB
WHERE S.USN=IA.USN AND
SS.SSID=IA.SSID AND
SUB.SUBCODE=IA.SUBCODE AND
SUB.SEM=7

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

CHAPTER – 5
COMPANY DATABASE
5. Consider the schema for Company Database:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo,DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON (SSN, PNo, Hours)
Write SQL queries to
i) 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.
ii) Show the resulting salaries if every employee working on the ‘IoT’ project is
given a 10 percent raise.
iii) 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
iv) Retrieve the name of each employee who works on all the projects controlled by
department number
v) (use NOT EXISTS operator). 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.

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

Table Creation:

DEPARTMENT
CREATE TABLE DEPARTMENT(
DNO NUMBER(3) CONSTRAINT DEPT_DNO_PK PRIMARY KEY, DNAME
VARCHAR(15) CONSTRAINT DEPT_DNAME_NN NOT NULL, MGRSSN
CHAR(10),
MGRSTARTDATE DATE);

EMPLOYEE
CREATE TABLE EMPLOYEE(
SSN CHAR(10) CONSTRAINT EMP_SSN_PK PRIMARY KEY,
NAME VARCHAR(18) CONSTRAINT EMP_NAME_NN NOT NULL,
ADDRESS VARCHAR(18),
SEX VARCHAR(3), SALARY
REAL, SUPER_SSN CHAR(10),
DNO NUMBER(3) CONSTRAINT EMP_DNO_FK REFERENCES
DEPARTMENT(DNO));
ALTER TABLE DEPARTMENT ADD CONSTRAINT DEPT_MGRSSN_FK FOREIGN
KEY(MGRSSN) REFERENCES EMPLOYEE(SSN);

Table altered.

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

DLOCATION
CREATE TABLE DLOCATION(
DLOC VARCHAR2 (20),
DNO REFERENCES DEPARTMENT (DNO),
PRIMARY KEY (DNO, DLOC));

PROJECT
CREATE TABLE PROJECT(
PNO INTEGER PRIMARY KEY,
PNAME VARCHAR2 (20),
PLOCATION VARCHAR2 (20),
DNO REFERENCES DEPARTMENT (DNO));

WORKS_ON
CREATE TABLE WORKS_ON(
HOURS NUMBER (2),
SSN REFERENCES EMPLOYEE (SSN),
PNO REFERENCES PROJECT(PNO),
PRIMARY KEY (SSN, PNO));

Values for tables:


DEPARTMENT
INSERT INTO DEPARTMENT VALUES (& DNO,' & DNAME', &MGRSSN,'
&MGRSTARTDATE');

SELECT * FROM DEPARTMENT;

EMPLOYEE
INSERT INTO EMPLOYEE VALUES ('&SSN','&NAME','&ADDRESS','&SEX',
&SALARY,'&SUPERSSN',& DNO);

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

SELECT * FROM EMPLOYEE;

DLOCATION

INSERT INTO DLOCATION VALUES(&DNO,'&DLOC');

PROJECT

INSERT INTO PROJECT VALUES(&PNO,'&PNAME','&PLOCATION',’&DNO’);

SELECT * FROM PROJECT;

WORKS_ON

INSERT INTO WORKS_ON VALUES('&SSN',&PNO,&HOURS);

SELECT * FROM WORKS_ON;

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

1. 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.

(SELECT DISTINCT PNO


FROM PROJECT P, DEPARTMENT D,
EMPLOYEE E WHERE P.DNO=D.DNO AND
SSN=MGRSSN AND
NAME='SCOTT')
UNION
(SELECT DISTINCT P.PNO
FROM PROJECT P, WORKS_ON W,
EMPLOYEE E WHERE P.PNO=W.PNO AND
W.SSN=E.SSN AND
NAME='SCOTT');

2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10
percent raise.

SELECT FNAME, LNAME, 1.1*SALARY AS INCR_SAL


FROM EMPLOYEE E, WORKS_ON W, PROJECT P WHERE E.SSN=W.SSN AND
W.PNO=P.PNO AND P.PNAME=’IOT’;

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

3. 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.

SELECT SUM(SALARY), MAX(SALARY), MIN(SALARY),


AVG(SALARY) FROM EMPLOYEE E, DEPARTMENT D
WHERE DNAME='ACCOUNTS' AND
D.DNO=E.DNO;

4. Retrieve the name of each employee who works on all the projects controlled by
department number 5 (use NOT EXISTS operator).

SELECT NAME FROM


EMPLOYEE E
WHERE NOT EXISTS( (SELECT PNO
FROM ROJECT
WHERE
DNO=5)
MINUS
(SELECT PNO FROM WORKS_ON W WHERE E.SSN=W.SSN))

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

5. 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.

SELECT DNO,COUNT(SSN)
FROM EMPLOYEE WHERE SALARY>600000 AND DNO
IN(SELECT DNO FROM EMPLOYEE
GROUP BY DNO HAVING COUNT(SSN)>5) GROUP BY DNO ;

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

VIVA QUESTIONS

1. Define Data.
2. Define Information.
3. Define Database.
4. Define DBMS.
5. What do you mean by processed data?
6. What do you mean by data management?
7. Which are the actions that are performed on the database?
8. Mention the different types of DBMS.
9. Define Data model.
10. Mention the different types of Data models.
11. Why database approach is advantageous than the file system approach?
12. Who is called as the father of RDBMS?
13. What do you mean by redundant data?
14. What do you mean by Data duplication?
15. Mention the different relational algebra operations.
16. Mention the different User interfaces provided by the database system.
17. Mention the different languages provided by the database system
18. What is the difference between select operation in relational algebra and in SQL?
19. What is the difference between JOIN and Cartesian product?
20. Mention the different types of Join operations.
21. What is the difference between EQUIJOIN and NATURAL JOIN?
22. What is the difference between OUTER JOIN and JOIN.?
23. What is the difference between OUTER UNION and UNION?
24. What do you mean by Union Compatibility.?
25. What do you mean by Type Compatibility?
26. Mention the different types of relational constraints.
27. Mention the different types of structural constraints
28. What do you mean by cardinality?
29. What do you mean by cardinality ratio?
30. What do you mean by degree of a relation?
31. What do you mean by entity integrity constraint?
32. What do you mean by referential integrity constraint?
33. What do you mean by NULL constraint?
34. What do you mean by unique constraint?
35. What do you mean by Check constraint?
36. Define functional dependency.
37. Define normalization.
38. Define normal form
39. Mention the different types of normal forms
40. What is the difference between 3NF and BCNF?
41. What do you mean by JOIN dependencies?

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

42. What do you mean by Inclusion dependencies?


43. What do you mean by Template dependencies?
44. What do you mean by Multivalued dependencies?
45. Define Project Join Normal form.
46. Define Domain Key Normal form.
47. Mention the informal guidelines for database design.
48. Define super key.
49. Define primary key.
50. Define foreign key.
51. Define unique key.
52. Define prime attribute.
53. Define trivial functional dependency.
54. When a FD is said to be fully FD?
55. Mention the different Armstrong’s inference rules.
56. Why Armstrong’s inference rules are said to be sound and complete?
57. Define denormalisation.
58. Define Transaction.
59. Mention the ACID properties.
60. Define schedule.
61. Is DBMS usage always advisable or some times we may depend on file base systems?
Comment on the statement by describing the situation where DBMS is not a better
option & file base systems is better.
62. Describe 3-level architecture of DBMS with details of languages associated at different
levels plus the level of data independence.
63. How logical architecture of DBMS differs from physical architecture?
64. Create an E R diagram and relational schema to hold information about the situation in
many institutions affiliated to some University, many teachers of different disciplines
are teaching to many students enrolled in many courses offered by the university to the
students through the institutions. Use concept of keys, aggregation, generalisation,
cardinality etc. in a proper way.
65. What is the utility of relational algebra & relational calculus? Name some software’s
based on these concepts?
66. Comment on the statement “Set theory has contributed a lot to RDBMS” support it with
the help of suitable examples.
67. “Redundancy of data is many times beneficial” Justify the statement, also describe the
situation when redundancy will mess up the current data base status, at that instance of
time what actions you will prefer to take.
68. In Oracle we are having variety of versions Oracle 8, Oracle 9, etc, what does the
associated number mean. Again we are having Oracle 8i, Oracle 9i etc, what does this
“i” mean.
69. Describe the various file organization techniques? How a binary tree is different from
B-tree and B+ tree? Under which situation we need to use B+ tree or B tree.
Prove “Any relation which is in BCNF is in 3NF,but converse is not true”

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur


DBMS LAB MANUAL

70. Which functional dependencies are to be removed to achieve respective normal form?
Discuss all the normal forms up to 4NF?
71. What is the mathematical basis of SQL? The SQL statement: select * from student will
perform like projection or selection? Give details in support of your answer.

AI & ML DEPARTMENT By-Kavya R,SSIT Clg,Tumkur

You might also like