Docsity

Appendix A Practices and Solutions, Schemes and Mind Maps of Database Management Systems (DBMS)

Partial preview of the text

Download Appendix A Practices and Solutions and more Schemes and Mind Maps Database Management Systems (DBMS) in PDF only on Docsity!

Appendix A

Practices and Solutions

Table of Contents

  • Practices for Lesson I................................................................................................................
    • Practice I-1: Introduction
    • Practice Solutions I-1: Introduction
  • Practices for Lesson
    • Practice 1-1: Retrieving Data Using the SQL SELECT Statement
    • Practice Solutions 1-1: Retrieving Data Using the SQL SELECT Statement
  • Practices for Lesson
    • Practice 2-1: Restricting and Sorting Data....................................................................
    • Practice Solutions 2-1: Restricting and Sorting Data
  • Practices for Lesson
    • Practice 3-1: Using Single-Row Functions to Customize Output
    • Practice Solutions 3-1: Using Single-Row Functions to Customize Output
  • Practices for Lesson
    • Practice 4-1: Using Conversion Functions and Conditional Expressions
    • Practice Solutions 4-1: Using Conversion Functions and Conditional Expressions
  • Practices for Lesson
    • Practice 5-1: Reporting Aggregated Data Using the Group Functions.........................
    • Practice Solutions 5-1: Reporting Aggregated Data Using the Group Functions
  • Practices for Lesson
    • Practice 6-1: Displaying Data from Multiple Tables Using Joins
    • Practice Solutions 6-1: Displaying Data from Multiple Tables Using Joins
  • Practices for Lesson
    • Practice 7-1: Using Subqueries to Solve Queries
    • Practice Solutions 7-1: Using Subqueries to Solve Queries
  • Practices for Lesson
    • Practice 8-1: Using the Set Operators...........................................................................
    • Practice Solutions 8-1: Using the Set Operators...........................................................
  • Practices for Lesson
    • Practice 9-1: Manipulating Data
    • Practice Solutions 9-1: Manipulating Data
  • Practices for Lesson
    • Practice 10-1: Using DDL Statements to Create and Manage Tables
    • Practice Solutions 10-1: Using DDL Statements to Create and Manage Tables..........
  • Practices for Lesson
    • Practice 11-1: Creating Other Schema Objects
    • Practice Solutions 11-1: Creating Other Schema Objects
  • Practices for Appendix F
    • Practice F-1: Oracle Join Syntax...................................................................................
    • Practice Solutions F-1: Oracle Join Syntax

Practice I-1: Introduction

This is the first of many practices in this course. The solutions (if you require them) can be found at the end of this practice. Practices are intended to cover most of the topics that are presented in the corresponding lesson.

Starting Oracle SQL Developer

  1. Start Oracle SQL Developer using the SQL Developer desktop icon.

Creating a New Oracle SQL Developer Database Connection

  1. To create a new database connection, in the Connections Navigator, right-click Connections. Select New Connection from the menu. The New/Select Database Connection dialog box appears.

  2. Create a database connection using the following information:

a) Connection Name: myconnection b) Username: ora c) Password: ora d) Hostname: localhost e) Port: 1521 f) SID: ORCL

Ensure that you select the Save Password check box.

Testing and Connecting Using the Oracle SQL Developer Database Connection

  1. Test the new connection.

  2. If the status is Success, connect to the database using this new connection.

Browsing the Tables in the Connections Navigator

  1. In the Connections Navigator, view the objects available to you in the Tables node. Verify that the following tables are present: COUNTRIES DEPARTMENTS EMPLOYEES JOB_GRADES JOB_HISTORY JOBS LOCATIONS REGIONS

  2. Browse the structure of the EMPLOYEES table.

  3. View the data of the DEPARTMENTS table.

Practice Solutions I-1: Introduction

Starting Oracle SQL Developer

  1. Start Oracle SQL Developer using the SQL Developer desktop icon.

a) Double-click the SQL Developer desktop icon.

The SQL Developer Interface appears.

Creating a New Oracle SQL Developer Database Connection

  1. To create a new database connection, in the Connections Navigator, right-click Connections and select New Connection from the menu.

Practice Solutions I-1: Introduction (continued)

Testing and Connecting Using the Oracle SQL Developer Database Connection

  1. Test the new connection.

  2. If the status is Success, connect to the database using this new connection.

Practice Solutions I-1: Introduction (continued)

When you create a connection, a SQL Worksheet for that connection opens automatically.

Browsing the Tables in the Connections Navigator

  1. In the Connections Navigator, view the objects available to you in the Tables node. Verify that the following tables are present: COUNTRIES DEPARTMENTS EMPLOYEES JOB_GRADES JOB_HISTORY JOBS LOCATIONS REGIONS

