DBMS Lab Record 2020-21-Final
DBMS Lab Record 2020-21-Final
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PC 263 CS: DATABASE MANAGEMENT limit the effects of statements and queries, or
SYSTEMS LAB to change program flow.
Cumulative Record
(Lab Manual & Observation Book) ● Queries which retrieve data based on specific
criteria.
Compiled By:-
K.Murali Krishna, N.Sabitha & G.Srishailam ● Statements which may have a persistent effect
Assistant Professor(s), CSED, MVSREC
on schemas and data, or which may control
SQL (Structured Query Language): transactions, program flow, connections,
Structured Query Language is a database computer sessions, or diagnostics.
language designed for managing data in relational ● SQL statements also include the semicolon
database management systems(RDBMS), and (";") statement terminator. Though not
originally based upon Relational Algebra. Its scope required on every platform, it is defined as a
includes data query and update, schema creation and standard part of the SQL grammar.
modification, and data access control. SQL was one ● Insignificant white space is generally ignored
of the first languages for Edgar F.Codd's relational in SQL statements and queries, making it
model in his influential 1970 paper, "A Relational easier to format SQL code for readability.
Model of Data for Large Shared Data Banks" and There are five types of SQL statements. They are:
became the most widely used language for relational 1. DATA DEFINITION LANGUAGE (DDL)
databases. IBM developed SQL in the mid 1970's. 2. DATA MANIPULATION LANGUAGE
Oracle incorporated in 1979. SQL used by IBM/DB2 (DML)
and DS Database Systems. SQL was adopted as the 3. DATA RETRIEVAL LANGUAGE (DRL)
standard language for RDBS by ANSI in 1989. 4. TRANSACTIONAL CONTROL
SQL language is subdivided into several language LANGUAGE (TCL)
elements, including: 5. DATA CONTROL LANGUAGE (DCL)
● Clauses, which are in some cases optional, DATA DEFINITION LANGUAGE (DDL):
constituent components of statements and The Data Definition Language (DDL) is used
queries. to create and destroy databases and database objects.
● Expressions, which can produce either scalar These commands will primarily be used by database
values or tables consisting of columns and administrators during the setup and removal phases of
rows of data. a database project. Let's take a look at the structure
● Predicates which specify conditions that can and usage of four basic DDL commands:
be evaluated to SQL three-valued logic (3VL) 1. CREATE 2. ALTER 3. DROP 4. RENAME
Boolean truth values and which are used to
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DATA MANIPULATION LANGUAGE (DML):
The Data Manipulation Language (DML) is used to
retrieve, insert and modify database information.
These commands will be used by all database users
during the routine operation of the database. Let's take
a brief look at the basic DML commands:
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE DATABASE [IF NOT EXISTS]
Problem Description <database_name>;
Creating the structure of an entity is done by using
Review the created database
DDL commands. Data Definition Language
SHOW DATABASE <database_name>;
(DDL) commands are those commands that define
either the structure of an object or the database in Delete an existing database in the server.
which the objects are stored. These commands
DROP DATABASE [IF EXISTS]
include the database creation, modification and
<database_name>;
administration.
Display Databases
● The name, data type and size of each column Foreign Key( column ) References
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Program 2
SIMPLE AND COMPLEX QUERIES
Problem Definition
Simple to complex condition query creation using
SQL Plus
Problem Description
SQL Plus commands are used fundamentally for
accessing and managing the data in the database.
Simple Queries: Simple Queries includes insert,
select, update, alter, delete, drop, savepoint,
commit, rollback etc.
Complex Queries: Complex Queries includes
Joins, Subqueries, constraints, views etc.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Pseudo Code (CONDITION);
Read data from one or more tables
ALTER TABLE <table_name> ADD
SELECT column1, column2, columnN FROM CONSTRAINT <MyUniqueConstraint> UNIQUE
table_name; (column1,column2….);
SELECT * FROM < table_name>;
ALTER TABLE <table_name> DROP
To insert new records in a table CONSTRAINT <constraint-name>;
UPDATE <table_name> SET column1 = value1, column1, column2 HAVING [conditions] ORDER
[condition];
Order by : The order by clause is used to display
Transactional Control Language
the results in sorted order.
SAVEPOINT <savepoint-name>; Group by : The attribute or attributes given in the
ALTER TABLE <table_name> ADD having clause after groups have been formed, so
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
another sub-query to compute or retrieve the table1.column_name=table2.column_name;
information.
SELECT <column_name(s)> FROM <table1>
JOINS
SELECT <column_name(s)> FROM <table1>
INNER JOIN <table2> ON
table1.column_name=table2.column_name;
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
● Cartesian Product
● Inner Join
○ Self Join
○ Equi join / Natural join
○ Non equi join
● Outer Join
○ Left
○ Right
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Problem Description
Database Triggers are procedures that are stored in
the database and are explicitly executed (fired)
where the contents of a table are changed.
Triggers have 3 basic parts.
1. Trigger event: It is a SQL statement that
causes the trigger to be fired. It can insert,
delete or update statements for a specified
table.
2. Trigger restriction: It specifies a Boolean
(logical) expression that must be true for the
trigger to fire. It is specified using a where
clause.
3. Trigger action: It is a procedure that contains
the SQL statement and Pl / SQL code to be
executed when a triggering statement is
issued and the trigger restriction evaluation to
true.
Types of triggers
Row trigger: A row trigger is fired each time the
table is affected by a triggering statement. Eg: if an
update statement updates multiple rows of a table,
a row trigger is fired once for each row affected by
the update statement.
Statement trigger: A row trigger is fired once on
behalf of the triggering statement independent of the
number of rows the triggering statement affects.
Program 3 Eg: If the trigger makes the security check on the
time or the user.
TRIGGERS
Before trigger: It executes the trigger action before
Problem Definition
the triggering statement is executed. Eg: Before
Using Triggers and stored procedures. triggers are used to derive specific column values
before completing a triggering INSERT or
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE statement. A cursor declared by the user is called Explicit
After trigger: It executes the trigger after the Cursor. For Queries that return more than one row,
triggering statement is executed. After triggers are You must declare a cursor explicitly.
used when you want the triggering statement to
The data that is stored in the cursor is called the
complete before executing the triggering action.
Active Data set. The size of the cursor in memory is
the size required to hold the number of rows in the
Pseudo code
Active
Create or replace trigger [Schema.]
Why use an Explicit Cursor- Cursor can be used
<Triggername>
when the user wants to process data one row at a
{Before | After}
time.
{Delete| Insert| Update [of column, -----] } On
Explicit Cursor Management- The steps involved
[Schema.] <tablename>
in declaring a cursor and manipulating data in the
[Referencing {OLD as old, NEW as new} ] for
active data set are:-
each row [ when condition ]
Raise_application_error (error-no, ‘message’); ● Declare a cursor that specifies the SQL select
statement that you want to process.
Syntax of deleting a trigger ● Open the Cursor.
Drop trigger trigger-name; ● Fetch the data from the cursor one row at a
time.
● Close the cursor.
CURSOR:
Explicit Cursor Attributes- Oracle provides
We have seen how oracle executes an SQL certain attributes/ cursor variables to control the
statement. Oracle DBA uses a work area for its execution of the cursor. Whenever any
internal processing. This work area is private to cursor(explicit or implicit) is opened and used,
SQL’s operation and is called a cursor. Oracle creates a set of four system variables via
The data that is stored in the cursor is called the which Oracle keeps track of the ‘Current’ status of
Active Data set. The size of the cursor in memory is the cursor. You
the size required to hold the number of rows in the ● Declare a cursor that specifies the SQL select
Active Data Set. statement that you want to process.
● Open the Cursor.
● Fetch the data from the cursor one row at a
time.
Explicit Cursor- You can explicitly declare a
● Close the cursor.
cursor to process the rows individually.
How to Declare the Cursor:-
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The General Syntax to create any particular cursor values:
is as follows:-
Open Cursorname;
One can make use of any loop structure(Loop-End Table : Accounts(AcCNo, Name, Balance,
Loop along with While,For) to fetch the records DateofOpening, Branch)
from the cursor into one row at a time.
Closing a Cursor:-
Close <cursorname>;
Task 3.1:
Create a row level trigger for the customers table
that would fire for INSERT or UPDATE or
DELETE operations performed on the
CUSTOMERS table. This trigger will display the
salary difference between the old values and new
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
output parameters.
The syntax of a creation of procedure is as follows:
Syntax:
CREATE OR REPLACE PROCEDURE
procedure_name [(argument1 [IN/OUT/IN OUT]
datatype,
argument2 [IN/OUT/IN OUT] datatype,_)] IS
[<local variable declarations>]
BEGIN
[EXCEPTION
Optional Exception Handler(s) ]
Program -4 Executable Statements
SUB PROGRAMS END;
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Program 5
FORM DESIGNING
Problem Definition
Creation of Forms using LibreOffice Base.
LibreOffice Base is a front end database manager that can be used to create forms and reports from a variety
of databases including MySql as well as others.
LibreOffice Base can be used to create small embedded databases when used with Java-based HSQLDB as its
storage engine (The LibreOffice Base default database). This makes LibreOffice Base appear as if it were a
database manager and the database, but it is not. LibreOffice Base is just the front end allowing us to tie into
the actual database.
The LibreOffice suite of tools includes a very powerful database application ─ one that happens to be incredibly
user-friendly. These databases can be managed/edited by any user and data can be entered by anyone using a
LibreOffice-generated form. These forms are very simple to create and can be attached to existing databases or
you can create both a database and a form in one fell swoop.
Design view is a versatile drag and drop form creator that is quite powerful and allows you to add elements and
assign those elements to database tables. The Form Wizard is a very simple step-by-step wizard that walks the
user through the process of creating a form. Although the Wizard isn’t nearly as powerful as the Design View
─ it will get the job done quickly and doesn’t require any form of design experience.
For this entry, I will address the Form Wizard (in a later post, I will walk you through the more challenging
Design View). I will assume you already have a database created and ready for data entry. This database can
either be created with LibreOffice and reside on the local system or be a remote database of the format:
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. Oracle JDBC
2. Spreadsheet
3. dBASE
4. Text
5. MySQL
6. ODBC.
For purposes of simplicity, we’ll go with a local LibreOffice Base-generated database. I’ve created a very
simple database with two tables to be used for this process. Let’s create a data entry form for this database.
The next window to appear is the heart and soul of LibreOffice Base. Here in Figure you can manage
tables, run queries, create/edit forms, and view reports of the opened database.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Click the Forms button in the left-side navigation and then double-click Use Wizard to Create Form under
Tasks.When the database opens in the Form Wizard, your first step is to select the fields available to the form.
You do not have to select all fields from the database. You can select them all or you can select as few as one.
If your database has more than one table, you can select between the tables in the Tables or queries drop-down
(NOTE: You can only select fields from one table in the database at this point). Select the table to be used and
then add the fields from the Available fields section to the Fields in the form section in the Figure 3.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Once you’ve selected all the necessary fields, click Next. At this point, you can choose to add a subform. A
subform is a form-within-a-form and allows you to add more specific data to the original form. For example, you
can include secondary data for employee records (such as work history, raises, etc.) to a form. This is the point at
which you can include fields from other tables (besides the initial table selected from the Tables or queries drop-
down). If you opt to create a subform for your data, the steps include:
After all subforms are added, click Next to continue on. In the next step, you must arrange the controls of the
form. This is just another way of saying how you want the form to look and feel (where do you want the data
entry field to reside against the field label). You can have different layouts for forms and subforms .
Click Next when you’ve arranged your controls. The next step is to select the data entry mode (Figure 6). There
are two data entry modes:
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
● Enter new data only
● Display all data.
If you want to use the form only as a means to enter new data, select Enter new data only. If, however, you know
you’ll want to use the form to enter and view data, select Display all data. If you go for the latter option, you will
want to select whether previously entered data can be modified or not. If you want to prevent write access to the
previous data, select Do not allow modification of existing data.
At this point you can select a style for your form. This allows you to pick a color and field border (no border, 3D
border, or flat). Make your selection and click Next.
The last step is to name your form. In this same window you can select the option, immediately begin working
with the form (Figure 7). Select that option and click Finish. At this point, your form will open and you can start
entering data.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
After a form is created, and you’ve worked with and closed said form … how do you re-open a form to add more
data? Simple:
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
As a final note, make sure, after you finish working with your forms, that you click File >
Save in the LibreOffice Base main window, to ensure you save all of your work.
You can create as many forms as you need with a single database ─ there is no limit to what
you can do.
If you’re looking to easily enter data into LibreOffice databases, creating user-friendly forms
is just a few steps away. Next time we visit this topic, we’ll walk through the Design View method of
form creation.
Reports
Reports give you the ability to present information from your database in an easy-to-read, visually appealing
printable format. For example, you can create a simple report of phone numbers for all your contacts, or a
summary report on the total sales across different regions and time periods. Base makes it easy to create and
customize a report using data from any query or table in your database.
Creating Reports
Select the Use Wizard to Create Report. The Wizard opens a dialog window. In the following example we will
generate a report that displays all book's title, author and publishing date. The results will be grouped by author.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Step 1
In this step you select the Table Fields that you want the report to contain. In this example we select the Title,
Author and PublishingDate fields.
Step 2
The report generates a label for each field. By default Base chooses the field name for the label name. In our
example we change the "PublishingDate" field to "Publishing Date".
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Step 3
A report can group the results by one or more fields. In our example we choose to group the results by Author.
Step 4
As with queries, the results in reports can be sorted. By default results are sorted by the group field (Author). You
can specify more levels of sorting. Leave the default values.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Step 5
In this step we choose the Layout for the data and the headers and footers of the report. Click on the available
options to preview each layout.
Step 6
Here we specify the title and the type of the report. A dynamic report generates data from the current table data.
This means that if we update data on the table that feeds the report, the report will change accordingly.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
When you finish the wizard the report will be created and opened. The report is a text document and therefore it
opens using the Writer component of LibreOffice.
Managing a Report
To manage a Report first select the Reports Object from the Database Objects Pane and then select the report
from the Reports List. Use the Reports toolbar to Open, Edit, Delete or Rename a Report.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Editing a Report
A Report is basically a Writer document that acquires data from Base. Therefore you can edit a report like any
other Writer Document. For example you can change the alignment of the Publishing Date column. When you
edit a report you must save it using the Save Button from the Standard toolbar.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------
password] [WITH ADMIN OPTION]
Program 6
GRANT ALL PRIVILEGES TO grantee
PASSWORD AND SECURITY
[IDENTIFIED BY password] [WITH ADMIN
FEATURES
OPTION]
Grant privs on specific objects:
Problem Definition
GRANT object_priv [(column, column,...)]
Creating password and security features of
application. ON [schema.]object TO grantee [WITH GRANT
OPTION] [WITH HIERARCHY OPTION]
Problem Description GRANT ALL PRIVILEGES [(column,
All the core of security in the Oracle database user column,...)] ON [schema.]object TO grantee
account, without an Oracle username there is no [WITH GRANT OPTION] [WITH
way that one can break into Oracle database. An HIERARCHY OPTION]
Oracle database typically has Number of Oracle GRANT object_priv [(column, column,...)] ON
user accounts that were either created when the DIRECTORY directory_name TO grantee
database was created or created later by the [WITH GRANT OPTION] [WITH
database administrator. Once created an Oracle HIERARCHY OPTION]
user has a number, and privileges that allow him to GRANT object_priv [(column, column,...)] ON
create and modify database objects. JAVA [RE]SOURCE [schema.]object TO
Once an Oracle user is given a username and grantee [WITH GRANT OPTION] [WITH
password he can connect to Oracle database HIERARCHY OPTION]
using any to
grantee: user role PUBLIC
-------------------------------------------------------------------------------------------------------------------------------------------------
- BE IV SEM(AICTE) 2020-2021