0% found this document useful (0 votes)
411 views18 pages

Bcsccs 505 r01 Dbms-Lab Manual-2010

This document contains instructions for a series of database experiments involving creating tables, inserting data, modifying data through queries, adding constraints, and performing arithmetic, logical, and set operations on the data. The experiments create tables for products, PCs, laptops, and printers, insert sample data, perform queries to update, select, delete and aggregate data based on various conditions, add primary key, foreign key and other constraints to the tables, and demonstrate sorting and filtering of the data.

Uploaded by

Hariharan Ram
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
411 views18 pages

Bcsccs 505 r01 Dbms-Lab Manual-2010

This document contains instructions for a series of database experiments involving creating tables, inserting data, modifying data through queries, adding constraints, and performing arithmetic, logical, and set operations on the data. The experiments create tables for products, PCs, laptops, and printers, insert sample data, perform queries to update, select, delete and aggregate data based on various conditions, add primary key, foreign key and other constraints to the tables, and demonstrate sorting and filtering of the data.

Uploaded by

Hariharan Ram
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SHANMUGHA ARTS, SCIENCE, TECHNOLOGY AND RESEARCH ACADEMY

( SASTRA UNIVERSITY )

Tirumalaisamudram
Thanjavur – 613 402

BCSCCS 505 R01/ BICCIC 506/ BITCIT 506R01

Database Management System Lab Manual


List of Experiments

PART - 1 : SQL and PL/SQL

1. Creation of Tables & Simple Queries


2. Key Constraints
3. Arithmetic, Logical, Set Operations & Sorting
4. Built-in Functions
5. Nested Queries & Grouping
6. Update Operations
7. Joins
8. Index, Views, Sequences & Synonyms
9. Simple PL/SQL Procedure and Cursor

PART - 2 : Developer 2000 (Forms)

10. Student Information System


11. Payroll System

PART - 3 : Developer 2000 (Reports)

12. Payroll Report


13. Student Report

Additional experiment

14. Trigers in PL/SQL


PART - 1 : SQL

1. CREATION OF TABLES & SIMPLE QUERIES

Table Name: Product_Info


Column Name Purpose
Maker Name of the maker/manufacturer of
computers/peripherals
Model_No The unique identifier for each product
manufactured
Type Indicates the type of the product – PC
for personal computers, LP for Laptop
and PR for printers

Table Name: PC
Column Name Purpose
Model_No The unique identifier for each PC
Speed Clock Speed of the PC
RAM RAM in MB
HD The hard-disk capacity of the PC in GB
CD The speed of the CD Drive
Price Price of the PC

Table Name: Laptop


Column Name Purpose
Model_No The unique identifier for each Laptop
Speed Clock Speed of the PC
RAM RAM in MB
HD The hard-disk capacity of the PC in GB
Screensize Screensize of the laptop
Price Price of the PC

Table Name: Printer


Column Name Purpose
Model_No The unique identifier for each Laptop
Color Flag indicating whether it is a color
printer or a black and white printer
Type Line, InkJet or Laser
Price Price of the printer

The product_info table serves as a master table containing the list of all models of pc, laptop
and printers manufactured by all makers. The PC, Laptop and the printer table give specific
details about each product in that class.
1. Create the 4 tables mentioned above
a. Check if the table is created successfully (tab table)

2. Insert the following tuples in the table ProductInfo

MAKER MODEL TYPE


--------- -------------------- -----------
HCL PC112 PC
HCL LP113 LP
ZENITH PR114 PR
WIPRO PC122 PC
WIPRO LP123 LP
WIPRO PR124 PR
IBM PC134 PC
HCL LP114 LP
IBM PC132 PC
IBM LP133 LP
IBM PR134 PR

3. Insert the following tuples in the table pc

MODEL SPEED RAM HD CD PRICE


-------------------- --------- --------- --------- --------- ---------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC132 1 128 100 68 50000
PC134 1 512 60 68 80000

4. Insert the following tuples in the table LAPTOP

MODEL SPEED RAM HD SCREEN PRICE


------------------------------------------------------------------------
lp113 1 64 40 14 59000
lp123 2 128 60 16 72000
lp133 2 256 80 17 100000
lp114 2 128 40 17 45000

5. Insert the following tuples in the table PRINTER

MODEL COLO TYPE PRICE


---------------------------------------------------
Pr114 true ink 17000
pr124 false dot 12000
pr134 true laser 17000

6. Insert a tuple such that the model value is PC100, it is manufactured by HCL and has
a speed of 3 GHz, RAM of 256 MB, HD of 40 GB and CD of speed 52x. The price is
50000.

