0% found this document useful (0 votes)
3 views15 pages

Lab Manual for Adbm

The lab manual for Advanced Database Management (ITec2072) outlines various practices focused on SQL programming, including data retrieval, manipulation, and reporting using the Human Resources schema. It includes exercises on using SQL functions, creating and managing tables, and controlling user access. The document serves as a guide for students to enhance their database management skills through practical applications.

Uploaded by

matias bahiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Lab Manual for Adbm

The lab manual for Advanced Database Management (ITec2072) outlines various practices focused on SQL programming, including data retrieval, manipulation, and reporting using the Human Resources schema. It includes exercises on using SQL functions, creating and managing tables, and controlling user access. The document serves as a guide for students to enhance their database management skills through practical applications.

Uploaded by

matias bahiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

LAB MANUAL

Course Name: Advanced Database Management

Course Code: ITec2072

Target Group: G2- IT (A&B) Day of Lab:

Semester: IInd Venue:

Academic Year:2013/2014 Time:

Instructor Name: Hours:

Student Name: __________________________________________

Student ID : __________________________________________

Add Student: Yes No

Objective of the Lab:

Instructor’s Final Comment (regarding the student at the end of the course based
on his performance-lab continuous assessment)

Mark(out of ):
Table of Contents
S.N DATE TOPIC SIGNATURE of LAB
O INSTRUCTOR
1 Practice 1- Retrieving, Restricting & Sorting Data
Practice 2- Using Single Row functions to
customize output.
2 Practice 3- Reporting Aggregated Data Using
the Group Functions & Displaying Data from
Multiple Tables
3 Practice 4 - Using Sub queries to Solve
Queries & Using the Set Operators
4 Practice 5- Using DDL Statements to Create

and Manage Tables


5 Practice 6 - Using DML Statements to
Manipulate Data
6 Practice 7 - Creating Other Schema Objects
7 Practice 8 - Managing Schema Objects
8 Practice 9 - Managing Objects with Data
Dictionary Views.
9 Practice 10 – Control User Access
10 Practice 11 - PL/SQL
11 Practice 12- Object Relational Database
Human Resources (HR) Data Set
The Human Resources (HR) schema is part of the Oracle Common Schema that can be installed
in an Oracle Database. The practices in this course use the data from the HR schema.
Table Descriptions

REGIONS contains rows representing a region (such as Americas, Asia, and so on).
COUNTRIES contains rows for countries, each of which are associated with a region.
LOCATIONS contains the addresses of specific offices, warehouses, and/or production
sites of a company in a particular country.
DEPARTMENTS shows details of the departments in which employees work. Each
department can have a relationship representing the department manager in
the EMPLOYEES
table.
EMPLOYEES contains details about each employee who works for a department. Some
employees may not be assigned to any department.
JOBS contains the job types that can be held by each employee.
JOB_HISTORY contains the job history of the employees. If an employee changes
departments within the job or changes jobs within the department, a new
row is inserted in
this table with the old job information of the employee.
JOB_GRADES identifies a salary range per job grade. The salary ranges do not overlap.
Practice 1- Retrieving, Restricting & Sorting Data
This practice covers the following topics:
 Selecting data from different tables
 Describing the structure of tables
 Performing arithmetic calculations and specifying column names
 Selecting data and changing the order of the rows that are displayed
 Restricting rows by using the WHERE clause
 Sorting rows by using the ORDER BY clause
 Using substitution variables to add flexibility to your SQL

You have been hired as a SQL programmer for Axenture Corporation. Your first task is to
create some reports based on data from the Human Resources tables.
1. Determine the structure of the DEPARTMENTS table and its contents.
2. The HR department wants a query to display the last name, job code, hire date, and employee
number for each employee, with the employee number appearing first. Provide an alias
STARTDATE for the HIRE_DATE column. Write the query to meet the requirements of the
department.
3. The HR department needs a query to display all unique job codes from the EMPLOYEES
table.
4. The HR department has requested a report of all employees and their job IDs. Display the last
name concatenated with the job ID (separated by a comma and space) and name the column
Employee and Title.
5. Create a report to display the last name, job ID, and start date for the employees whose last
names are Matos or Taylor. Order the query in ascending order by start date.
6. Display the last name and salary of employees who earn between $5,000 and $12,000, and are
in department 20 or 50. Label the columns Employee and Monthly Salary, respectively.
7. The HR department needs a report that displays the last name and hires date for all employees
who were hired in 1994.
8. Create a report to display the last name, salary, and commission of all employees who earn
commissions. Sort the data in descending order of salary and commissions.
9. Members of the HR department want to have more flexibility with the queries that you are
writing. They would like a report that displays the last name and salary of employees who
earn more than an amount that the user specifies after a prompt
10. Display the last names of all employees who have both an a and an e in their last name.
Practice 2 -Using Single-Row Functions to Customize Output

