DROP TABLE Customers;
DROP Table Stores;
-- Part A --
CREATE TABLE Customers (
AcctID Number(4),
custxml XMLType
);
INSERT INTO Customers VALUES (9545, XMLType('
9545
Lee
K.
Smith
Zappos
smith.lee@aigmail.com
'));
INSERT INTO Customers VALUES (9387, XMLType('
9387
Rudy
S.
Wolf
Amazon
Oct-24-2008
rswolf@mymail.com
'));
INSERT INTO Customers VALUES (9122, XMLType('
9122
Sue
S.
Sky
Amazon
Jan-25-2009
sssky@ua.edu
'));
CREATE TABLE Stores (
storexml XMLType
);
INSERT INTO Stores VALUES (XMLType('
Amazon
General
Shelby
'));
INSERT INTO Stores VALUES (XMLType('
Zappos
Shoes
Houser
'));
-- Part B: See if you must place a null for the OrderDate
INSERT INTO Customers VALUES (2222, XMLType('
2222
Bob
S.
Wormwood
Amazon
-- OMITTED ORDERDATE
bswormwood@ua.edu
'));
-- Error during XPath?
SELECT AcctID,
extractValue(custxml, '/customer/OrderDate') "Order date"
FROM Customers;
-- Answer: Looks OK, though extractValue() returns NULL when
-- you omit the node in total.
-- Cleanup
DELETE FROM Customers WHERE AcctID = 2222;
-- Part C: List all of the data using select *
SELECT * FROM Customers;
SELECT * FROM Stores;
-- Part D: List the lname of the customer with AcctID=9122
SELECT extractValue(custxml, '/customer/Lname') "Last names"
FROM Customers
WHERE extractValue(custxml, '/customer/AcctID') = '9122';
-- Part E: List AcctIDs of all customers who have accounts with Amazon
SELECT extractValue(custxml, '/customer/AcctID') "Amazon Customers"
FROM Customers
WHERE extractValue(custxml, '/customer/Store') = 'Amazon';
SELECT extractValue(custxml, '/customer/Lname') "General store customers"
FROM Customers, Stores
WHERE extractValue(custxml, '/customer/Store')
= extractValue(storexml, '/store/Name')
AND extractValue(storexml, '/store/Type') = 'General';
-- Part G: List the OrderDate number for all customers
SELECT
CONCAT(extractValue(custxml, '/customer/Fname'),
extractValue(custxml, '/customer/Lname')) "Full name",
extractValue(custxml, '/customer/OrderDate') "Order date"
FROM Customers;