7. Delete all PC’s with less than 50 cd speed.


8. Manufacturer IBM buys manufacture HCL. Change all products made by HCL so
they are now made by IBM.

9. For each PC double the amount of RAM and add 10 gb to the HDD.

10. For the printer whose modelno=pr124 subtract Rs. 4000 from the price.

2. KEY CONSTRAINTS

1. Alter the table product_info to make the type column NOT NULL.

2. Alter the table pc to have a default speed of 2.

3. Create suitable primary keys for all the tables of the above database schema
a. Check if the primary keys are created successfully (User_Constraints table)

4. Create suitable foreign keys for all the tables of the above database schema
a. Check if the foreign keys are created successfully (User_Constraints table)
b. Demonstrate the usage of On delete cascade

5. Check Constraints:
a. Apply a check constraint on the product_info table such that the only permitted
values for the type column are ‘pc’, ‘lp’ and ‘pr’.
b. Apply a check constraint such that the prices of pc, laptop and printer are all
positive.
c. Check if the check constraints are created successfully (User_Constraints
table)

6. What are the different values for constraint_type in the user_constraints table and
what is the meaning of each of those values?

7. Create a table named ‘Type_info’ with the columns model , type from ProductInfo
table ( Use the “create table yyy as select” construct )
a. Initially no rows should be present in the dummy table
b. Insert all the rows from product_info into ‘Type_Info’ (only selected columns)

8. Modify the table printer by adding a column printercode of type varchar2(10) to


identify the printer uniquely in the world.

9. Add a Unique key constraint in the printercode column.


a. Check if the unique key constraint is created successfully (User_Constraints
table)

10. Increase the width of the column printercode by 2.

11. Without removing the table definition from the database remove all the rows from the
table ‘Type_info’.

12. Drop the table ‘type_info’ from the database.


3. ARITHMETIC , LOGICAL , SET OPERATIONS & SORTING

Create a table ‘WORKERSKILL’ with the following details:

Name Skill City Phone


DICK JONES SMITHY TRICHY 91-0437-77651
JOHN PEARSON COMBINE DRIVER CHENNAI 91-0426-98721
HELEN BRANDT COMBINE DRIVER MADURAI 91-0435-33333
JOHN PEARSON COMBINE DRIVER CHENNAI 91-0453-98765
JOHN PEARSON SMITHY MADURAI 91-0345-34565
VICTORIA LYNN SMITHY SYDNEY 91-0234-98723
ADAH TALBOT WORK THANJAVUR 91-0652-66544
ELBERT LOWELL DISCUS DELHI 91-0433-90875
WILFRED LOWELL WORK SALEM 91-0213-98723
ANAND KUMAR PROGRAM ERODE 91-0441-98123
JACKSON PROGRAM HOSUR 91-0543-90873

Create a table named ‘WORKER’ with the following details

Name Age
DICK JONES 33
WILLIAM SWING 30
BART SARJEANT 32
SUMA RANGANATH 21
RICHARD WILLIAMS 29
BHARTH KUMAR 36
JOHN PEARSON 28
JACKSON 34
MEENA KUMARI 29
ADAH TALBOT 27

1. List those rows of the table PC where RAM sizes are either ‘128’ or ‘256’ and the
capacity of HD is greater than or equal to 50.

2. List all the rows from PRINTER except the printers ‘pr112’ or ‘pr124’.

3. List all the rows from LAPTOP in ascending and descending order of the screen size.

4. List the makers of PC’s. Use ‘like’ operator in the model field (don’t use type).

5. List the laptop details where the screen size is not 17.

6. List the printers whose price is between 5000 and 10000. (Use between).

7. Give examples for any one arithmetic , logical operator , between … and & in.

8. Display the PC details if we double the ram capacity. (Don’t update).


9. Display the printer details with the following columnname as heading

PRINTER MODEL COLORINFO PRINTERTYPE PRICE


------------------------ ------------------ --------------------- ----------

10. Apply the following operations on WORKER and WORKERSKILL tables


UNION, UNION ALL , INTERSECTION & MINUS.

11. Display the HD size available in PC without duplicate values(use DISTINCT).

4. BUILT- IN FUNCTIONS

1. Find the difference between the highest and lowest price from laptop.

2. Find the Standard deviation and Variance of price in PC table.

3. Use a specific command to get the data in the following manner:

Workername age

JACK ADAMS --21


MEENA KUMARI --31