This practice covers the following topics:


 Creating queries that require the use of numeric, character, and date functions
 Performing calculations of years and months of service for an employee

1. Write a query to display the current date. Label the column Date.
2. The HR department needs a report to display the employee number, last name, salary, and
salary increased by 15.5% (expressed as a whole number) for each employee. Label the
column New Salary.
3. Write a query that displays the last name (with the first letter uppercase and all other letters
lowercase) and the length of the last name for all employees whose name starts with the
letters J, A, or M. Give each column an appropriate label. Sort the results by the last names of
the employees.
4. The HR department wants to find the duration of employment for each employee. For each
employee, display the last name and calculate the number of months between today and the
date on which the employee was hired. Label the column MONTHS_WORKED. Order your
results by the number of months employed. Round the number of months up to the closest
whole number.
5. Display each employee’s last name, hire date, and salary review date, which is the first
Monday after six months of service. Label the column REVIEW. Format the dates to appear
in the format similar to “Monday, the Thirty-First of July, 2000.”
6. Create a query that displays the employees’ last names and commission amounts. If an
employee does not earn a commission, show “No Commission.” Label the column COMM.
7. Using the DECODE function, write a query that displays the grade of all employees based on
the value of the JOB_ID column, using the following data:
Job Grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
None of the above 0
8. Rewrite the statement in the preceding exercise using the CASE syntax.
9. Create a report that produces the following for each employee:
<employee last name> earns <salary> monthly but wants <3 times salary>.
Label the column Dream Salaries.
Practice 3 -Reporting Aggregated Data Using the Group Functions &
Displaying Data from Multiple Tables
This practice covers the following topics:
 Writing queries that use the group functions
 Grouping by rows to achieve multiple results
 Restricting groups by using the HAVING clause
 Joining tables using an equijoin
 Performing outer and self-joins

1. Find the highest, lowest, sum, and average salary of all employees. Label the columns
Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest
whole number.
2. Modify the above query to display the minimum, maximum, sum, and average salary for each
job type.
3. Write a query to display the number of people with the same job.
4. Determine the number of managers without listing them. Label the column Number of
Managers. Hint: Use the MANAGER_ID column to determine the number of managers.
5. Find the difference between the highest and the lowest salaries. Label the column
DIFFERENCE
6. Create a report to display the manager number and the salary of the lowest-paid employee for
that manager. Exclude anyone whose manager is not known. Exclude any groups where the
minimum salary is $6,000 or less. Sort the output in descending order of salary.
7. The HR department needs a report of all employees. Write a query to display the last name,
department number, and department name for all employees.
8. The HR department needs a report of employees in Toronto. Display the last name, job,
department number, and department name for all employees who work in Toronto.
9. Create a report to display the last name and employee number of employees along with their
manager’s last name and manager number. Label the columns Employee, Emp#, Manager,
and Mgr#, respectively.
10. Create a report for the HR department that displays employee last names, department
numbers, and all the employees who work in the same department as a given employee. Give
each column an appropriate label.
Practice 4 - Using Sub queries to Solve Queries & Using the Set Operators

This practice covers the following topics:


 Creating sub queries to query values based on unknown criteria
 Using sub queries to find out which values exist in one set of data and not in another.
 Use set operators to create reports

1. Create a report that displays the employee number, last name, and salary of all employees
who earn more than the average salary. Sort the results in ascending order by salary.
2. Write a query that displays the employee number and last name of all employees who work in
a department with any employee whose last name contains a u.
3. Display the employee number, last name, and salary of all the employees who earn more than
the average salary and who work in a department with any employee whose last name
contains a u.
4. The HR department needs a list of department IDs for departments that do not contain the job
ID ST_CLERK. Use set operators to create this report.
5. Create a report that lists the employee ID and job ID of those employees who currently have
a job title that is the same as their job title when they were initially hired by the company
(that is, they changed jobs but have now gone back to doing their original job).
6. The HR department needs a list of countries that have no departments located in them.
Display the country ID and the name of the countries. Use set operators to create this report.
Practice 5- Using DDL Statements to Create and Manage Tables

This practice covers the following topics


 Create new tables
 Create a new table by using the create table as
 Verifying that tables exist
 Dropping tables

1. Create the DEPT table based on the following instance chart. Confirm that table is created.
Column Name ID Name
Key Type Primary Key
Null/Unique
FK Table
FK Column
Data type NUMBER VARCHAR2
Length 7 25

