Visual Basic Lab Manual
Visual Basic Lab Manual
IV Sem, BE ( CS&E)
( 2009 scheme)
Approved By:
HOD
Dept. of CS&E
M. I. T., MANIPAL
2
1. Students should be regular and come prepared for the lab practice.
3. Students should bring the observation book, lab journal and lab manual.
Prescribed textbook and class notes can be kept ready for reference if required.
5. While conducting the experiments students should see that their programs would meet
the following criteria:
6. Once the experiment(s) get executed, they should show the program and results to the
instructors and copy the same in their observation book.
7. Questions for lab tests and exam need not necessarily be limited to the questions in the
manual, but could involve some variations and / or combinations of the questions.
Note: Above mentioned instructions can be modified based on the context of the lab.
CONTENTS
1. MSACCESS 1 week
2. SQL 4 weeks
12 weeks
PROCEDURE OF EVALUATION
Implementation of experiments,
Observation and /or 60%(60 Marks)
JournalAndViva
1. MSACCESS
1. Creating Tables
2. Forms
3. Relationships
4. Filters
5. Queries
6. Reports
7. SQL
Exercises:
1. Create four tables for the VideoParlour database using Design view. The tables
are Member to hold members details, Video to hold details of videos, VideoForRent
to hold the details of copies of videos for rent, and RentalAgreement to hold the
details of video rentals by members.
The Member table has the following fields (with the data type of each in brackets):
memberNo (AutoNumber), fName (Text), lName (Text), sex (Text), DOB
(Date/Time), address (Text), dateJoined (Date/Time), comments (Memo)
The primary key is memberNo.
(Also for this table, set the format property of the Sex field to a field size of 1 with an
Input Mask >L. Also, set this field with a Validation Rule =”M” or “F” and
Validation Text Please enter M or F. If you do not understand the purpose of the
properties associated with each field, Use the help facility using the F1 key).
The Video table has the following fields (with the data type of each in brackets):
catalogNo (Text), title (Text), category (Text), dailyRental (Currency), price
(Currency), directorNo (Text).
The primary key is catalogNo
The VideoForRent table has the following fields (with the data type of each in
brackets): videoNo (Text), available (Yes/No), catalogNo (Text)
The primary key is videoNo
The RentalAgreement table has the following fields (with the data type of each in
brackets): rentalNo (AutoNumber), dateOut (Date/Time), dateReturn (Date/Time),
memberNo (Text), videoNo(Text)
The primary key is rentalNo
(Also for this table, set the format property for the dateOut and dateReturn fields to
Medium Date format e.g. 10-Oct-00.)
2. Open your VideoParlour database. Create a form for your Video table using the
Form Wizard facility and name this form VideoForm1. Use the form to view
records in your Video table. Practise, changing between viewing your Video table
using Form view and Datasheet view.
4. Apply filters to the members and video records. For example, create the following
filters to view:
• Only male members of the video shop.
• Only male members of the shop who joined the shop this year in order of last
name and then first name (*2013).
• All members born in the 1960s.
• Only videos in the Children category with a daily rental rate of less than £4.00
and sorted according to video title.
• Only videos currently available for rent with a certification of “PG” or “U”.
• Only videos by a certain director.
6. Create a report for your Video table containing the catalogNo, title, category and
certificate fields. Group your records according to the values in the category field
and then sort on the values in the title field.
Create a report for your Video table containing the category, dailyRental and price
fields. Group your records according to the values in the category field and then
sum the values in the dailyRental and price fields.
7. Using Access SQL, create simple select queries on the tables of your StayHome
database. For example, create and save the following queries on the Video table.
• List the catalogNo, title and category of the Video table, ordered by video title
• List title, certificate, category and dailyRental of the Video table for videos in the
“Childrens” category with a rental rate less than £4.00.
• List all videos with a certification of “PG” or “18”in the Video table.
2. SQL
Creating a Table
Inserting Tuples
Deleting Tuples
Restrictions:
• Changing the name of table
• Changing the name of the column
• Decreasing the size of a column
Quitting sqlplus
quit;
You can also run a file at connection by using a special form on the command line. The
form of the command is:
If you end a command without a semicolon, but with an empty new line, the command
goes into a buffer. You may execute the command in the buffer by either the command
RUN or a single slash (/).
You may also edit the command in the buffer before you execute it. Here are some useful
editing commands. They are shown in upper case but may be either upper or lower.
lists the command buffer, and makes the last line in the buffer the "current"
LIST
line
LIST n prints line n of the command buffer, and makes line n the current line
LIST m n prints lines m through n, and makes line n the current line
enters a mode that allows you to input text following the current line; you
INPUT
must terminate the sequence of new lines with a pair of "returns"
CHANGE
replaces the text "old" by "new" in the current line
/old/new
APPEND text appends "text" to the end of the current line
DEL deletes the current line
All these commands may be executed by entering the first letter or any other prefix of the
command except for the DEL command.
An alternative is to edit the file where your SQL is kept directly from sqlplus. If you say
edit foo.sql
the file foo.sql will be passed to an editor of your choice. The default is Noted Pad.
There are several methods for creating a typescript to turn in for your programming
assignments. The most primitive way is to cut and paste your terminal output and save it
in a file (if you have windowing capabilities). sqlplus provides the command spool to
save query results to a file. At the SQL> prompt, you say:
spool foo;
and a file called foo.lst will appear in your current directory and will record all user input
and system output, until you exit sqlplus or type:
spool off;
:
KEY DATA TYPES
Fixed-length character data, size characters long.
CHAR(size) Maximum size=255; default=1 byte. Padded on right
with blanks to full length of size.
DATE Valid dates range from Jan 1, 4712 B.C. to Dec 31,
4712 A.D.
For NUMBER column with space for 40 digits, plus
space for a decimal point and sign. Numbers may be
expressed in two ways: first, with numbers 0 to 9, the
NUMBER signs + and -, and a decimal point(.); second, in
scientific notation, e.g. 1.85E3 for 1850. Valid
values are 0 and positive and negative numbers from
1.0E-130 to 9.99…E125.
Variable length character string, maximum size up to
VARCHAR2(size)
2000 bytes.
MISCELLANEOUS DATA
TYPES AND VARIATIONS
DECIMAL Same as NUMBER.
FLOAT Same as NUMBER.
INTEGER Same as NUMBER.
INTEGER(size) Integer of specified size digits wide; same as
NUMBER(size) of specific size digits wide.
students
RollNo Name HostelNoSize cgpa
‘99305017 ‘Sai Sundar’ 11 7.23
’ hostel INTEGER, cpi
‘99305018 ‘Shyam Sundar’ 11 9.23 NUMERIC(3,2) );
‘99305019 ‘Ram Sundar’ 12 8.32
’
Run the following queries :
Updates/deletes:
Multitable queries:
2.3 Implement the Bank Database and execute the given queries/updates
3. SQL (Continued…)
EXCEPT(Minus):
23. Find all customers who have an account but no loan at the bank.
40. Find the average account balance of those branches where the account balance is
greater than Rs. 1200.
41. Find the maximum across all branches of the total balance at each branch
With Clause
42. Select the accounts with maximum balance.
43. Find all branches where the total account deposit is greater than the average of the
total account deposits at all branches.
3.6 Views
44. Create a view all_customers consisting branches and their customers.
45. Select all the customers from all_customers view.
46. Create a view Perryridge_customers consisting customers of Perryridge branch using
all_customers view.
4. SQL (Continued…)
4. Insert few tuples into Employee and Department which satisfies the above
constraints
5. Insert few tuples into Employee and Department which violates some of the above
constraints
7. Modify the foreign key constraint of Employee table such that whenever a
department tuple is deleted, the employees belonging to that department will also
be deleted
9. Try some more examples with ADD, DROP, DISABLE, ENABLE, VALIDATE
INVALIDATE and NOVALIDATE
• LOWER(string)
11. List the customer names in lower case
• UPPER(string)
12. List the customer names in upper case
• ROUND(value, precision)
15. List the balance and balance/3 rounded to nearest hundred from account
(Add data of birth column DOB to Employee Table. Insert appropriate DOB values for
different employees and try the exercise problems given below)
• TO_CHAR(date1, format)
16. Display the birth date of all the employees in the following format:
• ‘DD-MON-YYYY’
• ‘DD-MON-YY’
• ‘DD-MM-YY’
17. List the employee names and the year(fully spelled out) in which they born
• ‘YEAR’
• ‘Year’
• ‘year’
18. List the employee names and the day(of the week fully spelled out) in which they
born
• ‘DAY’
• ‘Day’
19. List the employee names and the month(fully spelled out) in which they born
• ‘MONTH’
• ‘Month’
• LAST_DAY(date1)
20. Find the last day of the month(and its day of the week) in which employee Mr. X is
born
• MONTHS_BETWEEN(date1, date2)
21. Find the age of all the employees
[Hint: Use SYSDATE]
• NEXT_DAY(date1, ‘day’)
• ADD_MONTHS(date1, number of months)
22. Find the Saturday following the Employee’s 60th birthday
• TO_DATE(string, ‘format’)
e.g. to_date(‘12021998’, ‘DDMMYYYY’)
23. List the employees whose birth day falls in the given year X
24. List the employees whose birth day fall between the given years X and Y
25. List the employees who will retire on the given year X.
[Hint: use & with the variable name (e.g. &X) in the SQL query to read the value
from the user]
5. SQL(Continued…)
TREATS
PHY_ID NUMBER(4) - PRI KEY
PATIENT_NO NUMBER(4) - PRI KEY
PROCEDURE_NO NUMBER(4) - PRI KEY
DATE_TREATED DATE - PRI KEY
TREAT_RESULT VARCHAR2(50)
ITEM
ITEM_CODE NUMBER(4) - PRI KEY
DESCRIPTION VARCHAR2(50)
NORMAL_CHARGE NUMBER(7,2)
PHYSICIANS
PHY_ID NUMBER(4) - PRI KEY
PHY_PHONE CHAR(8)
PHY_NAME VARCHAR2(50)
PATIENT
PATIENT_NO NUMBER(4) - PRI KEY
DATE_ADMITTED DATE
DATE_DISCHARAGED DATE
PAT_NAME VARCHAR2(50)
ROOM_LOCATION CHAR(4)
ROOM
ROOM_LOCATION CHAR(4) - PRI KEY
ROOM_ACCOMODATION CHAR(2)
ROOM_EXTENSION NUMBER(4)
PROCEDURES
PROCEDURE_NO NUMBER(4) - PRI KEY
PROC_DESCRIPTION VARCHAR2(50)
1. Get the PATIENT_NO, ITEM_CODE, and CHARGE and from the BILLED
table for a specific PATIENT_NO
2. List all of the different charges that are stored to the table
3. Display all columns and all rows from the BILLED table
4. Display all charges greater than Rs. 5.00 for the PATIENT_NO 1116
5. Display all charges for either patient 1116 or patient 1117
6. Count the number of times patient 1116 has been charged for items
7. Display number of DISTINCT procedures performed on a patient
8. Give a meaningful column name for number of DISTINCT procedures in the
above query
9. Display a calculated value such as the current charge and the amount that would
be charged if the charge were increased by 6% for all rows in the ITEM table
10. List all patients hospitalized more than 6 days
11. List the total charges per patient for expensive medical items (CHARGE greater
than Rs100 for an item) where patients owe the hospital a sum (total charges over
Rs500)
12. List the patients who had either Dr. Hawkeye Pierce or Dr. Trapper John or Dr.
Jonas Salkman as a physician
13. Show the patient names (PAT_NAME field) and associated physician names
(PHY_NAME field) along with the Patient information
14. List the PATIENT_NO and DATE_DISCHARGED from the PATIENT table
and the associated CHARGE from the BILLED table.
6. PL/SQL
1. PL/SQL Environment
2. PL/SQL Syntax
3. Conditional & iteration control: IF-THEN – END IF
WHILE LOOP
Exercise
Usage of IF –THEN
Write a PL/SQL code block that will accept an account number from the user and debit
an amount of Rs. 2000 from the account if the account has a minimum balance of 500
after the amount is debited. The Process is to fired on the Accounts table.
Usage of While:
Write a PL/SQL code block to calculate the area of the circle for a value of radius
varying from 3 to 7. Store the radius and the corresponding values of calculated area in a
table Areas.
Areas – radius, area.
Usage of For:
Write a PL/SQL block of code to achieve the following: if the price of Product ‘p00001’
is less than 4000, then change the price to 4000. The Price change s to be recorded in the
old_price_table along with Product_no and the date on which the price was last changed.
Tables involved: product_master- product_no, sell_price.
Old_price_table- product_no,date_change, Old_price
-Exception handling
-Use of cursors
-Types of cursors –Implicit & Explicit
-Opening a cursor
-Explicit cursor attributes.
-Cursor for loops
Exercise:
Error Handling:
Write a Pl/Sql block which displays area for a given radius. If no data found then display
an error message.
Write a PL/SQL block of code such that depending upon the user entered salesman_no,
the commission_amount is calculated and inserted into the commission_payable table.
Salesman_Master (salesman_no, salesman_name, rate_of_commission, tgt_to_get,
ytd_sales) table records the sales informationof different salesperson. A salesman is
eligible for commission only when he achieves the target sales when omission is paid.
The commission amount, the salesman_no and the date_of_payment is recorded in
commission_payable table. Raise an exception if the total sales by a salesman is less than
the target.
Cursors:
Employee –Emp_code, Emp_name, job, salary, Dept_no.
CursorName%ROWCOUNT:
Write a PL/SQL block that will display the name, department and salary of the first 10
employees getting the highest salary.
Exercise:
A HRD manager has decided to raise the salary for all the employees in Department
number 20 by 0.05. Whenever any such raise is given to the Employees, an audit trail of
the same are maintained in the emp_raise table. The emp_raise table holds the employee
number the date when the raise was given and the raise amount. Write a PL/SQL block to
update the Salary of each employee of dept_no 20 appropriately and insert a record in the
emp_raise table as well.
Employee- Emp_code, Ename, Dept_no, job, salary.
Emp_raise-Emp_code, Raise_date, Raise_amt.
Parameterized Cursors:
Write a Pl/Sql block of code that would update the Bal_stock in the item_master(Itemid,
Description, Bal_stock) table each time a transaction takes place in the item_transaction
(Itemid, Description, Quantity) table. The change in the item_master table depends on the
itemid. If the itemid is already present in the item_master table then an update operation
is performed to decrease the Bal_stock by the Quantity specified in the item_transaction
table. In case the itemid is not present in the item_master table then the record is inserted
into the item_master table.
9. Triggers:
Exercise:
Write a row trigger to insert the existing values of the salary table into a new
table when the salary table is updated
Create a transparent audit system for a table Client_master (client_no, name, address,
Bal_due). The system must keep track of the records that are being deleted or
updated.
The functionality being when a record is deleted or modified the original record
details
and the date of operation are stored in the auditclient(client_no, name, bal_due,
operation,
userid, opdate) table, then the delete or update is allowed to go through.
Exercise:
Procedures:
Write a procedure which takes the department_id as input parameter and lists the names
of all employees belonging to that department.
Write a Pl/Sql block of code that lists the highest salary drawn by an employee in each of
the departments. It should make use of a named procedure dept_highest which finds the
highest salary drawn by an employee for the given department.
Code a procedure to calculate the sales made to a particular Customer. (The customer id
in the transaction file must be Selected, the quantity sold must be multiplied by the price
Which is in the ITEM master and this value must be Accumulated for all records of that
customer in the Transaction file).
Use movies database: (a) Create procedure to find out mv_no issued to IVAN (b) Create
procedure to find out names and mv_no of all customers who have been issued a movie
(c) Create title and type of movies that have been issued to 'Vandana'
Functions:
Code a function to return the net salary given the employee number.
Write a Pl/Sql block of code that lists the highest salary drawn by an employee in each of
the departments. It should make use of a function dept_highest which return the highest
salary drawn by an employee for the given department.
Packages:
Implement a GUI based database application for BANK Database to support few /
all of the following:
13&14. Test
References:
******************