0% found this document useful (0 votes)
4 views

DMS Question Bank Answers

The document contains a comprehensive question bank on PL/SQL, covering topics such as writing PL/SQL programs, exception handling, aggregate functions, views, ACID properties, SQL queries, database failures, and privileges. It includes examples and syntax for various commands and functions, as well as explanations of concepts like cursors and transaction management. Additionally, it discusses the advantages of using functions in PL/SQL and outlines different types of database users.

Uploaded by

ifatpatel18
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DMS Question Bank Answers

The document contains a comprehensive question bank on PL/SQL, covering topics such as writing PL/SQL programs, exception handling, aggregate functions, views, ACID properties, SQL queries, database failures, and privileges. It includes examples and syntax for various commands and functions, as well as explanations of concepts like cursors and transaction management. Additionally, it discusses the advantages of using functions in PL/SQL and outlines different types of database users.

Uploaded by

ifatpatel18
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

DMS Question Bank

4 marks

1. Write a PL-SQL program to print odd numbers from 1 to 10.


Begin
dbms_output.put_line('Odd Numbers from 1 to 10 are:');
for num in 1..10
loop if(mod(num,2)!=0) then
dbms_output.put_line(num);
end if;
end loop;
end;
/
2. Describe exception handling in PL/SQL
Exception Handling: Exception is nothing but an error.
Exception can be raise when DBMS encounters errors or it can
be raised explicitly. When the system throws a warning or has
an error it can lead to an exception. Such exception needs to be
handled and can be defined internally or user defined.
Exception handling is nothing but a code block in memory that
will attempt to resolve current error condition.
Syntax:
DECLARE
Declaration section
BEGIN
…executable statement;
EXCEPTION
WHEN exception_name THEN
Error handling statements/user defined action to be carried
out;
END;

To raise the exception explicitly within PL/SQL block, ’Raise’


statement is used.
RAISE exception_name;s
Types of Exception:
1) Predefined Exception/system defined exception/named
exception:
Are always automatically raised whenever related error occurs.
The most common errors that can occur during the execution
of PL/SQL. Not declared explicitly i.e. cursor already open,
invalid cursor, no data found, zero divide and too many rows
etc. Programs are handled by system defined Exceptions.
2) User defined exception:
It must be declare by the user in the declaration part of the
block where the exception is used. It is raised explicitly in
sequence of statements using:
Raise_application_error (Exception_Number, Error_Message);

3. Explain any four aggregate functions with example.


An aggregate function is a function where the values of
multiple rows are grouped together as input on certain criteria
to form a single value of more significant meaning.
Aggregate functions are:
1) Count()
2) Sum()
3) Avg()
4) Min()
5) Max()

1. Count () –
1) It returns number of rows from the given table if no
attribute is mentioned.
2) If some attribute is mentioned, it gives total number of not
null values for that attribute.
Eg :Select count(*) from emp;
Returns total number of records from emp table.
1) Select count(telephone) from emp;
Returns total number of employees having telephone
numbers.

2. Sum() –
It gives total of all values from a numeric attribute of the given
table.
Eg: Select sum(salary) from emp;
Returns total salary drawn of all employees from the emp
table.
3. Avg () –
It gives average of all the numeric values of the given attribute
from the table.
Eg: Select Avg(salary) from emp;
Returns average salary of employees from emp table.
4. Min () –
It gives minimum of all the values of the numeric given
attribute from the table.
Eg: Select Min(salary) from emp;
Returns minimum salary value from emp table.
5. Max () –
It gives maximum of all the values of the numeric given
attribute from the table.
Eg: Select Max(salary) from emp;
retunes maximum salary value from emp table.

4. Write and explain create, update and drop view


commands.
1.A view contains rows and columns, just like a real table.
2.The fields in a view are fields from one or more real tables in
the database.
View has two types:
1. Simple view: The fields in a view are fields from one table in
the database.
2. Complex view: The fields in a view are fields from more than
one table in the database.
CREATE VIEW Syntax
Create view view_name As
Select column1, column2…
From table_name
Where condition;

where view_name is the name of the SQL VIEW that you wish
to create.

For e.g.

1)Create view stud_vu as Select * from student;


Here stud_vu is a view name for student.

2)Create view stud_vu1 as Select Rno, Name, Marks from


student;
Here stud_vu1 is a view name for student.
Updating views:
Updates the view by making changes to the data if the main
table allows it.
Syntax: Update Viewname Set Attribute = ’expression/ Value’
Where
Condition;

For e.g.
Update stud_vu set name=’ABC’ where rno=5;