2. Populate the DEPT table with data from the DEPARTMENTS table. Include only columns
that you need.
3. Create the EMP table based on the following instance chart. Confirm that table is created.
Column Name ID LAST_NAME FIRST_NAME DEPT_ID
Key Type Primary Key
Null/Unique
FK Table DEPT
FK Column ID
Data type NUMBER VARCHAR2 VARCHAR2 NUMBER
Length 7 25 25 7

4. Create the EMPLOYEES2 table based on the structure of the EMPLOYEES table. Include
only the EMPLOYEE_ID,FIRST_NAME,LAST_NAME,SALARY and
DEPARTMENT_ID columns. Name the columns in your new table ID,
FIRST_NAME,LAST_NAME, SALARY, and DEPT_ID respectively.

5. Drop the EMP table.


Practice 6 – Using DML Statements to Manipulate Data
This practice covers the following topics:
 Inserting rows into tables
 Updating and deleting rows in a table
 Controlling transactions
1. Create table MY_EMPLOYEE Table. Structure is as follows:

Insert data into the MY_EMPLOYEE table.


2. Create an INSERT statement to add the first row of data to the MY_EMPLOYEE table from
the following sample data. Do not list the columns in the INSERT clause. Do not enter all
rows yet.

Populate the MY_EMPLOYEE table with the second row of sample data from the preceding
list. This time, list the columns explicitly in the INSERT clause.
3. Confirm your addition to the table.
4. Write an INSERT statement in a dynamic reusable script file named loademp.sql to load rows
into the MY_EMPLOYEE table. Concatenate the first letter of the first name and the first
seven characters of the last name to produce the user ID. Populate the table with the next two
rows of sample data listed in step 2 by running the INSERT statement in the script that you
created.
5. Make the data additions permanent.
Update and delete data in the MY_EMPLOYEE table.
6. Change the last name of employee 3 to Drexler.
7. Change the salary to $1,000 for all employees who have a salary less than $900.
8. Verify your changes to the table.
9. Delete Betty Dancs from the MY_EMPLOYEE table.
10. Commit all pending changes.
Control data transaction to the MY_EMPLOYEE table.
11. Populate the table with the last row of sample data listed in step 2 by using the statements in
the script that you created in step 4. Run the statements in the script.
12. Mark an intermediate point in the processing of the transaction.
13. Empty the entire table.
14. Discard the most recent DELETE operation without discarding the earlier INSERT
operation.
15. Make the data addition permanent.
Practice 7 - Creating Other Schema Objects - View

This practice covers the following topics


 Creating a simple view.
 Creating a view with check constraint.
 Attempting to modify data in the view.
 Removing views.

1. The staff in the HR department wants to hide some of the data in the EMPLOYEES table.
They want a view called EMPLOYEES_VU based on the employee numbers, employee
names, and department numbers from the EMPLOYEES table. They want the heading for the
employee name to be EMPLOYEE.

2. Display the contents of the EMPLOYEES_VU view.

3. Using your EMPLOYEES_VU view, write a query for the department to display all employee
names and department numbers.

4. Department 50 needs access to its employee data. Create a view named DEPT50 that contains
the employee numbers, employee last names, and department numbers for all employees in
department 50. You are asked to label the view columns EMPNO, EMPLOYEE and
DEPTNO. For security purposes, do not allow an employee to be reassigned to another
department through he view.

5. Display the structure and contents of the DEPT50 view.

6. Attempt to reassign Matos to department 80. Is it possible or not?

Part II
This practice covers the following topics
 Creating & Using sequences.
 Creating non-unique indexes.
 Creating synonyms.
7. Create a sequence that can be used with the primary key column of the DEPT table. The
sequence should start at 200 ,a maximum value of 1000 and increment by 10. Name the
sequence DEPT_ID_SEQ.
8. To test your sequence, insert two rows in the DEPT table. Add two departments: Education
and Administration. Be sure to use the sequence that you created for the ID column. Confirm
your additions.
9. Create a non unique index on the NAME column in the DEPT table.
10. Create a synonym for your Employees table. Call it EMP.
Practice 8- Managing Schema Objects

1. Create the DEPT2 table based on the following table instance chart. Confirm that the table is
created.
Column Name ID Name
Key type
Nulls/Unique
FK table
FK Column
Data Type Number VARCHAR2
Length 7 25

2. Populate the DEPT2 table with data from the DEPARTMENTS. Include only the columns
that you need.

3. Create table EMP2 table based on the following table instance chart. Confirm that the table is
created.
Column Name ID Last_Name First_Name Dept_Id
Key type
Nulls/Unique
FK table
FK Column
Data Type Number VARCHAR2 VARCHAR2 NUMBER
Length 7 25 25 7