Practice Solutions I-1: Introduction (continued)

Practices for Lesson 1

In this practice, you write simple SELECT queries. The queries cover most of the SELECT clauses and operations that you learned in this lesson.

Practice 1-1: Retrieving Data Using the SQL SELECT Statement

(continued)

  1. Your first task is to determine the structure of the DEPARTMENTS table and its contents.

  2. Determine the structure of the EMPLOYEES table.

The HR department wants a query to display the last name, job ID, hire date, and employee ID for each employee, with the employee ID appearing first. Provide an alias STARTDATE for the HIRE_DATE column. Save your SQL statement to a file named lab_01_05.sql so that you can dispatch this file to the HR department.

  1. Test your query in the lab_01_05.sql file to ensure that it runs correctly.

Note: After you have executed the query, make sure that you do not enter your next query in the same worksheet. Open a new worksheet.

Practice 1-1: Retrieving Data Using the SQL SELECT Statement

(continued)

  1. The HR department wants a query to display all unique job IDs from the EMPLOYEES table.

Part 3

If you have time, complete the following exercises:

  1. The HR department wants more descriptive column headings for its report on employees. Copy the statement from lab_01_05.sql to a new SQL Worksheet. Name the column headings Emp #, Employee, Job, and Hire Date, respectively. Then run the query again.

Practice Solutions 1-1: Retrieving Data Using the SQL SELECT

Statement

Part 1

Test your knowledge:

  1. The following SELECT statement executes successfully:

SELECT last_name, job_id, salary AS Sal FROM employees;

True /False

  1. The following SELECT statement executes successfully:

SELECT * FROM job_grades;

True /False

  1. There are four coding errors in the following statement. Can you identify them?

SELECT employee_id, last_name sal x 12 ANNUAL SALARY FROM employees;

 The EMPLOYEES table does not contain a column called sal. The column is called SALARY.

 *The multiplication operator is , not x, as shown in line 2.

 The ANNUAL SALARY alias cannot include spaces. The alias should read ANNUAL_SALARY or should be enclosed within double quotation marks.

 A comma is missing after the LAST_NAME column.

Part 2

You have been hired as a SQL programmer for Acme Corporation. Your first task is to create some reports based on data from the Human Resources tables.

  1. Your first task is to determine the structure of the DEPARTMENTS table and its contents.

a. To determine the DEPARTMENTS table structure: DESCRIBE departments

Practice Solutions 1-1: Retrieving Data Using the SQL SELECT

Statement (continued)

b. To view the data contained in the DEPARTMENTS table: SELECT * FROM departments;

  1. Determine the structure of the EMPLOYEES table.

DESCRIBE employees

The HR department wants a query to display the last name, job ID, hire date, and employee ID for each employee, with the employee ID appearing first. Provide an alias STARTDATE for the HIRE_DATE column. Save your SQL statement to a file named lab_01_05.sql so that you can dispatch this file to the HR department. SELECT employee_id, last_name, job_id, hire_date StartDate FROM employees;

  1. Test your query in the lab_01_05.sql file to ensure that it runs correctly.

SELECT employee_id, last_name, job_id, hire_date StartDate FROM employees;

  1. The HR department wants a query to display all unique job IDs from the EMPLOYEES table. SELECT DISTINCT job_id FROM employees;

Part 3

If you have time, complete the following exercises:

  1. The HR department wants more descriptive column headings for its report on employees. Copy the statement from lab_01_05.sql to a new SQL Worksheet. Name the column headings Emp #, Employee, Job, and Hire Date, respectively. Then run the query again. SELECT employee_id "Emp #", last_name "Employee", job_id "Job", hire_date "Hire Date" FROM employees;

  2. 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. SELECT last_name||', '||job_id "Employee and Title" FROM employees;

Practices for Lesson 2

In this practice, you build more reports, including statements that use the WHERE clause and the ORDER BY clause. You make the SQL statements more reusable and generic by including the ampersand substitution.

Practice 2-1: Restricting and Sorting Data

The HR department needs your assistance in creating some queries.

  1. Because of budget issues, the HR department needs a report that displays the last name and salary of employees who earn more than $12,000. Save your SQL statement as a file named lab_02_01.sql. Run your query.

  2. Open a new SQL Worksheet. Create a report that displays the last name and department number for employee number 176. Run the query.

  3. The HR department needs to find high-salary and low-salary employees. Modify lab_02_01.sql to display the last name and salary for any employee whose salary is not in the range of $5,000 to $12,000. Save your SQL statement as lab_02_03.sql.

  4. Create a report to display the last name, job ID, and hire date for employees with the last names of Matos and Taylor. Order the query in ascending order by the hire date.