Академический Документы
Профессиональный Документы
Культура Документы
com
Project Scenario:
You are a small consulting company specializing in database development. You have just been awarded the
contract to develop a data model for a database application system for a small retail store called Oracle
Baseball League (OBL).
The Oracle Baseball League store serves the entire surrounding community selling baseball kit. The OBL has
two types of customer, there are individuals who purchase items like balls, cleats, gloves, shirts, screen printed
t-shirts, and shorts. Additionally customers can represent a team when they purchase uniforms and equipment
on behalf of the team.
Teams and individual customers are free to purchase any item from the inventory list, but teams get a discount
on the list price depending on the number of players. When a customer places an order we record the order
items for that order in our database.
OBL has a team of three sales representatives that officially only call on teams but have been known to handle
individual customer complaints.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
2
In this step you will use the “obl Sports.ddl” file provided for you to create your relational database. This file
must first be extracted.
DESCRIBE customers;
4. All changes that you make in APEX are permanent changes so you do not need to save your work.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
3
Task Instructions
Use the completed table mapping document produced in a previous exercise to verify the table creation.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
4
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
5
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
6
2. How many columns are created for the price history table?
Solution : 6
3. What statement is used to enforce the constraint that the category column of the items table must
have a value?
Solution : NOT NULL
4. What is the name of the foreign key constraint between the customers and customer addresses tables?
Solution : customer_address_customer_fk
5. What are the lowest and highest values that can be stored in the commission_rate column for the
sales_representatives table?
Solution : -99 to 99
6. What are the lowest and highest values that can be stored in the price column for the price_history
table?
Solution : -99999.99 to 99999.99
7. What are the 3 columns that make up the primary key for the price_history table?
Solution : itm_number, start_date, start_time
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
7
Modifying a column
1. Run the DESCRIBE command on the orders table to view its structure.
2. Task: Add a default constraint that will use todays date to assign a value to the odr_date column of the
orders table if no date is provided.
Solution :
ALTER TABLE orders
MODIFY (odr_date DATE DEFAULT SYSDATE);
3. Run the DESCRIBE command again to verify the command was successful.
Adding a column
The client has decided that they would like a separate column for the customer’s mobile phone number. This is
an optional column that will be required to store 11 digits.
1. Run the DESCRIBE command on the customers table to view its structure.
2. Task: Add column that will satisfy the clients requirements
Solution :
ALTER TABLE customers
ADD mobile_number VARCHAR2(11);
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
8
3. Run the DESCRIBE command on the customers table to view its structure.
Dropping a column
The client has decided that they don’t need the mobile number column as most customers only provide a
single contact number and that is already catered for with the existing phone_number column.
1. Run the DESCRIBE command on the customers table to view its structure.
2. Task: Drop the column that was created to store the mobile phone number.
Solution :
ALTER TABLE customers
DROP COLUMN mobile_number;
3. Run the DESCRIBE command on the customers table to view its structure.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
9
In this step you will populate and work with the data that is stored in the database system tables.
2. Open the “sports data.sql” and look at the order the data is being added there, does your list match?
This file has been provided for you in the interaction (sports data.zip) and must first be extracted.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
10
Solution :
INSERT INTO teams (id, name, number_of_players, discount)
VALUES('t004', 'Jets', 10, 5);
Loyalty
ctr First Phone Current tem sre
email Last name card
number name number balance id id
number
brianrog@hoote
c02001 Brian Rogers 01654564898 -5 lc4587
ch.com
Solution :
INSERT INTO customers (ctr_number, email, first_name, last_name,
phone_number, current_balance, loyalty_card_number)
VALUES('c02001', 'brianrog@hootech.com', 'Brian', 'Rogers',
'01654564898', -5, 'lc4587' );
3. This information violates the check constraint that the current balance must not be less than zero.
Change the current balance to 50 and rerun the query.
Solution :
INSERT INTO customers (ctr_number, email, first_name, last_name,
phone_number, current_balance, loyalty_card_number)
VALUES('c02001', 'brianrog@hootech.com', 'Brian', 'Rogers',
'01654564898', 50, 'lc4587' );
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
11
In this step you will populate and work with the data that is stored in the database system.
2. Obl is going to update the price of the premium bat so you will need to write a query that will close off
the current price by adding the system date values to the end_date and end_time fields. To run this
query you will need to both match the item number and identify that the end date is null. This ensures
that you are updating the latest price.
Solution :
UPDATE price_history
SET end_date = SYSDATE, end_time = SYSDATE
WHERE itm_number = 'im01101045' AND end_date is null;
3. Rerun the select statement on the price_history table to ensure that the statement has been executed.
Solution :
SELECT start_date, TO_CHAR (start_time, 'HH24:MI:SS'), price, end_date,
TO_CHAR (end_time, 'HH24:MI')
FROM price_history;
4. Insert a new row that will use the current date and time to set the new price of the premium bat to be
99.99.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
12
Solution :
INSERT INTO price_history (start_date, start_time, price, itm_number)
VALUES(SYSDATE, SYSDATE, 99.99, 'im01101048');
5. Rerun the select statement on the price_history table to ensure that the statement has been executed.
Solution :
DELETE FROM customers_addresses
WHERE id = 'ca0101';
2. Run a select statement on the customers_addresses table to ensure that the statement has been
executed.
Solution :
SELECT * FROM customers_addresses
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
13
In this step you will retrieve data that is stored in the database system by using a SELECT statement.
1. customers.
Solution:
SELECT *
FROM customers;
2. teams.
Solution:
SELECT *
FROM teams;
3. items
Solution:
SELECT *
FROM items;
3. Display the name, description and category for every item in the table.
Solution:
SELECT name, description, category
FROM items;
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
14
In this step you will retrieve data that is stored in the database system by using a SELECT statement.
2. Obl is considering giving a gift card to all its customers of 5.00 that can be used to reduce their current
balance. Write a query that will show the customers first name, last name, customer number, current
balance and the value of their balance minus the gift value.
Solution:
SELECT first_name, last_name, ctr_number, current_balance, current_balance-5
FROM customers;
Solution:
The current balance cannot go below zero.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
15
Solution:
SELECT first_name AS "First Name", last_name AS "Last Name", current_balance
AS "Balance", current_balance/12 AS "Monthly Repayments"
FROM customers;
1. Write a query that will display the team information in the following format:
The Rockets team has 25 players and receives a discount of 10 percent.
Use Team Information as the column alias.
Solution:
SELECT 'The ' || name || ' team has ' || number_of_players || ' players and receives a
discount of ' || discount || ' percent.' AS "Team Information"
FROM teams;
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
16
In this step you will refine the data that is returned in your query by adding a WHERE clause to your SELECT
statement.
Solution:
SELECT *
FROM customers
WHERE ctr_number = 'c01986 ';
2. Display the first name, last name and customer number for all customers who have a current balance
of greater than 100. Use an appropriate alias for your column headings.
Solution:
SELECT first_name AS "First Name", last_name AS "Last Name", current_balance AS
"Balance"
FROM customers
WHERE current_balance >100;
3. Display the order id, date and time of all orders that were placed before the 28th of May 2017. Use an
appropriate alias for your column headings.
Solution:
SELECT id AS "Order ID", odr_date AS "Order Date", TO_CHAR (odr_time, 'HH24:MI:SS') AS
"Order Time"
FROM orders
WHERE odr_date < '28-May-2017';
Solution :
SELECT id AS "Inventory ID", cost AS "Cost", units AS "Number of Units in Stock"
FROM inventory_list
WHERE cost BETWEEN 3 AND 15;
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
17
Solution:
SELECT id AS "Inventory ID", cost AS "Cost", units AS "Number of Units in Stock"
FROM inventory_list
WHERE units IN (50, 100, 150, 200, 250);
Solution:
SELECT id AS "Inventory ID", cost AS "Cost", units AS "Number of Units in Stock"
FROM inventory_list
WHERE units NOT IN (50, 100, 150, 200, 250);
FROM items
Exercise 6 : Pattern Matching: Combining Wildcard Characters with the LIKE Operator
1. Display item number and name of all items that have a name that contain a lowercase o. Use an
appropriate alias for your column headings.
Solution:
SELECT itm_number AS "Item ID", name AS "Item Name"
FROM items
WHERE name LIKE '%o%';
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
18
In this step you will refine the data that is returned in your query by adding a WHERE clause to your SELECT
statement.
2. Write a query that will display information for only teams that receive a discount in the following
format:
The Rockets team has 25 players and receives a discount of 10 percent.
Use Team Information as the column alias.
Solution:
SELECT 'The ' || name || ' team has ' || number_of_players || ' players and receives a
discount of ' || discount || ' percent.' AS "Team Information"
FROM teams
WHERE discount IS NOT NULL;
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
19
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
20
In this step you will sort the order of the data that is returned in your query by adding an ORDER BY clause to
the end of your SELECT statement.
1. Display the team name and number of players alphabetically in order of team name. Use an
appropriate alias for your column headings.
Solution:
SELECT name AS "Team Name", number_of_players AS "Number of Players"
FROM teams
ORDER BY name
2. Display the team name and number of players in descending order of number of players. Use an
appropriate alias for your column headings.
Solution:
SELECT name AS "Team Name", number_of_players AS "Number of Players"
FROM teams
ORDER BY number_of_players DESC;
3. Display the team name and number of players alphabetically in order of team name. Use Team Name
for the name alias and Players for the number of players. Sort the output in descending order of name
using the alias in the ORDER BY clause.
Solution:
SELECT name AS "Team Name", number_of_players AS "Number of Players"
FROM teams
ORDER BY "Team Name" DESC;
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
21
Exercise 1 : TOP-N-ANALYSIS
1. The customers are numbered sequentially with each new customer being assigned a higher customer
number. Use TOP-N-ANALYSIS to only show the First and last name of the first three customers. Show
the customers first and last name in the same column using Customer Name as the column alias.
Solution:
SELECT ROWNUM As "Order of Membership", first_name || ' ' ||last_name AS "Customer
Name"
FROM customers
WHERE ROWNUM <=3
ORDER BY ctr_number;
1. Use a substitution variable that will allow you to enter the commission rate for the sales
representatives. The first and last names should be displayed to screen for any sales representatives
that earn that commission rate and the output should be ordered by their last name. Use an
appropriate alias for your column headings.
Solution:
SELECT first_name || ' ' ||last_name AS "Sales Representative Details"
FROM sales_representatives
WHERE commision_rate = :commission_rate
ORDER BY last_name;
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
22
In this step you will write SELECT statements to access data from more than one table.
Solution:
SELECT *
FROM sales_representatives NATURAL JOIN sales_rep_addresses;
2. Adapt the query from the previous question to only show the id, first name, last name, address line 1,
address line 2, city, email and phone_number for the sales representatives.
Solution:
SELECT id, first_name, last_name, address_line_1, address_line_2, city, email, phone_number
FROM sales_representatives NATURAL JOIN sales_rep_addresses;
Solution:
SELECT id, first_name, last_name, address_line_1, address_line_2, city, email, phone_number
FROM sales_representatives JOIN sales_rep_addresses
USING (id);
2. Display all of the information about items and their price history by joining the items and price_history
tables.
Solution:
SELECT *
FROM items JOIN price_history
USING (itm_number);
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
23
Solution:
SELECT ctr_number, c.first_name, c.last_name, c.phone_number, c.email, s.id, s.first_name,
s.last_name, s.email
FROM customers c JOIN teams t
ON (t.id = c.tem.id);
Solution:
SELECT ctr_number, c.first_name, c.last_name, c.phone_number, c.email, s.id, s.first_name,
s.last_name, s.email, t.name "Team Name"
FROM customers c JOIN sales_representatives s
ON ( s.id = c.sre_id)
JOIN teams t
ON (t.id = c.tem_id);
Solution:
SELECT ctr_number, c.first_name, c.last_name, c.phone_number, c.email, s.id, s.first_name,
s.last_name, s.email, t.name "Team Name"
FROM customers c JOIN sales_representatives s
ON ( s.id = c.sre_id)
JOIN teams t
ON (t.id = c.tem_id)
WHERE c.ctr_number ='c00001';
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
24
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
25
Write SELECT Statements Using Data From Multiple Tables Using Equijoins and Non-
Equijoins
1. Write a query that will display who the supervisor is for each of the sales representatives. The
information should be displayed in two columns, the first column will be the first name and last name
of the sales representative and the second will be the first name and last name of the supervisor. The
column aliases should be Rep and Supervisor.
Solution:
SELECT r.first_name|| ' ' || r.last_name AS "Rep", s.first_name||' '|| s.last_name AS
"Supervisor"
FROM sales_representatives r JOIN sales_representatives s
ON (r.supervisor_id = s.id);
1. Write a query that will display all of the team and customer information even if there is no match with
the table on the left (team).
Solution:
SELECT *
FROM teams t RIGHT OUTER JOIN customers c
ON (t.id = c.tem_id);
1. Create a Cartesian product between the customer and sales representative tables.
Solution:
SELECT *
FROM customers CROSS JOIN sales_representatives;
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.