DBMS Lab Record 2020-21
DBMS Lab Record 2020-21
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
PC 263 CS: DATABASE MANAGEMENT (3VL) Boolean truth values and which are
SYSTEMS LAB used to limit the effects of statements and
Cumulative Record
(Lab Manual & Observation Book) queries, or to change program flow.
● Queries which retrieve data based on specific
Compiled By:-
K.Murali Krishna, N.Sabitha & G.Srishailam criteria.
Assistant Professor(s), CSED, MVSREC
● Statements which may have a persistent
SQL (Structured Query Language): effect on schemas and data, or which may
Structured Query Language is a database computer control transactions, program flow,
language designed for managing data in relational connections, sessions, or diagnostics.
database management systems(RDBMS), and ● SQL statements also include the semicolon
originally based upon Relational Algebra. Its scope (";") statement terminator. Though not
includes data query and update, schema creation and required on every platform, it is defined as a
modification, and data access control. SQL was one standard part of the SQL grammar.
of the first languages for Edgar F.Codd's relational ● Insignificant white space is generally ignored
model in his influential 1970 paper, "A Relational in SQL statements and queries, making it
Model of Data for Large Shared Data Banks" and easier to format SQL code for readability.
became the most widely used language for relational There are five types of SQL statements. They are:
databases. IBM developed SQL in the mid 1970's. 1. DATA DEFINITION LANGUAGE (DDL)
Oracle incorporated in 1979. SQL used by 2. DATA MANIPULATION LANGUAGE
IBM/DB2 and DS Database Systems. SQL was (DML)
adopted as the standard language for RDBS by ANSI 3. DATA RETRIEVAL LANGUAGE (DRL)
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
rows of data. of a database project. Let's take a look at the
● Predicates which specify conditions that can structure and usage of four basic DDL commands:
be evaluated to SQL three-valued logic 1. CREATE 2. ALTER 3. DROP 4. RENAME
--------------------------------------------------------------------------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
Program 1 Pseudo code
CREATION OF DATABASE Select a database
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
ALTER TABLE <table_name> ADD
<column_name datatype>;
To list tables
Show tables;
TRUNCATE:
The SQL TRUNCATE TABLE command is used to
delete complete data from an existing table. The
basic syntax of a TRUNCATE TABLE command is
as follows.
TRUNCATE TABLE table_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
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
To delete data from a table
DELETE FROM <table_name> WHERE
[condition];
Program 2
UPDATE <table_name> SET column1 = value1,
SIMPLE AND COMPLEX QUERIES
column2 = value, columnN = valueN WHERE
[condition];
Problem Definition
Transactional Control Language
Simple to complex condition query creation using
SQL Plus SAVEPOINT <savepoint-name>;
ROLLBACK;
Problem Description
SQL Plus commands are used fundamentally for COMMIT;
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
[, table2 ] [WHERE]); 5. Max()
SELECT <column1, column2> FROM <table1, An operator is a reserved word or a character used
table2> WHERE [conditions] GROUP BY primarily in an SQL statement WHERE clause to
column1, column2 HAVING [conditions] perform operation(s), such as comparisons and
ORDER BY column1, column2; arithmetic operations. These Operators are used to
specify conditions in an SQL statement and to
Order by : The order by clause is used to display
serve as conjunctions for multiple conditions in a
the results in sorted order.
statement.
Group by : The attribute or attributes given in the
1. Arithmetic operators
clauses are used to form groups. Tuples with the
2. Comparison operators
same value on all attributes in the group by clause
3. Logical operators
are placed in one group.
4. Operators used to negate conditions
Having: SQL applies predicates (conditions) in
the having clause after groups have been formed,
JOINS
so aggregate function be used.
SELECT <column_name(s)> FROM <table1>
INNER JOIN <table2> ON
Set Operations: UNION - OR INTERSECT -
table1.column_name=table2.column_name;
AND EXCEPT - - NOT
NESTED QUERY:- A nested query makes use of SELECT <column_name(s)> FROM <table1>
another sub-query to compute or retrieve the LEFT JOIN/RIGHT JOIN <table2> ON
information. table1.column_name=table2.column_name;
a function where the values of multiple rows are FULL OUTER JOIN <table2> ON
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
Select
● distinct
● order by
● group by
Comparison operators
● Like, Not like
● > ,<.>=,<=,==,<>
● Between and, not between and
● In, not in
● Any, all
Aggregate functions
● Count, Sum, Average, Max, Min
Set Operations
● Union, Except, Intersect
Nested Queries
● Correlated
--------------------------------------------------------------------------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
--------------------------------------------------------------------------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
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
Program 3
affects.
TRIGGERS
Eg: If the trigger makes the security check on the
Problem Definition time or the user.
Using Triggers and stored procedures. Before trigger: It executes the trigger action
before the triggering statement is executed. Eg:
Problem Description Before triggers are used to derive specific column
Database Triggers are procedures that are stored in values before completing a triggering INSERT or
the database and are explicitly executed (fired) UPDATE statement.
where the contents of a table are changed. After trigger: It executes the trigger after the
Triggers have 3 basic parts. triggering statement is executed. After triggers are
1. Trigger event: It is a SQL statement that used when you want the triggering statement to
causes the trigger to be fired. It can insert, complete before executing the triggering action.
delete or update statements for a specified
table. Pseudo code
Create or replace trigger [Schema.]
2. Trigger restriction: It specifies a Boolean
(logical) expression that must be true for the <Triggername>
trigger to fire. It is specified using a where {Before | After}
clause. {Delete| Insert| Update [of column, -----] } On
3. Trigger action: It is a procedure that [Schema.] <tablename>
contains the SQL statement and Pl / SQL [Referencing {OLD as old, NEW as new} ] for
code to be executed when a triggering each row [ when condition ]
statement is issued and the trigger restriction Raise_application_error (error-no, ‘message’);
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
Syntax of deleting a trigger statement that you want to process.
Drop trigger trigger-name; ● Open the Cursor.
● Fetch the data from the cursor one row at a
time.
CURSOR: ● Close the cursor.
We have seen how oracle executes an SQL Explicit Cursor Attributes- Oracle provides
statement. Oracle DBA uses a work area for its certain attributes/ cursor variables to control the
internal processing. This work area is private to execution of the cursor. Whenever any
SQL’s operation and is called a cursor. cursor(explicit or implicit) is opened and used,
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
the cursor. You
is the size required to hold the number of rows in
the Active Data Set. ● Declare a cursor that specifies the SQL select
statement that you want to process.
● Open the Cursor.
● Fetch the data from the cursor one row at a
Explicit Cursor- You can explicitly declare a time.
cursor to process the rows individually. ● Close the cursor.
A cursor declared by the user is called Explicit How to Declare the Cursor:-
Cursor. For Queries that return more than one row, The General Syntax to create any particular cursor
You must declare a cursor explicitly. is as follows:-
The data that is stored in the cursor is called the Cursor <Cursorname> is Sql Statement;
Active Data set. The size of the cursor in memory How to Open the Cursor:-
is the size required to hold the number of rows in The General Syntax to Open any particular cursor
the Active is as follows:-
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 values:
Task:3.2
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
PROCEDURES
--------------------------------------------------------------------------------------------------------------------------------------------------
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:
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.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
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.
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.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
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.
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 .
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
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:
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.
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
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.
Drop user
Program 7
TABLE LOCKING
Problem Definition
Usage of file locking, table locking facilities in
application
Problem Description
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021
PC 263 CS: DATABASE MANAGEMENT SYSTEMS LAB
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
allowing max concurrent access to data by updating, inserting or deleting.
unlimited users.
SHARE ROW EXCLUSIVE They are used to
Locks are used to achieve two important goals. 1.
lock the whole table to selective update and to
Data concurrency
allow other users to lock a row in the table but not
2. Oracle lock is fully automatic and requires no
lock the table in share mode or to update rows.
user action .DBA locks the oracle data while
NO WAIT Indicates that you do not wish to wait
executing SQL statements. This type of locking is
if resources are unavailable. All locks are released
called implicit locking. When a lock is put by the
under the following circumstances:
user it is called explicit locking.
1. The transaction is committed successfully
Types of locks
2. A rollback is performed
Two levels of lock that a DBA can apply. They 3. A rollback to a save point will release locks
are set after specified save point
4. Row-level-locks are not released by rolling
1. Shared : Multi user can hold various share
back to a savepoint
lock on a single resource
5. Data locks are released by log off
2. Exclusive: It prohibits all sharing of
resources i.e. only one use has the sole
Task 7: Demonstrate MySQL locking
ability to alter the resources until locks are mechanism for cooperating table access
released. between sessions.
Pseudo code
--------------------------------------------------------------------------------------------------------------------------------------------------
BE IV SEM(AICTE) 2020-2021