4. a. Update the PC table such that, the price for all PC models is increased by 12.33%.
b. Display the PC model in lowercase and price rounded to the hundredths for all the
PCs.

5. List the names in the ascending order of the length of names from WORKER table.

6. List the phone number (eliminate area code like 91-0437) of all employees from
WORKERSKILL table.

7. Find the location of letter ‘A’ in the employee name of WORKERSKILL table.
(Hint: use INSTR ( )).

8. Use SOUNDEX ( ) function on WORKERSKILL table to find a record of column city


sound like ‘ Sidney ’.

9. List only the lastname from WORKERSKILL table.

10a. List the employee name from WORKERSKILL table with lastname first and firstname
next in single column. (using CONCAT (str1,str2) function).

b. List the employee name from WORKERSKILL table with lastname first and firstname
next in single column. (using || operator).

c. List the employee name from WORKERSKILL table with lastname first and firstname
next in single column. (without using CONCAT () function and || operator).
11a. Alter the table worker skill to add a new column called “date-of-joining” (date data type).

b. Update the table to include the values for date-of-joining for each worker.

c. List the name of each worker along with his experience in number of years and
months.

12. Display the current date/time in the following formats:


a. Sep-2006
b. September 12 2006
c. 12-09-06 12:11:10

13. Display the equivalent GMT and EST, for the given date and time.

14. Give examples for replace, decode and translate functions.

15. Apply formatting on price column of table PRINTER to display like Rs. 50,000
and $50,000 ( using to_char , format command ).

5. NESTED QUERIES & GROUPING

1. Display the printer information whose price is equal to any one of the printer
manufactured by ‘IBM’.

2. List the laptop details whose screen size is equal to the screen size of ‘lp114’.

3. Give an example for a correlated sub-query using any of the tables above.

4. List the printer details whose price is greater than any other printer.(use any, all or in).

5. Solve the above query without using any or all operators.

6. For each value of RAM, list the number of PCs (use group by function).

7. For each value of HD, list the number of PCs (use group by function).

8. List the HD values for which the number of PCs is more than 2. (Use group by and
Having clause).

9. List the details of the senior most worker and junior most worker.
6. UPDATE OPERATIONS

1. Update all the rows of the printer table, such that the price is increased by 12% for Ink
Jet Printers, 14% for Dot Matrix Printers, 10% for Laser Printers.( using separate
Update as well as single update statements)

2. Update the Laptop table to increase the price by 12.33% for only those laptops with a
speed of >= 2 and manufactured by HCL or IBM

3. Insert into Laptop table, all the rows from the PC table with the following considerations:
a. If the model number for the PC row is PCxxx, the model number in the Laptop
table should be LPxxx. ( Hint : Omit the CD column )

b. Keep the screen size as a constant value of 17.

c. Price should be greater than 15,000.

4. The above query can be solved using a single “Insert statement” as well. Try that.

5. Give an example of an update statement that uses any of the built-in functions

7. JOINS

1. Give an example for a Cartesian join.

2. Give an example for a Cartesian join involving 3 tables.

3. List the name, age, skill from the worker and worker skill tables for workers whose age is
greater than 30 (use equi join).

4. Give an example for a non-equi join.

5. List the distinct combinations of PC models whose HD capacities are same (use self
join).

6. Display the manufacturers of PC along with the details model, price (use inner join).

7. List the laptop and pc pairs whose price is same (list the model numbers, use inner join).

8. Give an example for left outer, right outer and full outer joins.

9. Give an example of how NVL function can be used effectively in outer joins.
8. INDEX, VIEWS, SEQUENCES & SYNONYMS

1. Create a view Price_info to include the columns model, maker and price for the PCs.

2. Create a view Price6000 on the table PRINTER to include only rows with price > 6000.

3. Modify one row in both the views and check whether it is reflected in the table and vice
versa.

4. Delete one row from the views. Record the observations.

5. Create a unique index on the worker name column of the worker table.

6. Create a bitmap index on the skill column of the worker table

7. Create a function based index on the city column of the worker table (Use upper, lower
or initcap function and test)

8. Create a sequence object in ascending order start with 100.

9. Create a sequence object in descending order start with 100.

10. Use the system variables currval, nextval to pick values from these sequences and
demonstrate their usage in insert, update, delete and select statements

11. Create a synonym object for worker skill with the name ‘G20K’.

9. Simple PL/SQL Procedure and Cursor

i) Create a table called “Areas” with the following columns:

a. Input_Value
b. Circle_Area
c. Square_Area
d. Sphere_Area
e. Sphere_Volume
f. Cube_Volume