4. Modify the EMP2 table to allow employee last names of length 50 character.Confirm your
modification.

5. Create the EMPLOYEES2 table used based on the structure of the EMPLOYEES table.
Include only the EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY and
DEPARTMENT_ID columns. Name the new columns in your new table ID,
FIRST_NAME,LAST_NAME, SALARY and DEPT_ID respectively. Confirm that the table
is created.

6. Drop the EMP2 table.

7. Query the Recycle bin to see whether the table is present.


8. Undrop the EMP2 table and confirm it.

9. Drop the first_name column for EMPLOYEES2 table. Confirm your modification by
checking the description of the table.

10. In the EMPLOYEES2 table, mark the dept_id column as UNUSED. Confirm your
modification by checking the description of the table.

11. Drop all the UNUSED columns of the EMPLOYEES2 table. Confirm your modification by
checking the description of the table.
12. Add a foreign key reference on the EMP2 table that ensures that the employee is not assigned
to a nonexistent department. Name the constraint my_emp_dept_id_fk.

13. Modify the EMP2 table. Add a COMMISSION column of the NUMBER data type , precision
2 and scale 2. Add a constraint to the commission column that ensures that a commission
value is greater than zero.

14. Drop the EMP2 and DEPT2 tables so that they cannot be restored. Verify the Recycle bin.

15. Create the DEPT_NAMED_INDEX table based on the following table instance chart.Name
the index for the PRIMARY_KEY column as DEPT_PK_IDX.
Colimn Name Dept No Dname
Primary Key Yes
Data Type Number Varchar2
Length 4 30
Practice 9 - Managing Objects with Data Dictionary Views.

This practice covers the following topics


 Querying the dictionary views for table and column information.
 Querying the dictionary views for constraint information.
 Querying the dictionary views for view information.
 Querying the dictionary views for sequence information.
 Querying the dictionary views for synonym information.
 Adding a comment to a table and querying the dictionary views for comment
information.

1. For a specified table, write a query reports the column names, data types, and lengths of the
data types as well as whether nulls are allowed. Prompt the user to enter the table name. Give
appropriate aliases to the DATA_PRECISION and DATA_SCALE columns.

2. Write a query that reports the column name, constraint name, constraint type, search
condition, and status for a specified table. You must join the USER_CONSTRIANTS and
USER_CONS_COLUMNS tables to obtain all this information. Prompt the user to enter the
table name.

3. Add a comment to the DEPARTMENTS table. Then query the USER_TAB_COMMENTS


view to verify that the comment is present.

4. Find the names of all synonyms that are in your schema.

5. Determine the names and definitions of all the views in your schema. Create a report that
retrieves the view information: the view name and text from the USER_VIEWS data
dictionary view.

6. Find the names of sequences that is created by you. Write a query to display the following
information about your sequences: sequence name, maximum value, increment size and
last_number.
Practice 10 - Controlling User Access
1. Which privileges should a user be given to log on to the Oracle server? Is this a system or an
object privilege?
2. Which privilege should a user be given to create tables?
3. If you create a table, who can pass along privileges to other users on your table?
4. You are the DBA. You are creating many users who require the same system privileges.
What should you use to make your job easier?
5. Which command would you use to change your password?
6. Login using system/managers account.
7. Create a user ora21 with password ora21.
8. Create user ora22 with password ora22.
9. Grant Create Session, Create Table privileges for users ora21 and ora22.
10. Grant select, insert, update and delete privileges on departments table of system user to
ora21 with grant option.
11. Connect as user ora21. Query all rows in your DEPARTMENTS table.
12. Add a new row to your DEPARTMENTS table. Add Education as department number 500.

13. Grant user ora22 access to your DEPARTMENTS table.

14. Connect as user ora22. Query on DEPARTMENTS table granted by ora21 .

15. Create a synonym for user system’s DEPARTMENT’s table. Query all the rows in user
ora21’s DEPARTMENTS table by using your synonym.

16. As user ora22, query the USER_TABLES data dictionary to see information about the tables
that you own.

17. As user ora22,query the ALL_TABLES data dictionary view to see information about all the
tables that you can access. Exclude the tables that you own.

18. Connect as user ora21 and revoke the SELECT privilege on departmentsfrom user ora22.

19. Remove the row that you inserted into the DEPARTMENTS table in step 12 and save the
changes.

20. Alter the password of the user ora21 as ‘xxxx123’


21. Create a Role “Admin’ for grouping the privileges create user, create session, create table,
create index.

22. Create user Tester, SysAdmin with privileges create user, create session, create table, create
index using the role Admin.

You might also like