DBMS Lab Manual 2019 Course
DBMS Lab Manual 2019 Course
CODE: 214456
1
Course Objectives:
5. To learn and understand various Database Architectures and its use for
application development.
Course Outcomes:
2
214456: DATABASE MANAGEMENT SYSTEM LAB
Teaching Scheme: Practical: 4 Hours/Week Credits: 02
Examination Scheme: Term Work: 25 Marks
Practical: 25 Marks
7 Group B: MySQL
Perform following SQL queries on the database created in assignment 1.
• Implementation of relational operators in SQL
• Boolean operators and pattern matching
• Arithmetic operations and built in functions
• Group functions
• Processing Date and Time functions
• Complex queries and set operators
8 Group B: MySQL
Execute DDL/DML statements which demonstrate the use of views. Try
to update the base table using its corresponding view. Also consider
restrictions on updatable views and perform view creation from multiple
tables.
3
9 Group C: PL/SQL
Write and execute PL/SQL stored procedure and function to perform a
suitable task on the database. Demonstrate its use.
10 Group C: PL/SQL
Write and execute suitable database triggers .Consider row level and
statement level triggers.
11
Group C: PL/SQL
Write a PL/SQL block to implement all types of cursor.
12 Group D: Relational Database Design
Design and case study of any organization (back end only), Project
Proposal and High Level SRS
To prepare for your project, do the following:
1. Form teams of around 3 to 4 people
2. Create a requirements document with the following information;
1. Give a one or two paragraph description of your goals for the
topic(s).
2. List all what all types of users will be accessing your application
(e.g., for moodle, the types are teachers, students, teaching
assistants, and a few more types).
3. List the various functionalities that your application will support. Explain
each in about a paragraph worth of detail.
4. List the hardware and software requirements at the backend and at
the front end.
5. Give an estimate of the number of users of each type, the expected
load (transactions per day), and the expected database size.
Reference Books:
1.Dr. P. S. Deshpande, SQL and PL/SQL for Oracle 10g Black Book, DreamTech.
2.Ivan Bayross, SQL, PL/SQL: The Programming Language of Oracle, BPB Publication.
3.Reese G., Yarger R., King T., Williums H, Managing and Using MySQL, Shroff
Publishers and Distributors Pvt. Ltd., ISBN: 81 - 7366 - 465 – X, 2nd Edition.
4.Eric Redmond, Jim Wilson, Seven databases in seven weeks, SPD, ISBN: 978-93-
5023-918-6.Jay Kreibich, Using SQLite, SPD, ISBN: 978-93-5110-934-1, 1st edition.
Web Resources:
1.Udemy 2. Coursera 3. SQL TutorialsPoint
4
IT/DBMSL: D-05 Group A: Study of Databases: Study Pages 6-11
of MySQL Open source software.
Experiment No: 1 Semester – II Rev.: 00 Date: 18/12/2017
ASSIGNMENT STATEMENT:
Group A: Study of Databases
Study of MySQL Open source software. Discuss the characteristics like efficiency,
scalability, performance and transactional properties
THEORY:
Database:
A database is a collection of information or data which are organized in such a way that it
can be easily accessed, managed and retrieved.
Elements of Database:
A database table consists of rows and columns which contain data. For example, you have
a table that stores profiles of individuals that is, ID, name, address and contact details.
EMPLOYEE TABLE:
6
IT/DBMSL: D-05 Group A: Study of Databases Pages 6-11
Study of SQLite:
Experiment No: 3 Semester – II Rev.: 00 Date: 18/12/2017
ASSIGNMENT STATEMENT:
Group A: Study of Databases
Study of SQLite: What is SQLite? Uses of SQLite. Building and installing SQLite.
THEORY:
SQLite Database
❖ SQLite is a self-contained, high-reliability, embedded, full-featured, public domain, SQL
database engine. SQLite is the most used database engine in the world
❖ SQLite databases are very lightweight. Unlike other database systems, there is no
configuration, installation required to start working on SQLite database.
❖ What you need is the SQLite library which is less than 500KB size.
DROP TABLE
To drop a table, use the "DROP TABLE" command followed by the table name as
following:
C:\SQLITE>DROP TABLE guru99;
ALTER TABLE
You can use "ALTER TABLE" command to rename a table as following:
C:\SQLITE>ALTER TABLE guru99 RENAME TO guru100;
7
SQLITE INSERT VALUE INTO A TABLE
C:\SQLITE>INSERT INTO guru100 VALUES(1, 'Mike', 25);
C:\>sqlite3
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
The above command will end with SQLite installation on your Linux machine.
$sqlite3
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
8
IT/DBMSL: D-05 Group B: MySQL: Draw suitable ER/ Pages 6-11
EER diagram for the system.
Experiment No: 4 Semester – II Rev.: 00 Date: 18/12/2017
ASSIGNMENT STATEMENT:
Group B: MySQL
Design any database with at least 3 entities and relationships between them. Draw
suitable ER/EER diagram for the system.
THEORY:
ER Diagram stands for Entity Relationship Diagram, also known as ERD. It displays
the relationship of entity sets stored in a database. ER Diagrams contain different symbols
that use rectangles to represent entities, ovals to define attributes and diamond shapes to
represent relationships.
9
EXAMPLE-2: MORE DETAILED ER DIAGRAM OF BANK MANAGEMENT SYSTEM
10
• Customer Entity : Attributes of Customer Entity are Customer_id, Name, Phone
Number and Address.
Customer_id is Primary Key for Customer Entity.
• Branch Entity : Attributes of Branch Entity are Branch_id, Name and Address.
Branch_id is Primary Key for Branch Entity.
• Account Entity : Attributes of Account Entity are Account_number, Account_Type
and Balance.
Account_number is Primary Key for Account Entity.
• Loan Entity : Attributes of Loan Entity are Loan_id, Loan_Type and Amount.
Loan_id is Primary Key for Loan Entity.
Relationships are :
• Bank has Branches => 1 : N
One Bank can have many Branches but one Branch can not belong to many Banks,
so the relationship between Bank and Branch is one to many relationship.
EER Diagram
Generalization is a bottom-up approach in which the common attributes of two or more
lower-level entities combines to form a new higher-level entity.
According to the below diagram, there are two entities, namely teacher and student. The
teacher entity contains attributes such as teacher_id, name, and address and student
entity include student_id, name, and address. A lower-level entity is called a subclass, and
the higher-level entity is called a superclass. So, the person entity is the superclass of two
subclasses teacher and student.
11
Specialization: It is opposite or inverse of generalization. A specialization is a top-down approach
in which an entity of higher-level entity is broken down into two or more entities of lower level.
In the below example, it can be seen that the employee is a high-level entity which is divided
into three sub-entities (Ram, Shyam, and Mohan). The sub-entities are the names of the
employees (relating to the high-level entity). Therefore, splitting a high-level entity into a low-
level entity is called specialization.
12
DBMS Aggregation
13
IT/DBMSL: D-05 Group B: MySQL: Perform Relational, Pages 6-11
Boolean, Arithmetic, Set operators,
Group Functions, Date, time, complex
queries SQL queries
Experiment No: 7 Semester – II Rev.: 00 Date: 18/12/2017
ASSIGNMENT STATEMENT:
Group B: MySQL
Perform following SQL queries on the database:
• Implementation of relational operators in SQL
• Boolean operators and pattern matching
• Arithmetic operations and built in functions
• Group functions
• Processing Date and Time functions
• Complex queries and set operators
THEORY:
14
+----+-------+----------+
| ID | NAME | SALARY |
+----+-------+----------+
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+-------+----------+
The OR Operator
SQL> SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY > 2000 OR age < 25;
V)SQL - GROUP BY
Employees
15
employee_number last_name first_name salary dept_id
1001 Smith John 62000 500
1002 Anderson Jane 57500 500
1003 Everest Brad 71000 501
1004 Horvath Jack 42000 501
There will be 2 records selected. These are the results that you should see:
dept_id total_salaries
500 119500
501 113000
Display all from the CUSTOMERS table, where the SALARY starts with 200.
SQL> SELECT * FROM CUSTOMERS
WHERE SALARY LIKE '200%';
16
Examples:
The following table has a few examples showing the WHERE part having different LIKE
clause with '%' and '_' operators
EXAMPLE :
SELECT DATE("2017-06-15 09:34:21");
Result:
2017-06-15
18
IT/DBMSL: D-05 Group B: MySQL: Create Table with Pages 6-11
primary key and foreign key
constraints. a. Alter table with add n
modify b. Drop table
Experiment No: 6 Semester – II Rev.: 00 Date: 18/12/2017
ASSIGNMENT STATEMENT:
Group B: MySQL
Create Table with primary key and foreign key constraints.
a. Alter table with add n modify b. Drop table
THEORY:
Foreign Key Constraint
-- https://www.mycompiler.io/new/mysql
-- https://www.youtube.com/watch?v=V_zL94zyuYc
deptno is primary key of dept table(parent table) and foreign key of emp table(child table).
So department number that is not existing is parent table cannot be inserted in child table.
-- Child Table
-- Creating emp Table
CREATE TABLE emp(
empno INTEGER primary key ,
empname VARCHAR(20),
deptno INTEGER,
empsal INTEGER);
19
SELECT * FROM emp;
-- Parent Table
-- Creating dept table
CREATE TABLE dept(
deptno INTEGER primary key ,
dname VARCHAR(10),
loc VARCHAR(5));
ERROR 1216 (23000) at line 38: Cannot add or update a child row: a foreign key
constraint fails
21
IT/DBMSL: D-05 Group B: MySQL:Execute DDL/DML Pages 6-11
statements which demonstrate the
use of views.
Experiment No: 8 Semester – II Rev.: 00 Date:18/12/2017
ASSIGNMENT STATEMENT:
Group B: MySQL
Execute DDL/DML statements which demonstrate the use of views. Try to update the base
table using its corresponding view. Also consider restrictions on updatable views and
perform view creation from multiple tables.
THEORY:
VIEWS IN DBMS
Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a
real table in the database. We can create a view by selecting fields from one or more tables
present in the database. A View can either have all the rows of a table or specific rows based
on certain condition.
CREATING VIEWS
Database views are created using the CREATE VIEW statement. Views can be created
from a single table, multiple tables or another view.
To create a view, a user must have the appropriate system privilege according to the
specific implementation.
Example
Consider the CUSTOMERS table having the following records –
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example to create a view from the CUSTOMERS table. This view would be
used to have customer name and age from the CUSTOMERS table.
SQL > CREATE VIEW CUSTOMERS_VIEW AS
22
SELECT name, age
FROM CUSTOMERS;
Now, you can query CUSTOMERS_VIEW in a similar way as you query an actual table.
Following is an example for the same.
SQL > SELECT * FROM CUSTOMERS_VIEW;
This would produce the following result.
+----------+-----+
| name | age |
+----------+-----+
| Ramesh | 32 |
| Khilan | 25 |
| kaushik | 23 |
| Chaitali | 25 |
| Hardik | 27 |
| Komal | 22 |
| Muffy | 24 |
+----------+-----+
Updating a View
A view can be updated under certain conditions which are given below −
• The SELECT clause may not contain the keyword DISTINCT.
• The SELECT clause may not contain summary functions.
• The SELECT clause may not contain set functions.
• The SELECT clause may not contain set operators.
• The SELECT clause may not contain an ORDER BY clause.
• The FROM clause may not contain multiple tables.
• The WHERE clause may not contain subqueries.
• The query may not contain GROUP BY or HAVING.
• Calculated columns may not be updated.
• All NOT NULL columns from the base table must be included in the view in order for the
INSERT query to function.
So, if a view satisfies all the above-mentioned rules then you can update that view. The
following code block has an example to update the age of Ramesh.
This would ultimately update the base table CUSTOMERS and the same would reflect in
the view itself. Now, try to query the base table and the SELECT statement would produce
the following result.
+----+----------+-----+-----------+----------+
23
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
This would ultimately delete a row from the base table CUSTOMERS and the same would
reflect in the view itself. Now, try to query the base table and the SELECT statement would
produce the following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Dropping Views
Obviously, where you have a view, you need a way to drop the view if it is no longer needed.
The syntax is very simple and is given below –
24
IT/DBMSL: D-05 Group C: PL/SQL:PL/SQL stored Pages 6-11
procedure and function
Experiment No: 9 Semester – II Rev.: 00 Date:18/12/2017
ASSIGNMENT STATEMENT:
Group C: PL/SQL:Write and execute PL/SQL stored procedure and function to perform a
suitable task on the database. Demonstrate its use.
THEORY:
PL/SQL – Procedure
Function:
Function, in computer programming language context, a set of instructions which takes
some input and performs certain tasks. In PL/SQL, a function returns a value.
Procedure:
Procedure, as well, is a set of instructions which takes input and performs certain task. In
SQL, procedure does not return a value.
Example1:
DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
/
25
IT/DBMSL: D-05 Group C: PL/SQL: Triggers in Pages 6-11
PL/SQL
Experiment No: 10 Semester – II Rev.: 00 Date: 18/12/2017
ASSIGNMENT STATEMENT:
Group C: PL/SQL:Write and execute suitable database triggers .Consider row level and
statement level triggers.
THEORY:
Example:
To create-Insert-Select Queries using Live Oracle SQL
Example:
26
Select * from customers;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
When the above code is executed at the SQL prompt, it produces the following result –
Trigger created.
UPDATE customers
SET salary = salary + 500
WHERE id = 2;
27
IT/DBMSL: D-05 Group C: PL/SQL: cursor in PL/SQL Pages 6-11
Experiment No: 11 Semester – II Rev.: 00 Date: 18/12/2017
ASSIGNMENT STATEMENT:
Group C: PL/SQL: Write a PL/SQL block to implement all types of cursor.
THEORY:
A cursor is a pointer to this context area. PL/SQL controls the context area through a
cursor. A cursor holds the rows (one or more) returned by a SQL statement. The set of
rows the cursor holds is referred to as the active set.
Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL statement is
executed, when there is no explicit cursor for the statement. Programmers cannot control
the implicit cursors and the information in it.
In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor, which always
has attributes such as %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT.
%NOTFOUND
The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or
2 DELETE statement affected no rows, or a SELECT INTO statement returned no
rows. Otherwise, it returns FALSE.
%ISOPEN
Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor
3
automatically after executing its associated SQL statement.
%ROWCOUNT
4 Returns the number of rows affected by an INSERT, UPDATE, or DELETE
statement, or returned by a SELECT INTO statement.
Example:
The following program will update the table and increase the salary of each customer by
500 and use the SQL%ROWCOUNT attribute to determine the number of rows affected −
28
Select * from customers;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END IF;
END;
/
6 customers selected
29
Explicit Cursors:
Explicit cursors are programmer-defined cursors for gaining more control over the context
area. An explicit cursor should be defined in the declaration section of the PL/SQL Block. It
is created on a SELECT Statement which returns more than one row.
The syntax for creating an explicit cursor is –
CURSOR c_customers IS
SELECT id, name, address FROM customers;
OPEN c_customers;
Example
DECLARE
c_id customers.id%type;
30
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is
SELECT id, name, address FROM customers;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name, c_addr;
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
CLOSE c_customers;
END;
/
When the above code is executed at the SQL prompt, it produces the following result –
1 Ramesh Ahmedabad
2 Khilan Delhi
3 kaushik Kota
4 Chaitali Mumbai
5 Hardik Bhopal
6 Komal MP
31