Model Answer Winter 2019
Model Answer Winter 2019
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
to a data element. reached only through a data from result set on row at a time. product_name,rate)
parent Implicit cursor: these types of cursors are generated and used by the Now the above two tables are in 2NF
(b) Explain any four set operators in SQL with example. 4M system during the manipulation of a DML query. An implicit cursor Each Step 2: To convert the above tables in 3NF, we have to
Ans. Set operators combine the results of two component queries into a is also generated by the system when a single row is selected by a explanat decomposehem in three tables satisfying the transitive dependencies
single result. Queries containing set operators are called as compound SELECT command. ion 2M property.
queries. Set operators in SQL are represented with following special Programmers cannot control the implicit cursors. Table 4: Supplier Details
keywords as: Union, Union all, intersection & minus. (Supplier_no,Supplier_name,Supplier_city)
Explicit cursor: this type of cursor is created by the user when the Table 5: Product Details:
1) Union: The Union of two or more sets contains all elements, 1M each
select command returns more than one row, and only one row is to be (Product_code, product_name,rate)
which are present in either or both. Union works as or. The duplicates for
processed at a time. An explicit cursor can move from one row to Table 6: Order Details (or Transaction Details)
of both the tables will appear only once. explanat
another in a result set. An explicit cursor uses a pointer that holds the ((Order_no,Supplier_no,Product_code,Order_quantity,Order_amount
E.g. select ename from emp1 union select ename from emp2; ion of
record of a row. )
2) Union all: The Union of 2 or more sets contains all elements, operator
To create an explicit cursor the following steps are used. Hence the above three tables are satisfying Transitive dependencies.
which are present in both, including duplicates. s with
1. Declare cursor: this is done in the declaration section of PL/SQL Thus they are in 3NF.
E.g. select ename from emp1 union all select ename from emp2; example
program. (b) Define index. Explain it‟s types. 4M
3) Intersection: The intersection of two sets includes elements which
2. Open: this step is done before the cursor is used to fetch the Ans. An Index is a schema object. It is used by the oracle server to
are present in both.
records. improve the speed of retrieval of the rows from a table .Indexes are of Definitio
E.g. select ename from emp1 intersect select ename from emp2;
3. Fetch: used to retrieve data row by row from the cursor. two types based on number of columns included in the index. n 1M
4) Minus: The minus of two sets includes elements from set1 minus
4. Close: once the processing of the data is done, the cursor can be
elements of set2.
closed. The types of index are:
E.g. select ename from emp1 minus select ename from emp2;
3. Attempt any THREE of the following: 12 1) Simple index: An index created on a single column of table is
(c) Describe Views and write a command to create view. 4M
(a) State and explain 3NF with example. 4M called as simple index
Ans. A view is a virtual table based on the result set of the SQL statement.
The fields in a view are fields from one or more than one table in the Ans. 3NF: Syntax: Each
An entity is said to be in the third normal form when, SQL>Create Index index_name on tablename(attribute); type
database. SQL functions, where, join statements can be added to a
1) It satisfies the criteria to be in the second normal form. Example:Create index emp_index on emp(empno); 1½M
view and the data in it can be presented as if it were from one table.
2) There exists no transitive functional dependency. (Transitive Explana
The database engine recreates the data, using the view’s SQL
functional dependency can be explained with the relationship link tion 2M 2) Composite Index: An index created on more than one column is
statement, every time a user queries a view. A view can be updated Explana
between three tables. If table A is functionally dependent on B, and B called composite index.
using the create or replace view command. For deleting a view, drop tion 3M
is functionally dependent on C then C is transitively dependent on A). Syntax:
query can be used.
SQL>Create Index index_name on
Let us consider the Schema given:
tablename(attribute1,attribute2);
General syntax to create a view: (Supplier_no,SupplierName,Supplier_city,Order_no,Order_quantity,
General Example: Create index emp_index on emp(empno,ename);
create view viewname as select query. Order_amount,Product_code,Product name,rate)
syntax/ Step 1.To convert it into 2NF, We have to decompose the given table (c) Explain Exception handling with it‟s types. 4M
Eg: Any Ans. An exception is an error condition during a program execution.
create view vw_student as select stud_id, stud_name,ssc_per from example into two tables with fully functional dependencies and establishing a
example PL/SQL supports programmers to catch such conditions Explana
student; 1M referential integrity constraint relationship among the two tables.
2M using EXCEPTION block in the program and an appropriate action tion 2M
(d) Explain implicit and explicit cursors. 4M Table2: Supplier Details
(Supplier_no,Supplier_name,Supplier_city) is taken against the error condition.
Ans. A cursor is a temporary work area created in system memory when an There are two types of exceptions −
SQL statement is executed. A cursor is a set of rows together with a Table 3:Order Details
1) System-defined exceptions/Predefined exceptions/Built-in
pointer that identifies a current row. It is a database object to retrieve (Order_no,Order_ quantity,Order_amount,Supplier_no Product_code,
exceptions
2) User-defined exception (d) Explain ACID properties of traction. 4M called as Strong entity set.
1) Predefined exceptions- PL/SQL provides predefined Exception, Ans. A transaction can be defined as a group of tasks. A single task is the Example: Employee is a Strong entity with attributes as empid, name,
which are executed when any database rule is violated by a program. minimum processing unit which cannot be divided further. address, salary, birthdate among which empid can be considered as
Example: NO_DATA_FOUND, ZERO_DIVIDE. primary key.
Syntax for Predefined Exception Handling: ACID Properties
Types A transaction is a very small unit of a program and it may contain Weak entity set: Each
The general syntax for exception handling is as follows. The entity set which does not have sufficient attributes to form a entity set
2M several lowlevel tasks. A transaction in a database system must Explana
DECLARE maintain Atomicity, Consistency, Isolation, and Durability − tion primary key is called as Weak entity set. 2M
<declarations section> commonly known as ACID properties − in order to ensure accuracy, of each A weak entity is an entity that cannot be uniquely identified by its
BEGIN completeness, and data integrity. property attributes alone; therefore, it must use a foreign key in conjunction
<executable command(s)> 1M with its attributes to create a primary key. The foreign key is typically
Atomicity: This property states that a transaction must be treated a primary key of an entity it is related to.
EXCEPTION
<exception handling goes here > as an atomic unit, that is, either all of its operations are executed Example: Employee has "dependents" with name, birthdate, and
WHEN exception1 THEN or none. There must be no state in a database where a transaction relationship to employee and it can be related to employee with the
exception1-handling-statements is left partially completed. States should be defined either before help of empid, so "dependents" is a weak entity which depends on
WHEN exception2 THEN the execution of the transaction or after the strong entity "Employee".
exception2-handling-statements execution/abortion/failure of the transaction. (b) Describe create & alter command with syntax & example. 4M
WHEN exception3 THEN Consistency: The database must remain in a consistent state after Ans. 1) The SQL CREATE TABLE statement is used to create a new
exception3-handling-statements any transaction. No transaction should have any adverse effect on table.
........ the data residing in the database. If the database was in a
WHEN others THEN consistent state before the execution of a transaction, it must Syntax
exception3-handling-statements remain consistent after the execution of the transaction as well. The basic syntax of the CREATE TABLE statement is as follows –
END;
Isolation: In a database system where more than one transaction CREATE TABLE table_name Each
2) User defined Exceptions:
PL/SQL allow us to define our own exception according to the need are being executed simultaneously and in parallel, the property of comman
isolation states that all the transactions will be carried out and (
of our program. A user defined exception must be declared and then d 2M
executed as if it is the only transaction in the system. No column1 datatype (size),
raised explicitly.
transaction will affect the existence of any other transaction. column2 datatype(size),
column3 datatype(size),
Syntax for User defined Exception: Durability: The database should be durable enough to hold all its ....
DECLARE latest updates even if the system fails or restarts. If a transaction );
exception_name EXCEPTION; updates a chunk of data in a database and commits, then the
BEGIN database will hold the modified data. If a transaction commits but Example:
IF condition THEN the system fails before the data could be written on to the disk, CREATE TABLE Persons
RAISE exception_name; then that data will be updated once the system springs back into (
END IF; action. PersonIDnumber(10),
EXCEPTION LastNamevarchar2(20),
WHEN exception_name THEN 4. Attempt any THREE of the following: 12 FirstNamevarchar2(20),
statement; (a) Explain strong and weak entity set. 4M Address varchar2(20),
END; Ans. Strong entity set: City varchar2(20)
An entity set that has sufficient attributes to form a primary key is
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
); (c) Define database trigger. How to create and delete trigger? 4M The conditional selection statements are IF and CASE.
Ans. Triggers are stored programs, which are automatically executed or Loop statements, which run the same statements with a series of
2)The ALTER TABLE statement is used to add, delete, or modify fired when some events occur. Triggers are, in fact, written to be Definitio different data values.
columns in an existing table. executed in response to any of the following events − n 1M Explana
A database manipulation (DML) statement (DELETE, The loop statements are the basic LOOP, FOR LOOP, tion of
The ALTER TABLE statement is also used to add and drop various INSERT, or UPDATE) and WHILE LOOP. Any one
constraints on an existing table. A database definition (DDL) statement (CREATE, ALTER, or control
DROP). The EXIT statement transfers control to the end of a loop. structur
i) To add Columns in a table Triggers can be defined on the table, view, schema, or database with The CONTINUE statement exits the current iteration of a loop and e 4M
which the event is associated. transfers control to the next iteration.
Syntax:ALTER TABLE table_name Both EXIT and CONTINUE have an optional WHEN clause, where
ADD column_name datatype; you can specify a condition.
Creating Triggers
Example The syntax for creating a trigger is −
ALTER TABLE Customers Sequential control statements, which are not crucial to PL/SQL
CREATE [OR REPLACE ] TRIGGER trigger_name
ADD Email varchar2(20); programming.
{BEFORE | AFTER | INSTEAD OF } Create
ii) To delete a column in a table {INSERT [OR] | UPDATE [OR] | DELETE} 2M The sequential control statements are GOTO, which goes to a
[OF col_name]
ALTER TABLE table_name specified statement, and NULL, which does nothing.
ON table_name
DROP COLUMN column_name;
[REFERENCING OLD AS o NEW AS n]
1)Conditional Control: IF and CASE Statements:
Example [FOR EACH ROW]
The IF statement lets us execute a sequence of statements
ALTER TABLE Customers WHEN (condition)
conditionally. That is, whether the sequence is executed or not
DROP COLUMN Email; DECLARE
depends on the value of a condition. There are three forms
Declaration-statements
iii) To modify a column in a table of IF statements: IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSIF.
BEGIN
The CASE statement is a compact way to evaluate a single condition
Syntax: Executable-statements
and choose between many alternative actions.
ALTER TABLE table_name EXCEPTION
MODIFY COLUMN column_name datatype; Exception-handling-statements
END; IF-THEN Statement:The simplest form of IF statement associates a
Example condition with a sequence of statements enclosed by the
ALTER TABLE Customers keywords THEN and END IF (not ENDIF), as follows:
MODIFY COLUMN customeridnumeric(10); To delete a trigger: Delete
Syntax: IF condition THEN
iv) To add Constraints in A table 1M
DROP TRIGGER trigger_name. sequence_of_statements
Syntax: (d) Explain any one control structure in PL/SQL with example. 4M END IF;
ALTER TABLE table_name Ans. PL/SQL has three categories of control statements: conditional
ADD constraint constraintname (column_name); selection statements, loop statements and sequential control IF-THEN-ELSE Statement: The second form of IF statement adds
Example: statements. the keyword ELSE followed by an alternative sequence of statements,
ALTER TABLE Customers PL/SQL categories of control statements are: as follows:
ADD constraint primary key(CustomerID); Conditional selection statements, which run different statements IF condition THEN
for different data values. sequence_of_statements1
Page 10 / 21 Page 11 / 21 Page 12 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
ELSE infinite) loop, which encloses a sequence of statements between the ...
sequence_of_statements2 keywords LOOP and END LOOP, as follows: <<insert_row>>
END IF; LOOP INSERT INTO empVALUES ...
sequence_of_statements END;
IF-THEN-ELSIF Statement: The third form of IF statement uses END LOOP; (e) Describe database backups with it‟s types. 4M
the keyword ELSIF (not ELSEIF) to introduce additional conditions, Ans. Regular backups are required to protect database and
as follows: WHILE-LOOP:The WHILE-LOOP statement associates a condition ensure its restoration in case of failure. Various backup types provide Descript
IF condition1 THEN with a sequence of statements enclosed by the different protection to our database. Backing up and restoring data is ion 2M
sequence_of_statements1 keywords LOOP and END LOOP, as follows: one of the most important responsibilities of IT professionals
ELSIF condition2 THEN WHILE condition LOOP Three common types of database backups can be run on a desired
sequence_of_statements2 sequence_of_statements system: normal (full), incremental and differential.
ELSE END LOOP;
sequence_of_statements3 i) Normal or Full Backups:
END IF; Before each iteration of the loop, the condition is evaluated. If the When a normal or full backup runs on a selected drive, all the files on
condition is true, the sequence of statements is executed, then control that drive are backed up. This, of course, includes system files,
CASE Statement: Like the IF statement, the CASE statement selects resumes at the top of the loop. If the condition is false or null, the application files, user data — everything. Those files are then copied
one sequence of statements to execute. loop is bypassed and control passes to the next statement. to the selected destination (backup tapes, a secondary drive or the Types
IF grade = 'A' THEN cloud), and all the archive bits are then cleared. 2M
dbms_output.put_line('Excellent'); FOR-LOOP:Whereas the number of iterations through
ELSIF grade = 'B' THEN a WHILE loop is unknown until the loop completes, the number of Normal backups are the fastest source to restore lost data because all
dbms_output.put_line('Very Good'); iterations through a FOR loop is known before the loop is the data on a drive is saved in one location.
ELSIF grade = 'C' THEN entered. FOR loops iterate over a specified range of integers. The
dbms_output.put_line('Good'); range is part of an iteration scheme, which is enclosed by the ii) Incremental Backups:
ELSIF grade = 'D' THEN keywords FOR and LOOP. A double dot (..) serves as the range A common way to deal with the long running times required for
dbms_output. put_line('Fair'); operator. The syntax follows: full backups is to run them only on weekends. Many businesses then
ELSIF grade = 'F' THEN FOR counter IN [REVERSE] lower_bound..higher_bound LOOP run incremental backups throughout the week since they take far less
dbms_output.put_line('Poor'); sequence_of_statements time. An incremental backup will grab only the files that have been
ELSE END LOOP updated since the last normal backup. Once the incremental
dbms_output.put_line('No such grade'); backup has run, that file will not be backed up again unless it changes
END IF; 3)Sequential Control: GOTO and NULL Statements: Unlike or during the next full backup.
the IF and LOOP statements, the GOTO and NULL statements
END CASE; are not crucial to PL/SQL programming.
iii) Differential Backups:
2) Iterative Control: LOOP and EXIT Statements: GOTO Statement
LOOP statements let us execute a sequence of statements multiple An alternative to incremental database backups that has a less
times. There are three forms of LOOP statements: LOOP, WHILE- complicated restore process is a differential backup. Differential
The GOTO statement branches to a label unconditionally. backups and recovery are similar to incremental in that these backups
LOOP, and FOR-LOOP. BEGIN grab only files that have been updated since the last normal backup.
... However, differential backups do not clear the archive bit. So a file
LOOP: The simplest form of LOOP statement is the basic (or GOTO insert_row;
Page 13 / 21 Page 14 / 21 Page 15 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
that is updated after a normal backup will be archived every time a SQL>Create table student( (ii) Display employee name & employee number whose
differential backup is run until the next normal backup runs and clears Rollno number(5), designation is Manager.
the archive bit. Stud_name char(10, Create (iii) Display age of employees with ename.
branch varchar(10), table 3M (iv) Display total salary of all employees.
5. Attempt any TWO of the following: 12 class varchar(10), (v) Display employee names having deptno as 20 and
(a) Draw an ER diagram for library management system. (Use 6M DOB date, dept_location is Mumbai
Books, Publisher & Member entities). city varchar(15), (vi) Display name of employee who earned lowest salary.
(Note: Consider any relevant diagram) Contact_no number(12) Ans.
Ans. ); (i)Display employees name &number in descending order of salary:
SQL> select ename,empno from EMP order by salary desc;
(i) Insert one row into the table:
Correct SQL>Insert into student values(1,‟Ram‟,‟CO‟,‟FirstYear‟,‟12- (ii) Display employee name & employee number whose designation
entities jun-2001‟,‟Pune‟,98576867) is Manager. Each
2M Each SQL> select ename,empno from EMP where correct
(ii) Save the data: correct designation=‟Manager‟; Query
Correct SQL> commit; Query 1M
symbols ( OR ) ½M (iii) Display age of employees with ename
2M SQL> commit work; each SQL>select round ( (sysdate - DOB ) /365, 0 ) as “age”,ename
from EMP;
Correct (iii)Insert second row into the table: OR
relations SQL>Insert into student values(2,‟Raj‟,‟CO‟,‟FirstYear‟,‟22-Sep- select months_between(TRUNC(sysdate),DOB)/12 as “age”
hips 2M 2002‟,‟Mumbai‟,98896863) ,ename from EMP ;
(**Note consider any other logic also)
(iv)Undo the insertion of second row:
SQL> rollback; (iv)Display total salary of all employees.
( OR) SQL> select sum(salary) from EMP;
SQL> rollback work;
(v)Display employee names having deptno as 20 and dept_location is
(v)Create savepoint s1: Mumbai.
(b) Write a command to crate table student (rollno, Stud_name, 6M SQL>Savepoint s1; SQL> select enamefrom EMP where deptno=20 and
branch, class, DOB, City, Contact_no) and write down queries dept_location=‟Mumbai‟;
for following: (vi) insert one row into the table:
(i) Insert one row into the table SQL>Insert into student values(3,‟Beena‟,‟CO‟,‟FirstYear‟,‟30- (vi)Display name of employee who earned lowest salary
(ii) Save the data Dec-2002‟,‟Mumbai‟,97846455) SQL> select ename from EMP where salary=(select min(salary)
(iii) Insert second row into the table (c) Consider following schema: 6M from EMP);
(iv) Undo the insertion of second row EMP (empno, deptno, ename, salary, designation, join_date, 6. Attempt any TWO of the following: 12
(v) Create save point S1. DOB, dept_location). Write down SQL queries for following: (a) Consider the structure for book table as Book-Master (bookid, 6M
(vi) Insert one row into the table. (i) Display employees name & number in decreasing order of bookname, author, no_of copies, price)
Ans. salary. Write down SQL queries for following:
(i) Write a command to create Book_master table. Ans. declare been granted to a user. Procedu
(ii) Get authorwise list of all books. num number; Correct Syntax: Revoke <object privileges>on<object name>from re for
(iii) Display all books whose price is between 500 & 800. n number:=&n; logic 3M <username> ; revoking
(iv) Display all books with details whose name start with „D‟. begin Example: Revoke select, update on emp from user1; privilege
(v) Display all books whose price is above 700. for num in 1..n loop s 2M
(vi) Display all books whose number of copies are less than 10. if(mod(num,2)=0) then
Ans. dbms_output.put_line(‘Even no are :’||num); Correct
(i)Write a command to create Book_Master table table. end if; syntax
SQL>Create table Book-Master( end loop; 3M
bookid number(5), end;
bookname char(10), (c) Describe database privileges. Write down the procedure for 6M
authorvarchar(20), Each granting & revoking privileges in database objects to the users.
no_of_copiesnumber(10), correct Ans. Database privileges:
price number(10,2) Query When multiple users can access database objects, authorization can
); 1M be controlled to these objects with privileges. Every object has an
owner. Privileges control if a user can modify an object owned by Databas
(ii)Get authorwise list of all books. another user. Privileges are granted or revoked either by the instance e
SQL>Select sum(no_of copies) from Book_Master group by administrator, a user with the ADMIN privilege or, for privileges to a Privilege
author; certain object, by the owner of the object. s 2M
1) System Privileges:
(iii)Display all books whose price is between Rs.500 & Rs. 800 System privileges are privileges given to users to allow them to
SQL> Select * from Book_Master where price between 500 and perform certain functions that deal with managing the database and
800; the server
OR e.gCreate user, Create table, Drop table etc.
SQL> Select * from Book_Master where price >=500 and
price<=800; 2) Object Privileges:
Object privileges are privileges given to users as rights and
(iv) Display all books with details whose name start with ‘D’ restrictions to change contents of database object – where database
SQL> Select bookname from Book_Master where bookname like objects are things like tables, stored procedures, indexes, etc.
„D%‟; Ex. Select,insert,delete,update,execute,references etc
(v)Display all books whose price is above Rs. 700 Procdure for granting privileges
SQL>Select * from Book_Master where price >700; Grant: This command is used to give permission to user to do Procedu
operations on the other user’s object. re for
(vi) Display all books whose number of copies are less than 10 Syntax: Grant<object privileges>on<object granting
SQL>Select * from Book_Master where no_of_copies<10; name>to<username>[with grant option] ; privilege
(b) Write a PL/SQL program to print n even numbers using For 6M Example: Grant select, update on emp to user1; s
Loop. 2M
(Note: Any other logic can be allowed) Procedure for revoking privileges
Revoke: This command is used to withdraw the privileges that has