CCFP4 0-RDBMSAssignments
CCFP4 0-RDBMSAssignments
System
Version 4.0
COPYRIGHT NOTICE
© 2014 Infosys Limited, Bangalore, India. All Rights Reserved.
Infosys believes the information in this document is accurate as of its publication date; such
information is subject to change without notice. Infosys acknowledges the proprietary rights of
other companies to the trademarks, product names and such other intellectual property rights
mentioned in this document. Except as expressly permitted, neither this documentation nor
any part of it may be reproduced, stored in a retrieval system, or transmitted in any form or by
any means, electronic, mechanical, printing, photocopying, recording or otherwise, without the
prior permission of Infosys Limited and/ or any named intellectual property rights holders
under this document.
Monika Verma,
Kiran R.K., Lakshmi
1.0 Jan 2014 Satyendra Singh, Initial Draft
D.L.
Diwakar Jaiswal
Monika Verma,
1.1 Feb 2014 Kiran R.K. As per feedback comments
Lakshmi D.L.
Monika Verma, Incorporated additional
2.0 May 2014 Kiran R.K.
Manjeet Singh Juneja assignments
CONFIDENTIAL
C
ONTENTS
COPYRIGHT NOTICE..............................................................................................................................................................i
Document Revision History.......................................................................................................................................................ii
Customization Revision History ................................................................................................................................................ii
CONTENTS .............................................................................................................................................................................iii
Assignments on Database basics ........................................................................................................................................... 1
Assignment 1: Relational model: Keys - Guided activity...................................................................................................... 1
Assignment 2: Identifying data items - Guided activity........................................................................................................ 2
Assignment 3: ER modeling - Terms and notations - Guided activity ................................................................................. 4
Assignment 4: ER model - Guided activity........................................................................................................................... 6
Assignment 5: Conversion ER model to Relational schema - Guided activity...................................................................... 7
Assignment 6: Functional dependency - Guided activity...................................................................................................... 8
Assignment 7: Identify highest normal form - Guided activity............................................................................................ 8
Assignments on SQL basics ................................................................................................................................................ 11
Assignment 1: CREATE and INSERT - Demo ................................................................................................................... 11
Assignment 2: DDL - CREATE table - Demo .................................................................................................................... 13
Assignment 3: DML - Demo ............................................................................................................................................... 18
Assignment 4: DDL - ALTER table - Demo ...................................................................................................................... 19
Assignment 5: DEFAULT - Guided Activity ..................................................................................................................... 22
Assignment 6: SELECT - Demo.......................................................................................................................................... 23
Assignment 7: DML - Guided activity................................................................................................................................ 25
Assignment 8: DISTINCT, ORDER BY - Demo ................................................................................................................ 26
Assignment 9: DISTINCT, ORDER BY - Guided activity................................................................................................. 27
Assignment 10: CASE statement - Demo............................................................................................................................ 28
Assignment 11: CASE statement - Guided Activity ........................................................................................................... 29
Assignment 12: SQL functions - Demo............................................................................................................................... 30
Assignment 13: SQL functions - Guided activity ............................................................................................................... 33
Assignments on GROUP BY and HAVING ....................................................................................................................... 35
Assignment 1: GROUP BY and HAVING - Guided activity ............................................................................................. 35
Assignments on Joins .......................................................................................................................................................... 35
Assignment 1a: INNER JOIN - Guided activity ................................................................................................................. 35
Assignment 1b: INNER JOIN - Guided activity................................................................................................................. 37
Assignment 2a: LEFT OUTER JOIN - Guided activity ..................................................................................................... 39
Assignment 2b: LEFT OUTER JOIN - Guided activity ..................................................................................................... 39
Assignment 3: RIGHT OUTER JOIN - Guided activity..................................................................................................... 39
Assignment 4: Self-Join - Guided activity .......................................................................................................................... 40
Assignments on Sub queries ................................................................................................................................................ 40
CONFIDENTIAL
Assignment 1: Independent sub query - Single row and Multi row -
Guided activity ........................................................ 40
Assignment 2: Multiple columns sub query - Demo ........................................................................................................... 42
Assignment 3: Multiple columns sub query - Guided activity............................................................................................. 43
Assignment 4: Correlated sub query - Guided activity ....................................................................................................... 44
Assignment 5: Exists/Not Exists - Guided activity ............................................................................................................ 45
Assignments on Index.......................................................................................................................................................... 45
Assignment 1: Index - Guided activity ................................................................................................................. 45
Assignments on SQL Best practices .................................................................................................................................... 46
Assignment 1: Best practice tips for writing SQL queries ............................................................................ 46
Assignments on Views......................................................................................................................................................... 49
Assignment 1: Views - Demo.................................................................................................................................... 49
Assignment 2: Views - Guided activity ................................................................................................................ 49
Assignments on Transaction and Locks............................................................................................................................... 50
Assignment 1: Concurrency issues - Self Study ................................................................................................................. 50
Assignment 2: Locks - Guided activity............................................................................................................................... 53
Assignments on PL/SQL ..................................................................................................................................................... 54
Assignment 1: PL/SQL block - Guided activity.................................................................................................................. 54
Assignment 2: Usage of BOOLEAN data type - Guided activity ....................................................................................... 56
Assignment 3: Usage of SQL% Attributes in PL/SQL - Guided activity ........................................................................... 57
CONFIDENTIAL
Infosys Limited Assignments for RDBMS
The relations along with their attributes are mentioned below. Identify the
candidate key, primary key and foreign key(s) for these relations:
a) item (itemcode, itemtype, description, price, reorderlevel, quantityonhand,
category)
b) supplier (supplierid, suppliername, suppliercontactno, supplieremailid)
c) quotation (quotationid, supplierid, itemcode, quotedprice, quotationdate,
quotationstatus)
Objective: Given the case study, identify the data items required
Problem description:
1. Consider the EasyShop application. Identify all the data items required
for creating the database.
Refer CCFP4.0-RDBMS-EasyShop Retail Application Case Study.docx
Summary of this assignment: Identification of the data items for a given case
study
employee
Degree of relationship:
Number of entities involved in the relationship
Cardinality of relationship:
The number of instances of one entity is related to the number of instances of
another entity.
Cardinality is represented by placing the appropriate number along with the
relationship
Cardinality of relationship Example
one to one (1:1)
Employee
one to many (1:M)
many to one (M:1)
Employee
many to many (M:N)
Note: ERD is usually viewed from left to right and from top to bottom.
M N
1 M Belongs
Branch Account Customer
Has to
Problem Description:
1. Consider the following relation and the given functional dependencies,
identify the candidate key and type of functional dependency
Relation:
item (itemid, marketingid, vendor, style, price)
Functional dependencies:
{itemid, marketingid} price
itemid vendor, style
Functional dependencies:
Problem Description:
1. Find out the candidate key(s) and the highest normal form in the given relation:
Suggested Solution:
Step 1: Identify the candidate key, with the help of given functional dependencies.
As per the first functional dependency, let us assume traineeid is a candidate key.
To confirm that, traineeid is a candidate key, it should determine rest of the
attributes.
The given two functional dependencies shows that, we can determine rest of the
attributes (i.e. traineename, classroomid and pcid} with the help of traineeid.
traineeid traineename
traineeid classroomid, pcid
Hence, traineeid is a candidate key. But as we know, a relation can have more
than one candidate key. Let's check, if we have another candidate key in the given
relation.
Infosys Limited
Assignments for
supplierna RDBMS
me
suppliercontact VARCHAR
2(30) Name of the
no supplier
VARCHAR
supplieremai Contact number of
2(12)
lid supplier
VARCHAR
Email id of
2(30)
supplier
CREATE TABLE supp
lier Data type
(size)
(supplierid VARCHAR2(6
),
C suppliername VARCHAR2
olumn (30),
suppliercontactno VARCHAR2(
name
12),
supplieremailid VARCHAR2(
30)
)
;
2. Insert a record in the
supplier table
INSERT INTO
supplier
(supplierid, suppliername,
suppliercontactno,
supplie
remaili
d)
V
A
L
U
E
S
Summary of this assignment: You have learnt to create table without any
constraints and insert a record into the table.
Problem description:
Step 2: Create supplier table, with "supplierid" column to have UNIQUE values
CREATE TABLE supplier(
supplierid VARCHAR2(6)
CONSTRAINT sup_sid_unq UNIQUE,
suppliername VARCHAR2(30),
suppliercontactno VARCHAR2(12),
supplieremailid VARCHAR2(30)
);
Best practice:
1) Every constraint must be named
2) The naming convention to be followed for a constraint:
<short table name>_<short column name>_<short
constraint type>
Example: sup_sid_unq
2. Create the table supplier where supplier id must not be left blank and it should
not repeat too
DROP TABLE supplier;
CREATE TABLE supplier (
supplierid VAR
A column can have
CHAR2(6) more than one
constraint
CONSTRAINT sup_sid_unq UNIQUE
CONSTRAINT sup_sid_nn NOT NULL,
suppliername VARCHAR2(30),
suppliercontactno VARCHAR2(12),
supplieremailid VARCHAR2(30)
);
CREATE TABLE supplier(
supplierid VARCHAR2(6)
CONSTRAINT sup_sid_pk PRIMARY KEY,
suppliername VARCHAR2(30),
suppliercontactno VARCHAR2(12),
supplieremailid VARCHAR2(30)
);
CREATE TABLE retailstock (
retailoutletid VARCHAR2(6),
itemcode VARCHAR2(6),
retailunitprice NUMBER,
quantityavailable NUMBER,
CONSTRAINT rtlstk_rid_icode_pk PRIMARY
KEY(retailoutletid, itemcode)
);
Note:
1) The composite primary key must be created as a table level
constraint
5. Create table supplier where the supplier id must start with 'S'
DROP TABLE supplier;
CREATE TABLE supplier
(supplierid VARCHAR2(6)
CONSTRAINT supl_sid_pk PRIMARY KEY
CONSTRAINT supl_sid_ck CHECK (supplierid LIKE 'S%'),
suppliername VARCHAR2(30),
suppliercontactno VARCHAR2(12),
supplieremailid VARCHAR2(30));
CREATE TABLE quotation (
quotationid VARCHAR2(6) CONSTRAINT quot_qid_pkey PRIMARY
KEY,
supplierid VARCHAR2(6)
CONSTRAINT quot_sid_fk REFERENCES supplier(supplierid),
itemcode VARCHAR2(10),
quotedprice NUMBER,
quotationdate DATE,
quotationstatus VARCHAR2(10)
);
Problem description:
1. Include a row in the supplier table
INSERT INTO supplier
VALUES ('S1009','M Stores','103‐237‐2017', '[email protected]');
UPDATE supplier
SET suppliercontactno = '303‐537‐9127'
WHERE supplierid='S1009';
DELETE FROM supplier
WHERE supplierid='S1009';
Problem description:
1. After creating the table supplier, EasyShop wants to store the supplier city
information also in the supplier table
ALTER TABLE supplier
ADD suppliercity VARCHAR2 (10);
2. After including the new supplier city column, EasyShop wants to increase the
column size to 20
ALTER TABLE supplier
MODIFY suppliercity VARCHAR2 (20);
Table: item
CREATE TABLE item(
itemcode VARCHAR2(6)
CONSTRAINT itm_icode_pk PRIMARY KEY,
itemtype VARCHAR2(30),
description VARCHAR2(255)
CONSTRAINT itm_desc_nn NOT NULL,
price NUMBER(10,4),
category CHAR(1)
);
ALTER TABLE quotation
ADD CONSTRAINT quot_icode_fk FOREIGN KEY (itemcode)
REFERENCES item (itemcode);
5. The category column of item table must have values, is a new requirement.
To do this, add NOT NULL constraint on category column by executing the
below SQL statement.
ALTER TABLE item
ADD CONSTRAINT itm_cat_nn NOT NULL (category);
Solution:
ALTER TABLE item
MODIFY category CONSTRAINT itm_cg_nn NOT NULL;
Note:
1) To modify the data type of column, the column should be empty.
2) If required, to add a constraint on existing column of a table, the data
present in that column must not violate the constraint that has to be added.
3) To change the column which can accept null values to not null, use
MODIFY clause of ALTER statement.
Note:
1. The default value specified will be used when no value is specified for the
column while inserting data
2. The default value specified in the definition should satisfy the data type
and length of the column
3. If a default value is not explicitly set, the default value for the column is
implicitly set to NULL
Example:
Inserting a row into the quotation table with the quotation status as a default
value:
INSERT INTO quotation VALUES('Q1001',1000, DEFAULT);
Inserting a row into the quotation table with quotation status is other than default
value:
INSERT INTO quotation VALUES('Q1002',1400, 'Accepted');
Note
• Refer the document "CCFP4.0-RDBMS-EasyShop Retail
Application_DB_Design.docx" to know the DB design of
EasyShop retail application.
Problem description:
OR
SELECT itemcode, itemtype, description, price, category
FROM item;
2. Retrieve the supplier name and supplier's contact number of the supplier
'S1002'.
SELECT suppliername, suppliercontactno
FROM supplier
WHERE supplierid = 'S1002';
3. Retrieve the quotation id and supplier id of the quotations which have been
SELECT quotationid, supplierid
FROM quotation
WHERE quotationstatus ='Accepted'
OR quotationstatus =' Rejected';
OR
SELECT quotationid, supplierid
FROM quotation
WHERE quotationstatus
IN ('Accepted', 'Rejected');
4. Retrieve supplier details like supplier id and supplier name whose names
have 'i' as the second character.
SELECT supplierid, suppliername
FROM supplier
WHERE suppliername LIKE '_i%';
5. Retrieve the order details like orderid, quotationid, status, paymentdate
for those orders where payments are not received.
SELECT orderid, quotationid, status, paymentdate
FROM orderstatus
WHERE amountpaid IS NULL;
6. Retrieve the quotation details like quotationid, quotationdate,
quotedprice for those quotations which are quoted in the range of 1400
and 2150.
SELECT quotationid, quotationdate, quotedprice
FROM quotation
WHERE quotedprice>=1400 AND quotedprice<=2150;
OR
SELECT quotationid, quotationdate, quotedprice
FROM quotation
WHERE quotedprice BETWEEN 1400 AND 2150;
7. The management of EasyShop wants to increase the salary of all
employees by 10%. Write a query to display the employee details along
with their increased salary.
SELECT empid, empname, designation, salary,
salary*1.10 "NewSal"
FROM employee;
Note:
The table creation scripts and the insertion scripts for the retail application
are available in "Easyshop_Retailapplication_Table_Creation_Script.txt".
Execute the SQL statements in this file and create the tables along with
data for practicing various SELECT statements
3. Retrieve the designation and salary of all 'Manager' and 'Billing Staff'
4. Retrieve the designation and salary of all 'Manager' and 'Billing Staff' who
have salary in the range of 2500 to 5000 (both inclusive).
SELECT DISTINCT itemtype
FROM item;
SELECT DISTINCT itemtype, category
FROM item;
3. Retrieve the itemcode, description and price with the increasing order of
item's price.
SELECT itemcode, description, price
FROM item
ORDER BY price;
4. Retrieve the different item types and category of the items in the increasing
order of item types and category.
SELECT DISTINCT itemtype, category
FROM item
ORDER BY itemtype, category DESC;
4. Retrieve the empname and salary in increasing order of the designation and
decreasing order of salary.
SELECT empid, empname, designation, salary,
CASE designation
WHEN 'Administrator' THEN salary*1.1
WHEN 'Manager' THEN salary*1.05
WHEN 'Billing Staff' THEN salary*1.20
WHEN 'Security' THEN salary*1.25
ELSE salary*1.02 END newsal
FROM employee;
2. The management of EasyShop would like to classify the salary of
employees as Class 3, Class 2 and Class 1. The classification is done as
if salary is less than 2500 then the class is 'Class 3' , if between 2500
and 5000 then 'Class 2', and if salary is more than 5000 then 'Class1'.
Write a query to display the same.
SELECT
empname, salary,
CASE
WHEN salary < 2500 THEN 'Class 3'
WHEN salary BETWEEN 2500 AND 5000 THEN 'Class 2'
WHEN salary > 5000 THEN 'Class 1'
END as Salgrade
FROM employee;
Summary of this assignment: In this assignment you have learnt the usage of
case statements and solve queries on them.
Summary of this assignment: In this assignment you have learnt the usage of
case statements and solve queries on them.
1. For a discount of 25.5% being offered on all FMCG item's unit price, display
item code, existing unit price as "Old Price" and discounted price as "New
Price". Round off the discounted price to two decimal values.
SELECT itemcode, price "Old Price",
ROUND (price*0.745, 2) "New Price"
FROM item
WHERE itemtype = 'FMCG';
2. Retrieve the employee id, employee name of billing staff and the retail outlet
where they work. Perform a case insensitive search.
SELECT empid, empname, worksin
FROM employee
WHERE UPPER (designation) = UPPER ('Billing staff');
OR
SELECT empid, empname, worksin
FROM employee
WHERE UPPER (designation) = 'BILLING STAFF';
OR
SELECT empid, empname, worksin
FROM employee
WHERE LOWER (designation) = 'billing staff';
3. Retrieve the order id, order status and payment mode of all the orders. Display
'Payment yet not done' when payment mode has NULL value.
SELECT orderid, status,
NVL(paymentmode, 'Payment yet not done')
FROM orderstatus;
SELECT description
FROM item
WHERE LENGTH (description)>15;
SELECT SUBSTR (supplierid, 2, 4)
FROM supplier;
6. Retrieve the order id and the number of days between order date and payment
date for all orders.
SELECT orderid, ABS(orderdate ‐ paymentdate)
FROM orderstatus;
7. Retrieve the order id and the number of months between order date and
payment date for all orders.
SELECT orderid, ABS (MONTHS_BETWEEN (orderdate,
paymentdate))
FROM orderstatus;
8. Insert a record into quotation table with quotation date exactly as '16/2/2014'.
INSERT INTO quotation VALUES ('Q1020', 'S1001',
'I1003', 125, '16/2/2014', 'Open');
INSERT INTO quotation VALUES ('Q1020', 'S1001',
'I1003', 125, TO_DATE('16/2/2014', 'DD/MM/YYYY'),
'Open');
SELECT SYSDATE, TO_CHAR(SYSDATE, 'Mon/dd/yyyy, Day')
FROM DUAL;
10. Retrieve the maximum salary, minimum salary, total salary and average
salary of employees.
SELECT MAX (salary), MIN (salary), SUM (salary),
AVG (salary)
FROM employee;
SELECT COUNT (*)
FROM item;
12. Retrieve the total number of orders made and the number of orders for which
payment has been done.
SELECT COUNT(orderid), COUNT(paymentdate)
FROM orderstatus;
Problem Description: Write the SQL queries for the following problem
statements.
Try out and observe the output:
SELECT COUNT (*)
FROM orderstatus;
SELECT COUNT (amountpaid)
FROM orderstatus;
2. Retrieve the orderid and the number of months between orderdate and
paymentdate for all orders where the number of months is more than one and
the payment has been done.
3. The salary of managers has been increased by 20%. Retrieve the empid,
existing salary as "Current Salary", increased salary as "New Salary" and the
difference of existing and increased salary as "Incremented Amount". Round
off the increased salary up to two decimal places.
4. Display the itemcode of those items where the difference of quantity on hand
and reorder level is more than 50.
Hint: use ABS() function
1) SELECT MAX(empname) FROM employee;
2) SELECT AVG(empname) FROM employee;
3) SELECT MIN(inwarddate) FROM inwarditem;
1. Retrieve the orderid and the number of times for which inward quantity
arrived for that order
Assignments on Joins
4. The Manager of EasyShop would like to know those supplier details with the
quoted price of those items for which quotations have been accepted.
Display supplierid, suppliername, itemcode and quotedprice for the
same.
Note: The table creation scripts and the insertion scripts for this
assignment are available in
"Emp_Dept_Vehicle_Table_Creation_Script.txt". Execute the SQL
statements in this file and create the tables along with data for
practicing various SELECT statements
1. Display name and salary of the employees, who are drawing salary more
than 2000. Along with that display the department names in which they are
working. Structure of the table is given below
Figure 1.1
2. Display the name of employees and their department names who are
managers.
Note: See the structure in Figure 1.1
3. List the department names that have more than one employee drawing
salary more than 1500, working under it.
Note: See the structure in Figure 1.1
2. For each item, identify the stock availability in the retail outlet R1001. Display
itemcode, description and qtyavailable for the same. If there is no stock
available for an item, display 'N.A.' for its quantity on available.
1. For each employee, identify the vehicle owned by them. Display ename and
vehicleid for the same. Display name of employees even if they don't own
any vehicle.
2. For each employee, identify the vehicle owned by them. Display ename and
vehiclename for the same. Display name of employees even if they don't
own any vehicle.
1. For every retail outlet, identify the employees working in it. Display empid,
empname, retailoutletid and their retailoutletlocation.
3. Retrieve the customer id, customer name, and customer type of those
customers who are of the same customer type as that of customer id 2004.
Do not display the record of customer id 2004 in the result.
Note: The required schema and data has already been created using
"Easyshop_RetailApplication_TableCreationScript.txt" script
Problem Description: Write the SQL queries for the following requirements.
1. Identify the items which are shipped from the warehouse to various retail
outlets. Display itemcode, itemtype, description and category of those
items.
2. Identify the item details that have the least quoted price with the quotation
status as 'Rejected'. Display itemcode, itemtype, description and
category of those items.
3. The management would like to know the details of the items which has
maximum quoted price amongst the quotations that have status as 'Closed' or
'Rejected'. Display itemcode and description of those items.
4. Identify the item having second highest unit price. Display itemcode,
description and price of those items.
5. Identify the supplier who has submitted quotation with the least quoted
price amongst the quotations that have been accepted. Display
itemcode, description, supplierid and suppliername for the same.
Summary of this assignment: In this assignment you have learnt the concept
and usage of independent sub queries.
Note: The required schema and data has already been created using
"Easyshop_RetailApplication_TableCreationScript.txt" script
SELECT empid, empname, designation as "Role", salary
FROM employee
WHERE (designation, salary) IN
(SELECT designation, MAX(salary)
FROM employee group by designation);
Summary of this assignment: In this assignment you have learnt the concept
of sub queries returning multiple columns.
Note: The required schema and data has already been created using
"Easyshop_RetailApplication_TableCreationScript.txt" script
Problem Description: Write the SQL queries for the following requirements.
Summary of this assignment: In this assignment you have learnt the concept
of sub queries returning multiple columns.
Note: The required schema and data has already been created using
"Easyshop_RetailApplication_TableCreationScript.txt" script
Problem Description: Write the SQL queries for the following requirements.
1. Display the itemcode, description and quotationdate for those items which
are quoted below the maximum quotation price on the same day.
3. Identify the supplier who has submitted the quotation for an item with the
quoted price, less than the maximum quoted price submitted by all other
suppliers, for the same item.
Display supplierid, suppliername and itemcode for the identified supplier.
Do not display duplicate records.
4. The payroll department requires the details of those employees who are
getting the highest salary in each designation. Display empid, empname,
designation and salary as per the given requirement.
Summary of this assignment: In this assignment you have learnt the concept
and usage of correlated sub queries.
Problem Description: Write the SQL queries for the following requirements.
1. Display the customer id and customer name of those customers who have
not purchased at all from any retail outlet.
2. Display the item code, description of those items which are shipped to any
retail outlet.
Estimated time: 30 minutes.
Summary of this assignment: In this assignment you have learnt the concept
and usage of exists and not exists.
Assignments on Index
1. ‐‐ Index on a single column
SQL> CREATE INDEX supplier_idx
ON supplier(suppliercontactno);
2. ‐‐ Index on multiple column
SQL> CREATE INDEX rs_itid_rup_idx
ON retailstock (itemcode, retailunitprice);
3. ‐‐ Drop an Index
SQL> DROP INDEX supplier_idx;
4. ‐‐ Unique Index on a column
SQL> CREATE UNIQUE INDEX supplier_idx
ON supplier(suppliercontactno);
Summary of this assignment: In this assignment you have learnt the concept
and usage of Index.
Objective: To write SQL queries by using the industry best practices and
formulating logically accurate queries
SELECT ename
FROM emp
WHERE empno = 7946;
5. Do not use HAVING for columns that can be used in the WHERE clause.
For example, do not write the following:
SELECT deptno, SUM(sal)
FROM emp
GROUP BY deptno
HAVING deptno IN(10, 20);
Optimized query
SELECT deptno, SUM(sal)
FROM emp
WHERE deptno IN(10, 20)
GROUP BY deptno;
6. Table aliases should be used in all queries that have more than one table
in the FROM clause. The use of table aliases speeds up the parse phase
of an oracle query, by reducing the number of recursive SQL queries.
7. When joining tables, the table returning the least number of rows should
be last in the FROM list.
8. Consider the following structure of the purchase table. Write a query to
retrieve purchaseid having value less than 100;
9. Remove unnecessary large-table full table scans. If the query returns less
and 40 percent of the table rows in an ordered table or 7 percent of the
rows in an unordered table), the query can be tuned to use an index in
lieu of the full table scan.
10. Try to avoid the use of DISTINCT clause, where ever possible. As the
DISTINCT clause will result in performance degradation, we should use
this clause only when it is necessary or unavoidable.
11. Try to drop indexes that are not being used. Because each index takes
up disk space and slow the DML operations, we should drop indexes that
are not used.
Summary of this assignment: In this assignment you have learnt a few tips for
writing good queries
Assignments on Views
SQL> CREATE OR REPLACE VIEW emp_vw1 AS
SELECT empid, empname, designation, salary
FROM employee;
SQL> SELECT * FROM emp_vw1;
‐‐Views with column alias
SQL> CREATE OR REPLACE VIEW emp_vw2(employeeid,
employeename, role, salary) AS
SELECT empid, empname, designation, salary
FROM employee;
SQL> SELECT * FROM emp_vw2;
Problem Description:
Summary of this assignment: In this assignment you have learnt the concept
and usage of Views
Problem description:
1. A store keeper is adding 25 units of item I1001 in retail outlet R1001. But
then he realized that he has to update the item I1002 and not I1001. So
he undid the update operation. Simultaneously 5 units of item I1001 is
being purchased from retail outlet R1001 by a customer. Sequence of the
transactions are as shown below:
What is your observation?
Dirty read
How many items should be present in the outlet finally? How can this be
resolved?
Incorrect summary
How many units of I1001 should be present in total finally? How can this be
resolved?
Step 1: Open two instances of SQL PLUS on your machine and login with your
Oracle ID in both the instances.
UPDATE emp SET sal=9000 WHERE empid=10004;
UPDATE emp SET sal=6000 WHERE empid=10004;
Note: The second instance is in wait state. The reason is the first transaction has
issued the update statement for the empid 10004 and it acquires the exclusive(X) lock.
The second transaction is also trying to update the same but because of the X lock
acquired by the first transaction it has to wait.
Summary: You have just learnt UPDATE DML statement acquire a row exclusive
lock
The X lock is released only at the end of transaction which is marked by
COMMIT/ROLLBACK.
Summary of this assignment: In this assignment you have learnt the concept
and usage of database locks.
Assignments on PL/SQL
Background: Create the tables and insert the data using the
Write a PL/SQL block as shown below, which declares variables for assigning
departmentid, departmentname and headofdepartment details and display the
same in execution section.
Summary of this assignment: You have just learnt how to declare variables in
the declaration section and how variables can be used in
DBMS_OUTPUT.PUT_LINE statements.
Problem Description: Analyze the following code and answer the questions
given below.
Problem Description:
1. Write a PL/SQL program to update the hostelfee by 15% where hostelid is
'H1' and verify whether any row is updated and if it has affected display how
many rows are updated.
Summary of this assignment: You have just learnt the usage of implicit cursor
attributes in PLSQL blocks.