Dropping Views
1.Deletes a view from the database.
2.Views can be dropped or destroyed using drop view
command.
Syntax: Drop View Viewname;
For e.g. Drop View stud_vu;

5. Describe ACID properties of transaction.


A transaction can be defined as a group of tasks. A single task is
the minimum processing unit which cannot be divided further.
ACID Properties
A transaction in a database system must maintain Atomicity,
Consistency, Isolation, and Durability − commonly known as
ACID properties − in order to ensure accuracy, completeness,
and data integrity.
ACID Properties of Transaction:
1. Atomicity
2. Consistency
3. Isolation
4. Durability
1. Atomicity:
1.Atomicity means all the operations included in the single
transaction gets executed at a time or none.
2.This property states that a transaction must be treated as an
atomic unit, that is, either all of its operations are executed or
none.
2. Consistency:
1.Consistency means update or edits the same data stored at
different locations.
2. The database must remain in a consistent state after any
transaction.
3. Isolation:
1.Isolation means all the transactions gets executed
independent of each other.
2. No transaction will affect the existence of any other
transaction.
4. Durability:
1.Durability means data can be saved in database permanently
until user change it.
2. The database should be durable enough to hold all its latest
updates even if the system fails or restarts.
6. Consider schema of book table as Book_Master (book_id,
book_name, author, no_of_copies, price)
Write down the SQL queries for the following.
i) Write a query to create table Book_Master table.
ii) Write a command to create composite index on
Book_Master table.
iii) Write a command to drop above index.
iv) Display book name and author in decreasing order of
price.
v) Display all books whose number of copies are greater
than 50.
vi) Create synonym for relation Book_Master as
Book_Information

1.Create Book_Master table:


CREATE TABLE Book_Master (
book_id NUMBER PRIMARY KEY,
book_name VARCHAR (100),
author VARCHAR (100),
no_of_copies NUMBER,
price NUMBER (10, 2)
);

2.Create composite index on Book_Master:


CREATE INDEX idx_book ON Book_Master (book_name,
author);

3. Drop the above index:


DROP INDEX idx_book;

4.Display book name and author in decreasing order of


price:
SELECT book_name, author FROM Book_Master ORDER
BY price DESC;

5.Display books with more than 50 copies:


SELECT * FROM Book_Master WHERE no_of_copies >
50;

6.Create synonym for Book_Master:

CREATE SYNONYM Book_Information FOR Book_Master;

7. Explain the most common types of database failures

Transaction Failures: Transactions can fail for a number of


reasons. Failure can be due to an error in the transaction
caused by incorrect input data as well as the types of detection
of a present or potential deadlock. Furthermore, some
concurrency failure, control algorithms do not permit a
transaction to proceed or even to wait if each data that they
attempt to access are currently being accessed by another
transaction. This might also be considered a failure.

1. System Failures (or Hardware/ Software failure): The


reasons for system failure can be traced back to a hardware or
to a software failure.
2. Media Failures: Media failure refers to the failures of the
secondary storage devices that store the database. Such
failures may be due to operating system errors, as well as to
hardware faults such as head crashes or controller failures.

3. Communication/Network Failures: There are a number of


types of communication failures. The most common ones are
the errors in the messages, improperly ordered messages, lost
messages, and communication line failures mainly arising due
to errors in network.

8. Describe Grant and Revoke commands with example


Grant: This command is used to give permission to user to do
operations on the Each other user s object.
Syntax: Grant <object privileges> on <object name> to
<User/Public/Role> [with grant option];
Example: Grant select, update on emp to user1;
Revoke: This command is used to withdraw the privilege that
has been granted to a user.
Syntax: Revoke <object privileges> on <object name> from
<User/Public/Role>;
Example: Revoke select, update on emp from user1;

9. Difference between Procedure and Function.


10. Write PL/SQL code to print largest number from three
Numbers (Accept 3 numbers from user)
DECLARE
num1 NUMBER;
num2 NUMBER;
num3 NUMBER;
largest NUMBER;
BEGIN
-- Accepting input from the user
DBMS_OUTPUT.PUT_LINE('Enter first number:');
num1 := &num1;

DBMS_OUTPUT.PUT_LINE('Enter second number:');


num2 := &num2;

DBMS_OUTPUT.PUT_LINE('Enter third number:');


num3 := &num3;

-- Finding the largest number


