DROP TABLE Customers;
DROP Table Stores;
-- Part A --
CREATE TABLE Customers (
AcctID Number(4),
custxml XMLType
);
INSERT INTO Customers VALUES (9545, XMLType('<customer>
<AcctID>9545</AcctID>
<Fname>Lee</Fname>
<Minit>K.</Minit>
<Lname>Smith</Lname>
<Store>Zappos</Store>
<OrderDate></OrderDate>
<email>smith.lee@aigmail.com</email>
</customer>'));
INSERT INTO Customers VALUES (9387, XMLType('<customer>
<AcctID>9387</AcctID>
<Fname>Rudy</Fname>
<Minit>S.</Minit>
<Lname>Wolf</Lname>
<Store>Amazon</Store>
<OrderDate>Oct-24-2008</OrderDate>
<email>rswolf@mymail.com</email>
</customer>'));
INSERT INTO Customers VALUES (9122, XMLType('<customer>
<AcctID>9122</AcctID>
<Fname>Sue</Fname>
<Minit>S.</Minit>
<Lname>Sky</Lname>
<Store>Amazon</Store>
<OrderDate>Jan-25-2009</OrderDate>
<email>sssky@ua.edu</email>
</customer>'));
CREATE TABLE Stores (
storexml XMLType
);
INSERT INTO Stores VALUES (XMLType('<store>
<Name>Amazon</Name>
<Type>General</Type>
<Location>Shelby</Location>
</store>'));
INSERT INTO Stores VALUES (XMLType('<store>
<Name>Zappos</Name>
<Type>Shoes</Type>
<Location>Houser</Location>
</store>'));
-- Part B: See if you must place a null for the OrderDate
INSERT INTO Customers VALUES (2222, XMLType('<customer>
<AcctID>2222</AcctID>
<Fname>Bob</Fname>
<Minit>S.</Minit>
<Lname>Wormwood</Lname>
<Store>Amazon</Store>
-- OMITTED ORDERDATE
<email>bswormwood@ua.edu</email>
</customer>'));
-- 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;