DBMS Lab Manual
DBMS Lab Manual
GHAZIABAD(U.P.)
Recognized by AICTE and Approved by Dr. APJ AKTU, Lucknow
PRACTICAL FILE
SESSION (2023-2024)
1
Creating tables and writing Queries in SQL.
8
To implement the concept of Indexes and views.
View definition-
The SQL DDL includes commands for defining views.
Transaction Control- SQL includes for specifying the beginning and ending of transactions.
Integrity-
The SQL DDL includes commands for specifying integrity constraints that the data stored in the
database must specify. Updates that violate integrity constraints are allowed.
Authorization-
The SQL DDL includes commands for specifying access rights to relations and views.
The SQL DDL allows specification of not only a set of relations but also information about each
relation, including-
• Schema for each relation
• The domain of values associated with each attribute.
• The integrity constraints.
• The set of indices to be maintained for each relation.
• The security and authorization information for each relation.
• The physical storage structure of each relation on disk.
Syntax-
Create table tablename
(columnname datatype(size), columnname datatype(size));
Syntax-
INSERT INTO tablename
[(columnname, columnname, ………)]
Values(expression, expression);
Syntax-
INSERT INTO tablename
SELECT columnname, columnname, …….
FROM tablename;
Syntax-
INSERT INTO tablename
SELECT columnname, columnname……..
FROM tablename
WHERE columnname= expression;
Syntax-
SELECT columnname, columnname, ….
FROM tablename;
Syntax-
SELECT DISTINCT columnname, columnname
FROM tablename;
Syntax-
SELECT columnname, columnname
FROM tablename
WHERE searchcondition;
Description
Deletion Operation:-
A delete reQuestionst is expressed in much the same way as Questionry. We can delete whole
tuple ( rows) we can delete values on only particulars attributes.
Syntax:
Delete from tablename :
+ Addition - Subtraction
* multiplication ** exponentiation
/ Division () Enclosed operation
Logical Operators:
The logical operators that can be used in SQL sentenced are
• Percent (%) ; the % character matches any substring we consider the following examples.
• ‘Perry %’ matches any string beginning with perry
• ‘% idge % matches any string containing’ idge as substring.
• ‘ - - - ‘ matches any string exactly three characters.
• ‘ - - - % matches any string of at least of three characters.
Oracle functions:
Functions are used to manipulate data items and return result. function follow the format of
function _name (argument1, argument2 ..) .An arrangement is user defined variable or constant.
The structure of function is such that it accepts zero or more arguments.
Examples:
Avg return average value of n
Syntax:
Avg ([distinct/all]n)
Min return minimum value of expr.
Syntax:
MIN((distict/all )expr)
Count Returns the no of rows where expr is not null
Syntax:
Count ([distinct/all)expr]
Count (*) Returns the no rows in the table, including duplicates and those with nulls.
Max Return max value of expr
Syntax:
Syntax:
Sum ([distinct/all]n)
Sorting of data in table
Syntax:
Select columnname, columnname
From table
Order by columnname;
Description:
Data constraints: Besides the cell name, cell length and cell data type there are other parameters
i.e. other data constrains that can be passed to the DBA at check creation time. The constraints can
either be placed at column level or at the table level.
i. Column Level Constraints: If the constraints are defined along with the column
definition, it is called a column level constraint.
ii. Table Level Constraints: If the data constraint attached to a specify cell in a table
reference the contents of another cell in the table then the user will have to use table level
constraints.
Null Value Concepts:- while creating tables if a row locks a data value for particular
column that value is said to be null . Column of any data types may contain null values
unless the column was defined as not null when the table was created
Syntax:
Primary Key: primary key is one or more columns is a table used to uniquickly identity
each row in the table. Primary key values must not be null and must be uniQuestion across
the column. A multicolumn primary key is called composite primary key.
UniQuestion key concept:-A uniQuestion is similar to a primary key except that the
purpose of a uniQuestion key is to ensure that information in the column for each record is
uniQuestion as with telephone or devices license numbers. A table may have many
uniQuestion keys.
Default value concept: At the line of cell creation a default value can be assigned to it.
When the user is loading a record with values and leaves this cell empty, the DBA wil
automatically load this cell with the default value specified. The data type of the default
value should match the data type of the column
Syntax:
Foreign Key Concept : Foreign key represents relationship between tables. A foreign key
is column whose values are derived from the primary key of the same of some other table
. the existence of foreign key implies that the table with foreign key is related to the primary
key table from which the foreign key is derived .A foreign key must have corresponding
primary key value in the primary key table to have meaning.
Foreign key as a column constraint
Syntax :
Create table table name
(columnname datatype (size) references another table name);
Syntax :
Create table name
(columnname datatype (size)….
primary key (columnname);
foreign key (columnname)references table name);
Check Integrity Constraints: Use the check constraints when you need to enforce
intergrity rules that can be evaluated based on a logical expression following are a few
examples of appropriate check constraints.
• A check constraints name column of the coient_master so that the name is entered
in upper case.
• A check constraint on the client_no column of the client _master so that no
client_no value starts with ‘c’
Syntax:
Create table tablename
(columnname datatype (size) CONSTRAINT constraintname)
Check (expression));
I. Sales_order_details
Column Datatype Size Attributes
Insert the following data into their respective tables using insert statement:
Description:
Modifying the Structure of Tables- Alter table command is used to changing the structure of a
table. Using the alter table clause you cannot perform the following tasks:
The following tasks you can perform through alter table command.
NOTE: Oracle not allow constraints defined using the alter table, if the data in the table, violates
such constraints.
Syntax:
DROP TABLE tabename:
You can also define integrity constraints using the constraint clause in the ALTER TABLE
command. The following examples show the definitions of several integrity constraints.
You can drop an integrity constraint if the rule that if enforces is no longer true or if the
constraint is no longer needed. Drop the constraint using the ALTER TABLE command with the
DROP clause. The following examples illustrate the droping of integrity constraints.
Description
Joint Multiple Table (Equi Join): Some times we require to treat more than one table as though
manipulate data from all the tables as though the tables were not separate object but one single
entity. To achieve this we have to join tables.Tables are joined on column that have dame data
type and data with in tables.The tables that have to be joined are specified in the FROM clause
and the joining attributes in the WHERE clause.
Algorithm for JOIN in SQL:
1. Cartesian product of tables (specified in the FROM clause)
2. Selection of rows that match (predicate in the WHERE clause)
3. Project column specified in the SELECT clause.
1. Cartesian product:-
Consider two table student and course
Select B.*,P.*
FROM student B, course P;
2. INNER JOIN:
Cartesian product followed by selection
Select B.*,P.*
FROM student B, Course P
WHERE B.course # P.course # ;
Exam:
Select B.*,P.*
From student B RIGHT JOIN course P
B.course# = P course # ;
Description
Grouping Data From Tables:
There are circumstances where we would like to apply the aggregate function not only to a single
set of tuples, but also to a group of sets of tuples, we specify this wish in SQL using the group by
clause. The attribute or attributes given in the group by clause are used to form group. Tuples with
the same value on all attributes in the group by clause are placed in one group.
Pre-Practical Questions:
Q1. Why groups are required in database?
Q2. What is minimal requirement for two tables to group together?
Q3. Differentiate b/w group & join syntax.
Syntax:
SELECT columnname, columnname
FROM tablename
GROUP BY columnname;
At times it is useful to state a condition that applies to groups rather than to tuples. For example
we might be interested in only those branches where the average account balance is more than
1200. This condition does not apply to a single tuple, rather it applies to each group constructed
by the GROUP BY clause. To express such Questionry, we use the having clause of SQL. SQL
applies predicates in the having may be used.
Syntax:
SELECT columnname, columnname
FROM tablename
GROUP BY columnname;
HAVING searchcondition;
Description
Sub Queries:- A Sub Queries is a form of an SQL statement that appears inside another SQL
statement. It also termed as nested Questionry. The statement containing a Sub Queries called a
parent statement. The rows returned bu the Sub Queries are use by the following statement.
It can be used by the following commands:
1. To insert records in the target table.
2. To create tables and insert records in this table.
3. To update records in the target table.
4. To create view.
5. To provide values for the condition in the WHERE , HAVING IN , SELECT,UPDATE,
and DELETE statements.
Pre-Practical Questions:
Q1. How logical operators apply at SQL table?
Q2. How sub quires apply at table?
Q3.Difference b/w quires and sub-quires.
Output: = Records only in Questionry one + records only in Questionry two + A single set of
records with is common in the both Queries.
Syntax:
SELECT columnname, columname FROM tablename 1
UNION
SELECT columnname, columnname From tablename2;
Intersect Clause: The use can put together multiple Queries and their output using the interest
clause. The final output of the interest clause will be :
MINUS CLAUSE:- The user can put together multiple Queries and combine their output = records
only in Questionry one
Syntax:
SELECT columnname, columnname
FROM tablename ;
MINUS
SELECT columnname, columnname
FROM tablename ;
Description:
Indexes- An index is an ordered list of content of a column or group of columns in a table. An index
created on the single column of the table is called simple index. When multiple table columns are
included in the index it is called composite index.
Creating an Index for a table:-
Syntax (Simple)
CREATE INDEX index_name
ON tablename(column name);
Composite Index:-
CREATE INDEX index_name
ON tablename(columnname,columnname);
Creating an UniQuestion Index:-
CREATE UNIQUESTION INDEX indexfilename
ON tablename(columnname);
Dropping Indexes:-
An index can be dropped by using DROP INDEX
SYNTAX:-
DROP INDEX indexfilename;
Views:-
Logical data is how we want to see the current data in our database. Physical data is how
this data is actually placed in our database.
Views are masks placed upon tables. This allows the programmer to develop a method via
which we can display predetermined data to users according to our desire.
Views may be created fore the following reasons:
1. The DBA stores the views as a definition only. Hence there is no duplication of data.
2. Simplifies Queries.
3. Can be Questionried as a base table itself.
4. Provides data security.
5. Avoids data redundancy.
Creation of Views:-
Syntax:-
CREATE VIEW viewname AS
SELECT columnname,columnname
FROM tablename
WHERE columnname=expression_list;
Destroying a view-
Syntax:-
DROP VIEW viewname;
Introduction – PL/SQL bridges the gap between database technology and procedural
programming languages. It can be thought of as a development tool that extends the facilities of
Oracles SQL database language. Via PL/SQL you can insert, delete, update and retrieve table
data as well as use procedural techniQuestions such as writing loops or branching to another
block of code.
DECLARE
Declarations of memory variables used later
BEGIN
SQL executable statements for manipulating table data.
EXCEPTIONS
SQL and/or PL.SQL code to handle errors.
END;
Displaying user Messages on the screen – Any programming tool requires a method through
which messages can be displayed to the user.
dbms_output is a package that includes a number of procedure and functions that accumulate
information in a buffer so that it can be retrieved later. These functions can also be used to
display message to the user.
put_line: put a piece of information in the buffer followed by a end of line marker. It can also be
used to display message to the user.
Setting the server output on:
Example: Write the following code in the PL/SQL block to display message to user
DBMS_OUTPUT.PUT_LINE(‘Display user message’)
The GOTO statement: The goto statement allows you to change the flow of control within a
PL/SQL Block.
Description:
Cursor– We have seen how oracle executes an SQL statement. Oracle DBA uses a work area for
its internal processing. This work area is private to SQL’s operation and is called a cursor.
The data that is stored in the cursor is called the Active Data set. The size of the cursor in memory
is the size required to hold the number of rows in the Active Data Set.
Explicit Cursor- You can explicitly declare a cursor to process the rows individually. A cursor
declared by the user is called Explicit Cursor. For Queries that return more than one row, You
must declare a cursor explicitly.
The data that is stored in the cursor is called the Active Data set. The size of the cursor in
memory is the size required to hold the number of rows in the Active
Why use an Explicit Cursor- Cursor can be used when the user wants to process data one row
at a time.
Explicit Cursor Management- The steps involved in declaring a cursor and manipulating data
in the active data set are:-
• 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 time.
• Close the cursor.
Explicit Cursor Attributes- Oracle provides certain attributes/ cursor variables to control the
execution of the cursor. Whenever any cursor(explicit or implicit) is opened and used Oracle
creates a set of four system variables via which Oracle keeps track of the ‘Current’ status of the
cursor. You
• 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 time.
• Close the cursor.
How to Declare the Cursor:-
The General Syntex to Fetch the records from the cursor is as follows:-
Fetch cursorname into variable1,variable2,______
Closing a Cursor:-
Database Triggers:-
Database triggers are procedures that are stored in the database and are implicitly executed(fired)
when the contents of a table are changed.
Types of Triggers:-