CS157a
Chris Pollett
Dec 4, 2023
SELECT ExtractValue('<a><b>X</b><b>Y</b><b>Z</b></a>','//b[2]');would return Y and
SELECT UpdateXML('<a><b>CAT</b><d></d></a>', '/a', '<e>FOX</e>') AS val1, UpdateXML('<a><b>CAT</b><d></d></a>', '/b', '<e>FOX</e>') AS val2, UpdateXML('<a><b>CAT</b><d></d></a>', '//b', '<e>FOX</e>') AS val3, UpdateXML('<a><b>CAT</b><d></d></a>', '/a/d', '<e>FOX</e>') AS val4 , UpdateXML('<a><d></d><b>CAT</b><d></d></a>', '/a/d', '<e>FOX</e>') AS val5 \G *************************** 1. row *************************** val1: <e>FOX</e> val2: <a><b>CAT</b><d></d></a> val3: <a><e>FOX</e><d></d></a> val4: <a><b>CAT</b><e>FOX</e></a> val5: <a><d></d><b>CAT</b><d></d></a>
Which of the following is true?
CREATE TABLE PRODUCT( PRODNR CHAR(6) NOT NULL PRIMARY KEY, PRODNAME VARCHAR(60) NOT NULL, PRODTYPE VARCHAR(15), AVAILABLE_QUANTITY INTEGER, REVIEW XML);
INSERT INTO PRODUCT VALUES('000120', 'Conundrum', 'white', 12, XMLPARSE(CONTENT '<review><author>Bart Baesens</author> <date>27/02/2017</date> <description>This is an excellent white wine with intriguing aromas of green apple, tangerine and honeysuckle blossoms. </description><rating max-value="100">94</rating> </review>'));
SELECT xmlelement(name foo, xmlattributes(current_date as bar), 'content'); outputs <foo bar="2020-09-09">content</foo>
SELECT xmlforest('abc' AS foo, 123 AS bar); xmlforest ------------------------------ <foo>abc</foo><bar>123</bar>
CREATE TABLE test (y int, x xml); INSERT INTO test VALUES (1, '<foo>abc</foo>'); INSERT INTO test VALUES (2, '<bar/>'); SELECT xmlagg(x) FROM test; xmlagg ---------------------- <foo>abc</foo><bar/>This can also work with an ORDER BY clause:
SELECT xmlagg(x ORDER BY y DESC) FROM test; xmlagg ---------------------- <bar/><foo>abc</foo>
doc("winecellar.xml")/winecellar/winematches all wine nodes (and their contents) in the winecellar.xml file which start from the whole document, then immediately go through a winecellar sub-tag then have a wine tag.
doc("winecellar.xml")/winecellar/wine[2]
doc("winecellar.xml")/winecellar/wine[price > 20]/namereturns the names of all wine nodes with price attribute greater than 20.
SELECT XMLEXISTS('//town[text() = ''Toronto'']' PASSING BY REF '<towns><town>Toronto</town><town>Ottawa</town></towns>');would return t (for true).
SELECT XPATH('//author', REVIEW) FROM PRODUCT;
SELECT xpath('//author/text()', REVIEW) FROM PRODUCT WHERE CAST(xpath('//author/text()', REVIEW) AS TEXT) LIKE '%B%';
12.3 -- an example Number "hi there" -- an example String true -- an example Boolean, other possibility false
[1, 4, 9]
{"bob": 29, "sally" : 35}
myObj = eval( "(" + data + ")" );
CREATE TABLE CART ( id SERIAL PRIMARY KEY, details JSON NOT NULL );A serial column will count up for us if we don't bother to include it in inserts (like Mysql AUTOINCREMENT)
INSERT INTO CART (details) VALUES('{ "customer": "Chris Pollett", "items": {"product": "computer","qty": 1}}'), ('{ "customer": "Sponge Bob", "items": {"product": "pineapple","qty": 5}}');Here we are inserting two tuples into the table.
SELECT details -> 'customer' AS customer FROM CART; SELECT details ->> 'customer' AS customer FROM CART;The -> operator gets a JSON object out, the ->> operator retrieves a specific node as text.
SELECT details -> 'items' ->> 'product' as product FROM CART ORDER BY PRODUCT;
SELECT details ->> 'customer' AS customer FROM CART WHERE details -> 'items' ->> 'product' = 'pineapple';
SELECT details ->> 'customer' AS customer, details -> 'items' ->> 'product' AS product FROM CART WHERE CAST ( details-> 'items' ->> 'qty' AS INTEGER) = 5
SELECT MIN (CAST (details -> 'items' ->> 'qty' AS INTEGER)), MAX (CAST (details -> 'items' ->> 'qty' AS INTEGER)), SUM (CAST (details -> 'items' ->> 'qty' AS INTEGER)), AVG (CAST (details -> 'items' ->> 'qty' AS INTEGER)) FROM CART;
min | max | sum | avg -----+-----+-----+-------------------- 1 | 5 | 6 | 3.0000000000000000
SELECT json_each (details) FROM CART;would output:
json_each --------------------------------------------------- (customer,"""Chris Pollett""") (items,"{""product"": ""computer"",""qty"": 1}") (customer,"""Sponge Bob""") (items,"{""product"": ""pineapple"",""qty"": 5}") (4 rows)
SELECT json_object_keys (details->'items') FROM CART;would output:
json_object_keys ------------------ product qty product qty (4 rows)
SELECT json_typeof (details->'items'->'qty') FROM CART;would output:
json_typeof ------------- number number (2 rows)