DBMS Lab Manual
DBMS Lab Manual
Lab Manual
1
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
Branch : CSE
Date of Issue :
2
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
INDEX
3
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
LAB OBJECTIVE
Introduce ER data model, database design and normalization
Learn SQL basics for data definition and data manipulation
LAB OUTCOMES
Upon successful completion of this Lab, students will be able to:
CO1: Design database schema for a given application and apply normalization.
CO2: Acquire skills in using SQL Commands for data Definition and data manipulation.
CO3: Develop solutions for database applications using procedures, cursors and triggers.
4
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
Students are required to carry their lab observation book and record book with completed
experiments while entering the lab
Students must use the equipment with care
Students are not allowed to use their cell phones/pen drives/CDs in labs
Students need to maintain proper dress code along with ID Card
Students are supposed to occupy the computers allotted to them and are not supposed to talk or
make noise in the lab
Students, after completion of each experiment they need to be updated in observation notes and
same to be updated in the record
Lab records need to be submitted after completion of experiment and get it corrected with the
concerned lab faculty
If a student is absent for any lab, they need to be completed the same experiment in the free time
before attending next lab.
5
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
Step2: Students have to listen and understand the experiment explained by the faculty and note down
the important points in the observation book.
Step4: Analyze and Develop/implement the logic of the program by the student in respect platform
Step5: After approval of logic of the experiment by the faculty then the experiment has to executed on
the system.
Step6: After successful execution the results are to be shown to the faculty and noted the same in the
observation book.
Step7: Students need to attend the Viva-Voce on that experiment and write the same in the observation
book.
Step8: Update the completed experiment in the record and submit to the concerned faculty in-charge.
6
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
Record 3 Marks
Procedure 20 Marks
Viva-Voce 10 Marks
7
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
LIST OF EXPERIMENTS
2 ER To Relational Model
3 Normalization
6 Querying using ANY, ALL, IN, EXISTS, NOT EXISTS, UNION, INTERSECT
Querying using aggregate functions, Group by, Having and Creation and dropping
7
Views.
9 Procedures
Cursors
10
ADDITIONAL EXPERIMENTS
8
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
AIM: Analyzing a system to implement the concepts of E-R Model through Bus Management System.
CONCEPTS OF ER MODEL:
Entities and its types
Cardinalities for each relationship.
Identify strong entities and weak entities (if any).
Indicate the type of relationships (total/partial).
Try to incorporate generalization, aggregation, specialization etc. wherever required.
Note: The student is required to submit a document by drawing the E-R' Diagram to the lab teacher.
RECOMMENDED HARDWARE / SOFTWARE REQUIREMENTS:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster
processor with at least 1GB RAM and 500 MB free disk space.
MySQL 5.6.1
PRE-REQUISITES: Student must know the concepts of ER MODEL.
PROCEDURE:
After identifying the system, students will
1. Identify the entities
2. Identify the attributes for the taken entities.
3. Establish the relationship between the entities.
4. Identify the cardinalities and types of relationships for the mentioned relationships.
5. Identify the class hierarchy (Generalization/ Specialization) and aggregation if any.
6. Show all the above representations in one diagram to generate an E-R Diagram.
The following are the entities
i. Bus
ii. Reservation
iii. Ticket
iv. Passenger
v. Cancellation
9
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
Bus: (Entity)
Source Destination
BUS
Reservation: (Entity)
Journey date
Bus No.
PNR NO Address
RESERVATION
10
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
Ticket: (Entity)
TICKET
Passenger: (Entity)
Age Sex
Contact No
PNR NO
PASSENGER
Cancellation: (Entity)
CANCELLATION 11
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
RESULT: Student gains the ability to describe the data requirements for a new information system
and implement them through an E-R Model.
VIVA- VOCE
1. Distinguish database with database management system?
2. Define entity and how it is different from an attribute?
3. Explain Participation Constraints?
4. Differentiate generalization and specialization?
5. Describe degree of cardinality?
12
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
AIM: To Convert the concepts of ER model (Graphical Notation) into Relational Model (Table)
database.
Hints:
Represent attributes as columns in tables or as tables based on the requirement.
Different types of attributes (Composite, Multi-valued, and Derived) have different way of
representation.
Represent all the entities (Strong, Weak) in tabular fashion.
Represent relationships in a tabular fashion. There are different ways of representing,
relationships as tables based on the cardinality.
RECOMMENDED HARDWARE/ SOFTWARE REQUIREMENTS:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster
processor with at least 1GB RAM and 500 MB free disk space.
MySQL 5.6.1
Multi-Valued Attributes:
13
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
1:1 Relationships
1:N Relationships
Persons( personid , name, lastname, email ), House ( houseid , num , address, personid)
N:N Relationships
14
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
RESULT: The student gains the knowledge on converting the E-R Model concepts in to a
relational database.
VIVA-VOCE:
1. Show the representation of data in relational model?
2. State the use of CASCADE constraint?
3. Explain Query optimization?
4. Differentiate relation schema from relation?
5. Define view and how it is related to data independence?
15
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
EXPERIMENT 3: NORMALIZATION
AIM: Implementing the normalization techniques up to 3NF to avoid data redundancy in the database
design.
RECOMMENDED HARDWARE/ SOFTWARE REQUIREMENTS:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster
processor with at least 1GB RAM and 500 MB free disk space.
MySQL 5.6.1
PRE-REQUISITES: Student must know about different types of normal forms.
NORMALIZATION: Database normalization is a technique for designing relational database tables
to minimize duplication of information and, in so doing, to safeguard the database against certain types
of logical or structural problems, namely data anomalies. A table that is sufficiently normalized is less
vulnerable to problems of anomalies.
1NF: A Relation scheme is said to be in 1NF if the attribute values in the relation are atomic. Muti –
valued attributes are not permitted.
2NF: A Relation scheme is said to be in 2NF, if every non-key attribute is fully functionally dependent
on primary Key.
3NF: A Relation scheme is said to be in 3NF, if it does not have transitivity dependencies. A Relation
is said to be 3NF if every determinant is a key for each & every functional dependency.
16
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
17
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
18
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
VIVA-VOCE:
1. Define Normalization?
2. Outline Fully Functional dependency?
3. Define partial dependency?
4. Recall join dependency?
5. Define multi valued dependency?
19
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
ALTER
20
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
DROP
TRUNCATE
RESULT: The student is able to work in the MYSQL environment and gains the knowledge
on DDL Commands.
VIVA VOCE:
21
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
INSERT
SELECT
22
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
UPDATE
DELETE
RESULT: The Student gains the knowledge on DML Commands like Insert, Update, Delete
and Select.
VIVA-VOCE:
23
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
EXPERIMENT 6: QUERYING
AIM: Performing querying using ANY, ALL, IN, Exists, NOT EXISTS, UNION, INTERSECT,
Constraints etc.
RECOMMENDED HARDWARE/ SOFTWARE REQUIREMENTS:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster
processor with at least 64MB RAM and 100 MB free disk space.
MySQL 5.6.1
PRE-REQUISITES: Student must know about the RDBMS- Basic forms of SQL
OPERATORS:
1.UNION:
UNION is used to combine results of two or more SELECT statements.it eliminates the duplicate
rows from result set.
2.INTERSECT:
Intersect operation is used to combine two select statements, but it only returns the records which are
COMMON from both SELECT statements
3.MINUS/EXCEPT:
The minus operation combines results of two SELECT statements and return only those in the final
result, which belongs to the first set of the result
4.IN:
The in-operator helps to connect inner query to outer query and also allows to test whether a value is
in a given set of elements.
5.NOT-IN:
The not-in operator is used as opposite of IN operator
6.EXISTS:
The exists operator is used to search for the presence of a row in a specified table that meets a certain
criterion. It allows us to test whether a set is non empty or not.
7.NOT EXISTS:
It is used opposite to EXISTS OPERATOR.
8.ANY:
It compares a value to any applicable value in the list As per the condition
9.ALL:
ALL operator is used to select all tuples of SELECT statements.
24
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
UNION
INTERSECT
25
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
EXCEPT
IN-OPERATOR
NOT-IN OPERATOR
26
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
27
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
ANY
ALL
RESULT: The Student is able execute the Queries by using the above
operators.
VIVA- VOCE
28
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
AIM: Performing the querying using Aggregate functions (COUNT, SUM, AVG, and MAX and MIN),
GROUP BY, HAVING and Creation and dropping of Views.
29
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
AGGREGATE FUNCTIONS:
AVG:
mysql> create table Customer
-> (
-> cust_id integer(20),
-> fname varchar(30),
-> lname varchar(30),
-> age int(10),
-> country varchar(30)
-> );
mysql> INSERT INTO Customer VALUES('1','John','Doe','31','USA');
mysql> INSERT INTO Customer VALUES('2','Robinson','Luna','22','USA');
mysql> INSERT INTO Customer VALUES('3','David','Robinson','22','UK');
mysql> INSERT INTO Customer VALUES('4','John','Ronald','25','UK');
mysql> INSERT INTO Customer VALUES('5','Betty','Doe','28','UAE');
30
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
SUM
create table Orders
-> (
-> order_id integer(20),
-> item varchar(30),
-> amount integer(40),
-> cust_id integer(40)
-> );
mysql> INSERT INTO Orders VALUES('1','Keyboard','400','4');
mysql> INSERT INTO Orders VALUES('2','Mouse','300','4');
mysql> INSERT INTO Orders VALUES('3','Monitor','12000','3');
mysql> INSERT INTO Orders VALUES('4','Keyboard','400','1');
mysql> INSERT INTO Orders VALUES('5','Mousepad','250','2');
Count
mysql> create table Customer
-> (
-> cust_id integer(20),
-> fname varchar(30),
-> lname varchar(30),
-> age int(10),
-> country varchar(30)
-> );
31
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
MAX
create table Orders
-> (
-> order_id integer(20),
-> item varchar(30),
-> amount integer(40),
-> cust_id integer(40)
-> );
mysql> INSERT INTO Orders VALUES('1','Keyboard','400','4');
mysql> INSERT INTO Orders VALUES('2','Mouse','300','4');
mysql> INSERT INTO Orders VALUES('3','Monitor','12000','3');
mysql> INSERT INTO Orders VALUES('4','Keyboard','400','1');
mysql> INSERT INTO Orders VALUES('5','Mousepad','250','2');
32
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
MIN
create table Orders
-> (
-> order_id integer(20),
-> item varchar(30),
-> amount integer(40),
-> cust_id integer(40)
-> );
mysql> INSERT INTO Orders VALUES('1','Keyboard','400','4');
mysql> INSERT INTO Orders VALUES('2','Mouse','300','4');
mysql> INSERT INTO Orders VALUES('3','Monitor','12000','3');
mysql> INSERT INTO Orders VALUES('4','Keyboard','400','1');
mysql> INSERT INTO Orders VALUES('5','Mousepad','250','2');
GROUP BY
33
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
ORDER BY
HAVING
35
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
VIEWS
CREATE VIEW
36
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
UPDATING VIEW
DELETING VIEW
37
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
EXPERIMENT 8: TRIGGERS
38
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
3. Create an Audit table for the bus to track the actions on the table using
Triggers concept. ( Schema : Bus_Audit1(ID, Source, Changedon, Action))
CREATE TABLE BUS_AUDIT1(ID INT NOT NULL AUTO_INCREMENT, SOURCE
VARCHAR(10) NOT NULL, CHANGEDON DATETIME DEFAULT NULL, ACTION
VARCHAR(10) DEFAULT NULL, PRIMARY KEY(ID));
39
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
BEGIN
SET action='Insert',
source=NEW.source,
changedon=NOW();
END$$
40
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
41
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
VIVA- VOCE
1. DefineTRIGGER?
2. List the types of triggers?
3. List the trigger timings?
4. Is it possible to create a trigger on views?
5. Outline row and statement trigger?
42
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
43
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
Ex2: Executing the procedure to show the declaration of local variables in a stored
procedure.
Local variables are declared within stored procedures and are only valid between Begin and END.
Block where they are declared. Local variables can have any SQL data type.
CREATE PROCEDURE SAMPLE2()
BEGIN
DECLARE X INT(3);
SET X=10;
SELECT X;
END$$
OUTPUT:
CALL SAMPLE2()$$
44
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
AIM: To declare MySQL cursor in stored procedure to iterate through a result set returned by a
SELECT statement.
RECOMMENDED HARDWARE/ SOFTWARE REQUIREMENTS :
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster
processor with at least 64MB RAM and 100 MB free disk space.
MySQL 5.6.1
PRE-REQUISITES: Student must know about the Relational SQL-Cursors
CURSOR: To handle a result set inside a stored procedure, you use a cursor. A cursor allows you
to iterate a set of rows returned by a query and process each row accordingly.
MySQL cursor is read-only, non-scrollable and asensitive.
Read-only: you cannot update data in the underlying table through the cursor.
Non-scrollable: you can only fetch rows in the order determined by the SELECT statement.
You cannot fetch rows in the reversed order. In addition, you cannot skip rows or jump to a
specific row in the result set.
Asensitive: there are two kinds of cursors: asensitive cursor and insensitive cursor. An
asensitive cursor points to the actual data, whereas an insensitive cursor uses a temporary copy
of the data. An asensitive cursor performs faster than an insensitive cursor because it does not
have to make a temporary copy of data. However, any change that made to the data from other
connections will affect the data that is being used by an asensitive cursor, therefore, it is safer if
you do not update the data that is being used by an asensitive cursor. MySQL cursor is
asensitive.
Working with MySQL cursor:
Step1: Declare a cursor by using the DECLARE statement:
The cursor declaration must be after any variable declaration. If you declare a cursor before variables
declaration, MySQL will issue an error. A cursor must always be associated with a SELECT statement.
46
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
Step2: Open the cursor by using the OPEN statement. The OPEN statement initializes the result set
for the cursor; therefore, you must call the OPEN statement before fetching rows from the result set.
1 OPEN cursor_name;
Step:3: FETCH statement to retrieve the next row pointed by the cursor and move the cursor to the
next row in the result set.
After that, you can check to see if there is any row available before fetching it.Declare a NOT
FOUND handler to handle the situation when the cursor could not find any row.
Step:4: CLOSE statement to deactivate the cursor and release the memory associated with it as
follows:
1 CLOSE cursor_name;
Example: Developing a stored procedure that builds an email list of all employees in
the employees table in the sample database.
DELIMITER $$
CREATE PROCEDURE build_email_list (INOUT email_list varchar(4000))
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE v_email varchar(100) DEFAULT "";
-- declare cursor for employee email
DEClARE email_cursor CURSOR FOR
SELECT email FROM employee;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = 1;
47
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
OPEN email_cursor;
get_email: LOOP
FETCH email_cursor INTO v_email;
IF v_finished = 1 THEN
LEAVE get_email;
END IF;
-- build email list
SET email_list = CONCAT(v_email,";",email_list);
END LOOP get_email;
CLOSE email_cursor;
END$$
You can test the build_email_list stored procedure using the following script:
SET @email_list = ""$$
CALL build_email_list(@email_list)$$
48
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
SELECT @email_list$$
VIVA VOCE:
1. Define a cursor?
2. List the types of cursor?
3. State the use of parameterized cursor?
4. State the use of cursor variable?
5. Define normal cursor?
49
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
ADDITIONAL EXPERIMENTS
1. Design and implement queries on Tables (Emp,Dept)
A) AIM: To create the following relations using appropriate SQL statements:
Define a constraint on EMP relation that will ensure that every employee earns
atmost Rs.10000/-
Define constraint on EMP relation such that deptno will be foreign key to DEPT
relation
Define dept’s relation so that every department is guaranteed to have some name.
SQL>CREATE TABLE DEPT (DEPTNO NUMBER (2) PRIMARY KEY, DNAME VARCHAR2 (10)
NOT NULL, LOC VARCHAR2 (8));
Table created.
Table created
SQL>CREATE TABLE SALGRADE (GRADE NUMBER (1), LOSAL NUMBER (4), HISAL
NUMBER (4));
Table created.
50
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
VALUES(&EMPNO,’&ENAME’,’&JOB’,&MGR,’&HIREDATE’,&SAL,&COMM,&DEPT
NO);
Enter value for empno: 7499
Enter value for ename: ALLEN
Enter value for job: SALESMAN
Enter value for mgr: 7698
Enter value for hiredate: 20-FEB-81
Enter value for sal: 1600
Enter value for comm: 300
Enter value for deptno: 30
old 1: INSERT INTO
EMP(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
new 1: INSERT INTO EMP(7499,’ALLEN’,’SALESMAN’,7698,’20-FEB-81’,1600,300,30)
1 row created.
3) SQL> INSERT INTO SALGRADE (GRADE,LOSAL,HISAL)
VALUES(&GRADE,&LOSAL,&HISAL);
Enter value for grade: 5
Enter value for losal: 3001
51
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
52
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
53
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
54
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
55
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
8) List max sal, min sal and average sal of depts. 10,30.
10) Display the jobs where the minimum salary is greater than or equal to 3000.
56
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
LPAD (ENAM
---------
****SMITH
****ALLEN
*****WARD
****JONES
14 rows selected.
RPAD (ENAM
57
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
---------
SMITH****
ALLEN****
FORD*****
MILLER***
14 rows selected.
4) Left trim of character ‘s’ from employee names of department number 20.
SQL> SELECT LTRIM(ENAME,'S') FROM EMP WHERE DEPTNO=20;
LTRIM(ENAM
------------------
MITH
JONES
5) Right trim of character ‘s’ from employee names of department number 20.
SQL> SELECT RTRIM(ENAME,'S') FROM EMP WHERE DEPTNO=20;
RTRIM(ENAM
----------
SMITH
JONE
SCOTT
ADAM
6) List employee names with all capital letters, with all small letters and with first
letter only as capital of department number 10.
7) List employee names with length of the name sorted on length for department
number 30.
SQL>SELECT ENAME, LENGTH (ENAME) FROM EMP WHERE DEPTNO=30 ORDER BY
LENGTH(ENAME);
ENAME LENGTH(ENAME)
---------- -------------------------
WARD 4
ALLEN 5
BLAKE 5
JAMES 5
MARTIN 6
TURNER 6
6 rows selected.
JOB
----
CLER
SALE
59
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
MANA
ANAL
PRES
5 rows selected.
ENAME INSTR(ENAME,'S')
---------- ----------------
SMITH 1
JONES 5
SCOTT 1
LOWEST
----------
-2
HIGHEST
------------
60
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
89
TRUNC(567.231656,3)
ENAME EXP
---------- ----------
SMITH 28
ALLEN 28
WARD 28
2)Find the first ‘SUN’day of employees after join in the organization of EMP table.
SQL>SELECT NEXT_DAY (HIREDATE,'SUN') AS HOLIDAY FROM EMP;
HIREDATE HOLIDAY
61
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
-------------- ---------------
17-DEC-80 21-DEC-80
20-FEB-81 22-FEB-81
22-FEB-81 01-MAR-81
02-APR-81 05-APR-81
HIREDATE REVIEWDAT
------------- ---------------
17-DEC-80 17-DEC-81
02-APR-81 02-APR-82
4)Display last day of joining month of employees of deptno ‘10’ from EMP table.
SQL> SELECT HIREDATE,LAST_DAY(HIREDATE) AS LASTDAY FROM EMP WHERE
DEPTNO=10;
HIREDATE LASTDAY
------------- -------------
09-JUN-81 30-JUN-81
17-NOV-81 30-NOV-81
23-JAN-82 31-JAN-82
62
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
ENAME HIREDATE
----------------------------
SMITH 17/12/80
JONES 02/04/81
SCOTT 19/04/87
ADAMS 23/05/87
FORD 03/12/81
2) Display empno, employee name, job, salary of the employees. Show the salary
with thousand separators.
SQL>SELECT EMPNO, ENAME,JOB,TO_CHAR(SAL,’$9,999’)AS SALARY FROM EMP;
EMPNO ENAME JOB SALARY
---------- ---------- --------- ----------------
7369 SMITH CLERK $800
7499 ALLEN SALESMAN $1,600
ENAME HIREDATE
---------- ----------------
JONES APR 81
MARTIN SEP 81
Solution:
Entity-Relationship Diagram
N
Library_Branch
64
Department of Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem
(2021-22)
Schema Diagram
66
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
TABLE CREATION
CREATE TABLE PUBLISHER
(NAME VARCHAR2 (20)
PRIMARY KEY, PHONE
INTEGER,
ADDRESS VARCHAR2 (20));
CREATE TABLELIBRARY_BRANCH
(BRANCH_ID INTEGER PRIMARY
KEY, BRANCH_NAME VARCHAR2
(50),
ADDRESS VARCHAR2 (50));
CREATE TABLE
BOOK_COPIES
(NO_OF_COPIES
INTEGER,
BOOK_ID REFERENCES BOOK (BOOK_ID) ON DELETE CASCADE,
BRANCH_ID REFERENCES LIBRARY_BRANCH (BRANCH_ID)
ON DELETE CASCADE,
PRIMARY KEY (BOOK_ID, BRANCH_ID));
66
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
CREATE TABLE
BOOK_LENDING
(DATE_OUT DATE,
DUE_DATE DATE,
BOOK_ID REFERENCES BOOK (BOOK_ID) ON DELETE CASCADE,
BRANCH_ID REFERENCES LIBRARY_BRANCH (BRANCH_ID)
ON DELETE CASCADE,
CARD_NO REFERENCES CARD (CARD_NO) ON DELETE
CASCADE, PRIMARY KEY (BOOK_ID, BRANCH_ID,
CARD_NO));
TABLE DESCRIPTIONS
DESC PUBLISHER;
DESC BOOK;
67
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
DESC BOOK_AUTHORS;
DESC LIBRARY_BRANCH;
DESC BOOK_COPIES;
DESC CARD;
DESC BOOK_LENDING;
68
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
69
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
INSERT INTO CARD VALUES (100); INSERT INTO CARD VALUES (101);
INSERT INTO CARD VALUES (102); INSERT INTO CARD VALUES (103);
INSERT INTO CARD VALUES (104);
INSERT INTO BOOK_LENDING VALUES (‘01-JAN-17‘,‘01-JUN-17‘, 1, 10, 101);
INSERT INTO BOOK_LENDING VALUES (‘11-JAN-17‘,‘11-MAR-17‘, 3, 14, 101);
INSERT INTO BOOK_LENDING VALUES (‘21-FEB-17‘,‘21-APR-17‘, 2, 13, 101);
INSERT INTO BOOK_LENDING VALUES (‘15-MAR-17‘,‘15-JUL-17‘, 4, 11, 101);
INSERT INTO BOOK_LENDING VALUES (‗12-APR-17‘,‘12-MAY-17‘,
1, 11, 104); SELECT * FROM PUBLISHER;
70
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
QUERIES:
1. Retrieve details of all books in the library – id, title, name of publisher, authors,
number of copies in each branch, etc.
71
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
2. Get the particulars of borrowers who have borrowed more than books,
but from Jan 2017 to Jun 2017.
B. Delete a book in BOOK table. Update the contents of other tables to reflect this
data manipulation operation.
CREATE VIEW
V_PUBLICATION AS SELECT
PUB_YEAR
FROM BOOK;
72
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
D. Create a view of all books and its number of copies that are currently
available in the Library.
73
Computer Science & Engineering Data Base Management Systems Lab Manual – II Year II Sem (2023-2024)
Database Management Systems, Raghurama Krishnan, Johannes Gehrke, Tata Mc Graw Hill,
T1
3rd Edition
Database Systems design, Implementation, and Management, Peter Rob & Carlos Coronel 7th
R1
Edition.
74