Write a procedure to declare a character & constant number and do the following:

a. If the character is ‘R’ assuming that the constant number is a radius, calculate the
area of the circle , sphere and the volume of the sphere it will form.

b. If the character is ‘L’ assuming that the constant number is the length of the side
of a square, calculate the area of the square it will form and the volume of the
cube it will form.

c. If the character is not ‘R’ or ‘L’ then print “ invalid choice ”.


d. After calculation, insert the values into the “Areas” table.

Implement the same functionality using a “Function”

ii) Create a table called “Constant_Values” with the following columns:

Constant_1 (Integer)
Constant_2 (Integer)

Insert few rows into the “Constant_Values” table.

Alter the table “Areas” to have the columns as follows:

a. Input_Value1
b. Input_Value2
c. Circle_Area
d. Square_Area
e. Cylinder_Volume
f. Rectangle_Area
g. Sphere_Area
h. Sphere_Volume
i. Cube_Volume

Write a procedure to do the following:

a. Declare a cursor
b. Read the rows one by one into the cursor ROWTYPE variable
c. If both the constants have a value in that row,
i) Assume them to be the length and breadth of a rectangle and calculate the
area.
ii) Assume them to be the radius and height of a cylinder and calculate the
volume.
iii) Assuming the 1st value to be the radius, do as in the prior exercise
iv) Assuming the 1st value to be the side of the square, do as in the prior
exercise

After calculation, insert the values into the “Areas” table. Repeat the process for all the rows
in the “Constant_Values” table. Close the cursor.
PART - 2 : Developer 2000 (Forms)
10. Student Information Systems

Problem Description :

a. A university conducted examination and collected the students personal data


and their marks.

b. Performance operations like ADD,MODIFY,SEARCH,DELETE on students


personal data and academic data.

c. Also find the top 3 students in each subject.

d. Use appropriate interface items like radio group for gender column, list box for
branch of study.

The following are the students personal details:

REGISTER NO.,NAME,GENDER,DATE OF BIRTH & ADDRESS.

Academic details:

DEGREE,BRANCH OF STUDY,YEAR OF STUDY,SEMESTER NUMBER,STUDENTS


MARKS IN VARIOUS SUBJECTS

Validation :

1. No two students should have the same register no.


2. The branch should either be one of the following:
B.E,B.Tech,M.Tech,Ph.D,MCA,MBA,M.E,M.S
3. Calculate the total and % for each of the student.
4. The class evaluation should be as follows:
i. IF THE % IS >59.99 THEN THE CLASS MUST BE FIRST
ii. IF THE % IS >49.99 THEN THE CLASS MUST BE SECOND.
iii. IF THE % IS >39.99 THEN THE CLASS MUST BE THIRD.

Use appropriate items like LOVs, push buttons, text items, display items, radio group etc.
Note: Calculate age of each student from his or her date of birth.
11. Payroll System

Problem Description:

– Employee details (EMPNO,DESIG, DOJ (dd-mon-yy), AGE, EXP)


– Loans ( EMPNO,NAME,DESIG,LOAN,IDATE, NOINST,INTEREST,
DETECTION).

Validations:

– No two employees have the same empno.


– For every 5 yrs. of total service , RS. 500 must be automatically added to the BP.
– Detection = (loan + interest) / No. Of installment
– DA=20 % of BP
– HRA = CCA = 50% of BP.
– GP = BP +DA + HRA + CCA .

PART- 3 : Developer 2000 (Reports)

1. Payroll Reports

Problem Description:

Consider the relation Schema:


– Employee details (EMPNO,DESIG, DOJ (dd-mon-yy), AGE, EXP)
– Loans ( EMPNO,NAME,DESIG,LOAN,IDATE, NOINST,INTEREST,
DETECTION).

Using report generation generate the following type of reports


Normal report
Master Detail report

2. Student Reports

Problem Description:

Consider the relation Schema:

Stud_pers(REGISTER NO.,NAME,GENDER,DATE OF BIRTH,and ADDRESS)


Stud_Acad(DEGREE,BRANCH OF STUDY,YEAR OF STUDY,SEMESTER
NUMBER,STUDENTS MARKS IN VARIOUS SUBJECTS)

Using report generation generate the following type of reports


Normal report
Master Detail report
Additional Exercise:
14. TRIGGERS

Create the necessary triggers for the following scenario:

Sales_Transaction

Prod_Code Date_Of_Sale Qty Price