IF num1 >= num2 AND num1 >= num3 THEN
largest := num1;
ELSIF num2 >= num1 AND num2 >= num3 THEN
largest := num2;
ELSE
largest:= num3;
END IF;
-- Printing the largest number
DBMS_OUTPUT.PUT_LINE('The largest number is: ' |largest);
END;
/
11. Explain pattern matching operators ‘Like’ and range
searching operator ‘Between’ with example.
Types of SQL operators:
1) SQL Arithmetic Operators
2) SQL Comparison Operators
3) SQL Logical Operators
Arithmetic operators are used to perform arithmetic
operations on numbers. They are +,-,*, / and %.
Comparison operators are used in between two variables to
compare their values. They are ,<=,>=,=,!=or <>,!< and !>.'
Logical operators are used for the Boolean results in sql queries
for comparison of values from the attributes of the tables. Eg:
Any, Exists, All, Like, Between, In etc.
Between operator: The BETWEEN operator is used to search
for values that are within a set of values, given the minimum
value and the maximum value inclusive of both the limits.
Eg: select * from emp where salary between 40000 and 50000;
This will result in rows from emp table where salary falls in the
range of 40000 to 50000.
Like operator: The LIKE operator is used to compare a value to
similar values using wildcard operators. It uses two wild
characters as ‘%’ and ‘_’ where ‘%’ represents all characters of
the pattern and ‘_’ represents one single character from
pattern.
Eg: Select ename from emp where ename like ‘S%’;
This will return all employee names starting with ‘S’.
Select ename from emp where ename like ‘_a%;
This will return all employee names whose second character is
‘a’.

2 marks
1. Draw the block structure of PL-SQL.
2. State syntax of while loop command
Syntax:
WHILE<condition>
Loop statements;
END loop;

3. List any four string functions in SQL


Initcap(String) – converts first character of string to upper case
Upper(String) – converts the string to upper case
Lower(String) – converts string to lower case
Length(String) – returns the number of characters in the string
Replace(String, char,char) – replace all occurrence of a
substring by another substring
Substring(String,number) – extracts substring from the string

4. Enlist types of SQL Joins


1) INNER JOIN or EQUI JOIN: A join which is based on equalities
is called equi join. In equi join comparison operator “=” is used
to perform a Join.
2) SELF JOIN: The SQL SELF JOIN is used to join a table to itself,
as if the table were two tables, temporarily renaming at least
one table in the SQL statement.
3) LEFT OUTER JOIN: A left outer join retains all of the rows of
the “left” table, regardless of whether there is a row that
matches on the “right” table.
4) RIGHT OUTER JOIN: A right outer join retains all of the rows
of the “right” table, regardless of whether there is a row that
matches on the “left” table.
5) NON EQUI JOIN: Non equi joins is used to return result from
two or more tables where exact join is not possible.

5. List the different types of database user.


There are four different types of database-system users:

1.Naive Users

2. Sophisticated Users

3. Application Programmers

4. Database Administrator (DBA)


6. Define cursor and its types.
Cursor:
1.The Oracle Engine uses a work area for its internal processing
in order to execute an SQL statement. This work area is private
to SQL‟s operations and is called a Cursor.
2.A cursor is a temporary work area created in the system
memory when a SQL statement is executed.
Types of cursor are:
1) Implicit cursor
2) Explicit cursor

7. State use of i) Commit and ii) Rollback commands


Commit: The COMMIT command is used to saves all changes
made during the current transaction to the database
permanently.
Syntax: COMMIT;
ROLLBACK: The ROLLBACK command is used to undo changes
made during the current transaction, reverting the database to
its previous state.
Syntax: ROLLBACK;

8. List system privileges and object privileges.


System Privileges:
CREATE SESSION
CREATE TABLE
CREATE VIEW
CREATE PROCEDURE
ALTER USER
DROP USER
GRANT ANY PRIVILEGE
CREATE ROLE
ALTER SYSTEM

Object Privileges:
SELECT
INSERT
UPDATE
DELETE
EXECUTE
REFERENCES
INDEX
9. Write a syntax of function in pl/sql
Syntax:
sql > Create Function Function_name [(Parameter1 [mode1]
data type, ...)]
Return data type is
Variable Declarations;

Begin
Executable code;

[Exception
Exception section]
End;
/

10. State any two advantages of functions in PL/SQL.


Advantages of functions in PL/SQL are:
 Work can be divided into smaller modules so that it can
be manageable and also enhances the readability of the
code.
 It promotes reusability.
 It is secure, as the code is in the database and hides the
internal database details from the user.
 It improves performance against running SQL queries
multiple times.
 Memory Allocation
 Productivity, Integrity.

You might also like