DBMS Lab Manual - 20CS34P
DBMS Lab Manual - 20CS34P
Examiner 1: ………………………..
Examiner 2: ...………………………
CHAPTER – 1
1|Page
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
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.
column_nameN datatype);
2|Page
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
Character Datatypes
PostgreSQL supports character data types for storing text values. PostgreSQL builds character data
types off of the same internal structures. PostgreSQL offers three character data types: CHAR(n),
VARCHAR(n), and TEXT.
Name Description
Text Use can use this data type to declare a variable with unlimited length
Numeric Datatypes
PostgreSQL supports two distinct types of numbers:
Integers
Floating-point numbers
Store
Name Range
size
smallint 2 bytes -32768 to +32767
integer 4 bytes -2147483648 to +2147483647
bigint 8 bytes -9223372036854775808 to 9223372036854775807
If you declared it as decimal datatype ranges from 131072 digits before the
decimal variable
decimal point to 16383 digits after the decimal point
If you declare it as the number, you can include number up to 131072 digits
numeric variable
before the decimal point to 16383 digits after the decimal point
real 4 bytes 6 decimal digits precision
double 8 bytes 15 decimal digits precision
3|Page
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
Character strings not allow zero octets and also disallows any other octet values and sequences
which are invalid as per the database’s character set encoding rules.
Name Storage size Description
Byte 1 to 4 bytes plus the size of the binary string Variable-length binary string
1) Primary key:
This constraint defines a column or combination of columns which uniquelyidentifies
each row in the table.
Syntax to define a Primary key at column level:
column_name2,..) 4|Page
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
column_name1, column_name2 are the names of the columns which define the
primary key.
The syntax within the bracket i.e. [CONSTRAINT constraint_name] is optional.
5|Page
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
referenced_table_name(column_name)
referenced_table_name(column_name);
4) Unique Key:
This constraint ensures that a column or a group of columns in each row have adistinct
value. A column(s) can have a null value but the values cannot be duplicated.
Syntax to define a Unique key at column level:
[CONSTRAINT constraint_name] UNIQUE
5) Check Constraint:
This constraint defines a business rule on a column. All the rows must satisfy thisrule.
The constraint can be applied for a single column or a group of columns.
Syntax to define a Check constraint:
[CONSTRAINT constraint_name] CHECK (condition)
6|Page
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
For Example: To add a column "experience" to the employee table, the query would be like
For Example: To drop the column "location" from the employee table, the query would be like
For Example: To modify the column salary in the employee table, the query would be like
column_name;
7|Page
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
SELECT column_name(s)
FROM table_name
WHERE column_name operator value;
8|Page
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
GROUP BY column_name(s);
9|Page
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
20CS34P
values of expr.
MAX([DISTINCT|ALL]expr) Returns maximum value of expr
MIN([DISTINCT|ALL]expr) Returns minimum value of expr
SUM([DISTINCT|ALL]n) Returns sum of values of n
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
GROUP BY column_name(s)
HAVING condition;
The second form specifies both the column names and the values to be inserted:
10 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value;
Commit command
commit;
Rollback command
This command restores the database to last committed state. It is also use with
savepoint command to jump to a savepoint in a transaction.
rollback to savepoint_name;
Savepoint command
savepoint command is used to temporarily save a transaction so that you can rollback to that
point whenever necessary.
savepoint savepoint_name;
9|Page
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
System : creating session, table etc are all types of system privilege.
Object : any command or query to work on tables comes under object privilege.
PostgreSQL allows the users to extend the database functionality with the help of user-defined
functions and stored procedures through various procedural language elements, which are often
referred to as stored procedures.
10 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
The store procedures define functions for creating triggers or custom aggregate functions. In
addition, stored procedures also add many procedural features e.g., control structures and complex
calculation. These allow you to develop custom functions much easier and more effective.
It is possible to call a Procedural code block using the DO command without defining a function
or stored procedure.
PostgreSQL categorizes the procedural languages into two main groups:
1. Safe languages can be used by any users. SQL and PL/pgSQL are safe languages.
2. Sand-boxed languages are only used by superusers because sand-boxed languages provide the
capability to bypass security and allow access to external sources. C is an example of a
sandboxed language.
By default, PostgreSQL supports three procedural languages: SQL, PL/pgSQL, and C. You can
also load other procedural languages e.g., Perl, Python, and TCL into PostgreSQL using
extensions.
Advantages of using PostgreSQL stored procedures:
The stored procedures bring many advantages as follows:
Reduce the number of round trips between applications and database servers. All SQL
statements are wrapped inside a function stored in the PostgreSQL database server so the
application only has to issue a function call to get the result back instead of sending multiple
SQL statements and wait for the result between each call.
Increase application performance because the user-defined functions and stored procedures are
pre-compiled and stored in the PostgreSQL database server.
Reusable in many applications. Once you develop a function, you can reuse it in any
applications.
Disadvantages of using PostgreSQL stored procedures:
Besides the advantages of using stored procedures, there are some caveats:
Slowness in software development because stored procedure programming requires
specialized skills that many developers do not possess.
Difficult to manage versions and hard to debug.
May not be portable to other database management systems e.g., MySQL or Microsoft SQL
Server.
11 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
12 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
o Views in SQL are virtual table. A view also contains rows and columns.
o To create the view, we can select the fields from one or more tables present in the database.
o A view can either have specific rows based on certain condition or all the rows of a table.
The reasons for using Views”
o Views restrict access to the data because the view can display selective columns and rows
from the table.
o Views provide groups of users with access to data according to their particular permissions.
o Views can be used to retrieve data from several tables, providing data independence for
users.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
13 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
CHAPTER – 2
LIBRARY DATABASE
ER-Diagram:
14 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
SCHEMA:
15 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
Table Creation:
PUBLISHER
Table created.
BOOK
Table created.
BOOK_AUTHORS
Table created.
LIBRARY_BRANCH
Table created.
BOOK_COPIES
Table created.
16 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
BOOK_LENDING
Table created.
PUBLISHER
BOOK
BOOK_AUTHORS
17 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
LIBRARY_BRANCH
BOOK_COPIES
BOOK_LENDING
4 rows selected.
18 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
BOOK_ID AUTHOR_NAME
1111 SOMMERVILLE
2222 NAVATHE
3333 HENRY GRAY
4444 THOMAS
4 rows selected.
3 rows selected.
1111 11 5
3333 22 6
4444 33 10
2222 11 12
4444 55 3
5 rows selected.
7 rows selected.
19 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
5 rows selected.
Queries:
1) Retrieve details of all books in the library – id, title, name of publisher, authors, number of
copies in each branch, etc.
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-JAN-2017' AND '30-JUN-2017'
GROUP BY CARD_NO
HAVING COUNT(*) > 3;
CARD_NO
20 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
3) Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
1 row deleted.
1111 11 5
4444 33 10
2222 11 12
4444 55 3
4) Partition the BOOK table based on year of publication. Demonstrate its working with a
simple query.
21 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
5) Create a view of all books and its number of copies that are currently available in the
Library.
View created.
1111 SE 5
3333 ANOTOMY 6
4444 ENCYCLOPEDIA 10
2222 DBMS 12
4444 ENCYCLOPEDIA 3
22 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
CHAPTER – 3
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
1. Count the customers with grades above Bangalore’s average.
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don’t have customers in their cities
(Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest order of a
day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders
must also be deleted.
ER-Diagram:
23 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
SCHEMA:
24 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
Table Creation:
SALESMAN
Table created.
CUSTOMER
Table created.
ORDERS
Table created.
25 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
SELECT COUNT(CUSTOMER_ID)
FROM CUSTOMER
WHERE GRADE>(SELECT AVG(GRADE)
FROM CUSTOMER
WHERE CITY LIKE '%BENGALURU');
COUNT(CUSTOMER_ID)
26 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
2. Find the name and numbers of all salesmen who had more than one customer.
NAME COUNT(CUSTOMER_ID)
ASHWIN 2
RAJ 2
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));
NAME
ASHWIN
BINDU
LAVANYA
RAJ
ROHIT
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.
2000 300000
27 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders
must also be deleted.
1 row deleted.
11 INFOSYS BENGALURU 5
22 TCS BENGALURU 4 2000
33 WIPRO MYSORE 7
44 TCS MYSORE 6 2000
55 ORACLE TUMKUR 3 3000
28 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
CHAPTER – 4
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
1. List the titles of all movies directed by ‘Hitchcock’.
2. Find the movie names where one or more actors acted in two or more movies.
3. List all actors who acted in a movie before 2000 and also in a movie after
2015 (use JOIN operation).
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.
5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.
ER-Diagram:
29 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
SCHEMA:
30 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
Table Creation:
ACTOR
Table created.
DIRECTOR
Table created.
MOVIES
Table created.
MOVIE_CAST
Table created.
RATING
Table created.
31 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
Description of Schema:
32 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
ACT_ID ACT_NAME AC
1111 3
2222 4
3333 3
5555 4
4444 5
33 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
SELECT MOV_TITLE
FROM MOVIES M, DIRECTOR D
WHERE D.DIR_ID=M.DIR_ID AND
DIR_NAME='HITCHCOCK';
MOV_TITLE
NOTORIOUS
REAR WINDOW
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);
MOV_TITLE
MALE
MANASARE
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
A.ACT_ID=C.ACT_ID
4. 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);
ACT_NAME
DHIGANTH
34 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
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.
MOV_TITLE REV_STARS
EEGA 4
LASTWORLD 3
MALE 3
MANASARE 4
PARAMATHMA 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.
MOV_ID REV_STARS
1111 5
2222 4
3333 3
5555 4
4444 5
35 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
CHAPTER - 5
COLLEGE DATABASE
ER-Diagram:
36 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
SCHEMA:
37 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
Table Creation:
STUDENT
Table created.
SEMSEC
Table created.
CLASS
Table created.
SUBJECT
Table created.
IAMARKS
TEST3 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.
STUDENT:
SEMSEC:
SSID SEM S
-
5A 5A
3B 3B
7A 7A
2C 2C
4B 4B
4c 4c
CLASS:
USN SSID
1cg15cs001 5A
39 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
1cg15cs002 5A
1cg16me063 3B
1cg14ec055 7A
1cg15ee065 3B
1cg15ee065 4c
1cg15cs002 4c
SUBJECT:
15cs53 dbms 5 4
15cs33 ds 3 4
15cs34 co 3 4
15csl58 dba 52
10cs71 oomd 7 4
IAMARKS:
1cg15cs001 15cs53 5A 18 19 15 19
1cg15cs002 15cs53 5A 15 16 14 16
1cg16me063 15cs33 3B 10 15 16 16
1cg14ec055 10cs71 7A 18 20 21 21
1cg15ee065 15cs33 3B 16 20 17 19
1cg15ee065 15cs53 4c 19 20 18 20
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;
40 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
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;
SEM S G COUNT(*)
- -
3B M 2
4c F 1
4c M 1
5A F 1
5A M 1
7A F 1
View created.
SUBCODE TEST1
15cs33 16
15cs53 19
4. Calculate the FinalIA (average of best two test marks) and update the corresponding table
for all students.
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
2 AVG;
3 END;
1cg15cs001 15cs53 5A 18 19 15 19
1cg15cs002 15cs53 5A 15 16 14 16
1cg16me063 15cs33 3B 10 15 16 16
1cg14ec055 10cs71 7A 18 20 21 21
1cg15ee065 15cs33 3B 16 20 17 19
1cg15ee065 15cs53 4c 19 20 18 20
6 rows selected.
42 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
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
43 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
CHAPTER – 6
COMPANY DATABASE
ER-Diagram:
44 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
SCHEMA:
45 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
Table Creation:
DEPARTMENT
EMPLOYEE
Table altered.
DLOCATION
PROJECT
WORKS_ON
CREATE TABLE WORKS_ON(
HOURS NUMBER (2),
SSN REFERENCES EMPLOYEE (SSN),
PNO REFERENCES PROJECT(PNO),
PRIMARY KEY (SSN, PNO));
46 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
DEPARTMENT
5 rows selected.
EMPLOYEE
10 rows selected.
DLOCATION
DNO DLOC
1 MYSORE
1 TUMKUR
2 BENGALURU
47 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
3 GUBBI
4 DELHI
5 BENGALURU
6 rows selected.
PROJECT
5 rows selected.
WORKS_ON
666666 333 4
666666 111 2
111111 222 3
555555 222 2
333333 111 4
444444 111 6
222222 111 2
8 rows selected.
48 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
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.
PNO
111
333
444
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10
percent raise.
10 rows selected.
49 | P a g e
Database System Concepts and PL/SQL- 20CS34P III Sem CSE
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;
SUM(SALARY) MAX(SALARY) MIN(SALARY) AVG(SALARY)
- - -
-
-
-
-
-
-
-
-
-
-
-
6
4
0
0
0
0
440000 200000 320000
4. Retrieve the name of each employee who works on all the projects controlled by
department number 5 (use NOT EXISTS operator).
SCOTT
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 ;
DNO COUNT(SSN)
3 4
50 | P a g e