P001 01-Jan-2007 10 1000
P002 01-Jan-2007 10 1000
P003 01-Jan-2007 10 1000
P004 02-Jan-2007 10 1000
P001 02-Jan-2007 10 1000

VAT_Calculation

Date_Of_Sale Prod_Code VAT


01-Jan-2007 P001 10% of the price sold
on this date
01-Jan-2007 P002 10% of the price sold
on this date

1. Whenever an insert happens to the Sales_Transaction table, the VAT for the price
should be calculated and inserted into the VAT_Calculation table.

2. If the price is updated in the Sales_Transaction table, the VAT in the


VAT_Calculation table should be updated accordingly.

3. If a row is deleted in the Sales_Transaction table, the corresponding row in the


VAT_Calculation table should be deleted as well.
Syntax for Various SQL & PL/SQL Commands

The Create Table Command:

CREATE TABLE tablename


(Columnname datatype(size),
Columnname datatype(size));

Creating a table from a table:

CREATE TABLE tablename


[(columnname,columnname)]
AS SELECT columnname,columnname
FROM tablename;

Insertion of Data into tables:

INSERT INTO tablename


[(columnname,columnname)]
VALUES (expression,expression);

Inserting data into a table from another table:

INSERT INTO tablename


SELECT columnname,columnname
FROM tablename;

Insertion of selected data into a table from another table:

INSERT INTO tablename


SELECT columnname,columnname
FROM tablename
WHERE column = expression;

Updating the Contents of a table:

UPDATE tablename
SET columnname = expression, columnname = expression
WHERE columnname = expression;
Deletion:

(I) All Rows:


DELETE FROM tablename;

(II) Specified Number of Rows:

DELETE FROM tablename WHERE search condition ;


Select :

(I) Global Data Extract:

SELECT * FROM tablename ;

(II) Retrieval of Specific Columns:

SELECT columnname,columnname FROM tablename;

(III) Elimination of Duplicates:

SELECT DISTINCT columnname,columnname


FROM tablename;

Sorting of Data in a Table:

SELECT columnname,columnname
FROM tablename
ORDERBY columnname,columnname;

Grouping Data From Tables:

SELECT columnname,columnname
FROM tablename,
GROUPBY columnname,columnname;

Modifying the Structure of tables:


(I) Adding new columns:

ALTER TABLE tablename


ADD (newcolumnname datatype(size),
newcolumnname datatype(size)….);

(II) Modifying Existing Columns:

ALTER TABLE tablename


MODIFY (columnname newdatatype(size));
Removing/Deleting/Dropping Tables:

DROP TABLE tablename;

Indexes:

CREATE INDEX indexfilename


ON tablename (columnname);

Dropping Indexes:

DROP INDEX indexfilename;


Views:

CREATE VIEW viewname AS


SELECT columnname,columnname
FROM tablename
WHERE columnname = expressionlist;

Destroying a View:

DROP VIEW viewname;

Granting Permissions With GRANT Statement:

GRANT { Object Privileges }


ON objectname
TO username
[WITH GRANT OPTION] ;

Revoking Permissions with Revoke Statement:

REVOKE { Object Privileges }


ON objectname
FROM username;

Cursors:
Declaration :

CURSOR <CURSOR_NAME> IS SELECT <FIELD NAME(S) > FROM <TABLE_NAME>

Cursor Handling :

1. OPEN <CURSOR_NAME>;
2. FETCH <CURSOR_NAME> INTO <COLUMN-NAME(S)>;
3. CLOSE <CURSOR_NAME>;

Explicit cursor is one in which cursor name is explicitly assigned to the select statement.
Implicit cursor is used for all other SQL statement.
Subprograms :

(i) procedures:

CREATE [OR REPLACE] PROCEDURE <PROC_NAME> [PARAMETER LIST] IS


<LOCAL DECLARATIONS>
BEGIN
<STATEMENTS>;
END;

(ii) Functions:

CREATE [OR REPLACE] FUNCTION <FUNC_NAME>[PARAMETER LIST] RETURN


data type IS
<LOCAL DECLARATIONS>
BEGIN
<STATEMENTS>;
END;

A function should return a value to the called block whereas a procedure will not return any
value.

Triggers :

CREATE [OR REPLACE] TRIGGER <TRIGGER-NAME>


[BEFORE/AFTER] [INSERT/UPDATE/DELETE]
ON <TABLE NAME> [ FOR EACH ROW/STATEMENT]
[WHEN <CONDITION>];
<trigger body>;

Trigger is a stored procedure that is fired when a DML statement is issued against the
associated table.

****************************

You might also like