Debian Codegraph @

www/rowvalue.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>Row Values</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 Row Values
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#definitions">1. Definitions</a></div>
129 <div class="fancy-toc1"><a href="#syntax">2. Syntax</a></div>
130 <div class="fancy-toc2"><a href="#row_value_comparisons">2.1. Row Value Comparisons</a></div>
131 <div class="fancy-toc2"><a href="#row_value_in_operators">2.2. Row Value IN Operators</a></div>
132 <div class="fancy-toc2"><a href="#row_values_in_update_statements">2.3. Row Values In UPDATE Statements</a></div>
133 <div class="fancy-toc1"><a href="#example_uses_of_row_values">3. Example Uses Of Row Values</a></div>
134 <div class="fancy-toc2"><a href="#scrolling_window_queries">3.1. Scrolling Window Queries</a></div>
135 <div class="fancy-toc2"><a href="#comparison_of_dates_stored_as_separate_fields">3.2. Comparison of dates stored as separate fields</a></div>
136 <div class="fancy-toc2"><a href="#search_against_multi_column_keys">3.3. Search against multi-column keys</a></div>
137 <div class="fancy-toc2"><a href="#update_multiple_columns_of_a_table_based_on_a_query">3.4. Update multiple columns of a table based on a query</a></div>
138 <div class="fancy-toc2"><a href="#clarity_of_presentation">3.5. Clarity of presentation</a></div>
139 <div class="fancy-toc1"><a href="#backwards_compatibility">4. Backwards Compatibility</a></div>
140 </div>
141 </div>
142 <script>
143 function toggle_toc(){
144 var sub = document.getElementById("toc_sub")
145 var mk = document.getElementById("toc_mk")
146 if( sub.style.display!="block" ){
147 sub.style.display = "block";
148 mk.innerHTML = "&#x25bc;";
149 } else {
150 sub.style.display = "none";
151 mk.innerHTML = "&#x25ba;";
152 }
153 }
154 </script>
155 </div>
156
157
158
159
160
161 <h1 id="definitions"><span>1. </span>Definitions</h1>
162
163 <p>A "value" is a single number, string, BLOB or NULL.
164 Sometimes the qualified name "scalar value" is used to emphasize that
165 only a single quantity is involved.
166
167 </p><p>A "row value" is an ordered list of two or more scalar values.
168 In other words, a "row value" is a vector or tuple.
169
170 </p><p>The "size" of a row value is the number of scalar values the row value contains.
171 The size of a row value is always at least 2.
172 A row value with a single column is just a scalar value.
173 A row value with no columns is a syntax error.
174
175 </p><h1 id="syntax"><span>2. </span>Syntax</h1>
176
177 <p>SQLite allows row values to be expressed in two ways:
178 </p><ol>
179 <li>A parenthesized, comma-separated list of scalar values.
180 </li><li>A subquery expression with two or more result columns.
181 </li></ol>
182
183 <p>SQLite can use row values in two contexts:
184 </p><ol>
185 <li>Two row values of the same size
186 can be compared using operators &lt;, &lt;=, &gt;, &gt;=,
187 =, &lt;&gt;, IS, IS NOT, IN, NOT IN, BETWEEN, or CASE.
188 </li><li>In an <a href="lang_update.html">UPDATE</a> statement, a list of column names can be set to a row value of
189 the same size.
190 </li></ol>
191
192 <p>The syntax for row values and the circumstances in which row values
193 can be used are illustrated in examples below.
194
195 </p><h2 id="row_value_comparisons"><span>2.1. </span>Row Value Comparisons</h2>
196
197 <p>Two row values are compared by looking at the constituent scalar
198 values from left to right.
199 A NULL means of "unknown".
200 The overall result of comparison is NULL if it is possible to make the
201 result either true or false by substituting alternative values in place
202 of the constituent NULLs.
203 The following query demonstrates some row value comparisons:
204
205 </p><div class="codeblock"><pre>SELECT
206 (1,2,3) = (1,2,3), -- 1
207 (1,2,3) = (1,NULL,3), -- NULL
208 (1,2,3) = (1,NULL,4), -- 0
209 (1,2,3) &lt; (2,3,4), -- 1
210 (1,2,3) &lt; (1,2,4), -- 1
211 (1,2,3) &lt; (1,3,NULL), -- 1
212 (1,2,3) &lt; (1,2,NULL), -- NULL
213 (1,3,5) &lt; (1,2,NULL), -- 0
214 (1,2,NULL) IS (1,2,NULL); -- 1
215 </pre></div>
216
217 <p>The result of "(1,2,3)=(1,NULL,3)" is NULL because the result might be
218 true if we replaced NULL&rarr;2 or false if we replaced NULL&rarr;9.
219 The result of "(1,2,3)=(1,NULL,4)" is not NULL because there is no
220 substitutions of the constituent NULL that will make the expression true,
221 since 3 will never equal 4 in the third column.
222
223 </p><p>Any of the row values in the previous example could be replace by a
224 subquery that returns three columns and the same answer would result.
225 For example:
226
227 </p><div class="codeblock"><pre>CREATE TABLE t1(a,b,c);
228 INSERT INTO t1(a,b,c) VALUES(1,2,3);
229 SELECT (1,2,3)=(SELECT * FROM t1); -- 1
230 </pre></div>
231
232 <a name="rvinop"></a>
233
234 <h2 id="row_value_in_operators"><span>2.2. </span>Row Value IN Operators</h2>
235
236 <p>For a row-value <a href="lang_expr.html#in_op">IN operator</a>, the left-hand side (hereafter "LHS") can be either
237 a parenthesized list of values or a subquery with multiple columns. But the
238 right-hand side (hereafter "RHS") must be a subquery expression.
239
240 </p><div class="codeblock"><pre>CREATE TABLE t2(x,y,z);
241 INSERT INTO t2(x,y,z) VALUES(1,2,3),(2,3,4),(1,NULL,5);
242 SELECT
243 (1,2,3) IN (SELECT * FROM t2), -- 1
244 (7,8,9) IN (SELECT * FROM t2), -- 0
245 (1,3,5) IN (SELECT * FROM t2); -- NULL
246 </pre></div>
247
248 <h2 id="row_values_in_update_statements"><span>2.3. </span>Row Values In UPDATE Statements</h2>
249
250 <p>Row values can also be used in the SET clause of an <a href="lang_update.html">UPDATE</a> statement.
251 The LHS must be a list of column names. The RHS can be any row value.
252 For example:
253
254 </p><div class="codeblock"><pre>UPDATE tab3
255 SET (a,b,c) = (SELECT x,y,z
256 FROM tab4
257 WHERE tab4.w=tab3.d)
258 WHERE tab3.e BETWEEN 55 AND 66;
259 </pre></div>
260
261 <h1 id="example_uses_of_row_values"><span>3. </span>Example Uses Of Row Values</h1>
262
263 <h2 id="scrolling_window_queries"><span>3.1. </span>Scrolling Window Queries</h2>
264
265 <p>Suppose an application wants to display a list of contacts
266 in alphabetical order by lastname, firstname, in a scrolling window
267 that can only show 7 contacts at a time. Initialize the scrolling
268 window to the first 7 entries is easy:
269
270 </p><div class="codeblock"><pre>SELECT * FROM contacts
271 ORDER BY lastname, firstname
272 LIMIT 7;
273 </pre></div>
274
275 <p>When the user scrolls down, the application needs to find the
276 second set of 7 entries. One way to do this is to use the OFFSET clause:
277
278 </p><div class="codeblock"><pre>SELECT * FROM contacts
279 ORDER BY lastname, firstname
280 LIMIT 7 OFFSET 7;
281 </pre></div>
282
283 <p>OFFSET gives the correct answer. However, OFFSET requires time
284 proportional to the offset value. What really happens
285 with "LIMIT x OFFSET y" is that SQLite computes the query as
286 "LIMIT x+y" and discards the first y values without returning them
287 to the application. So as the window scrolls down toward
288 the bottom of a long list, and the y value becomes larger and larger,
289 successive offset computations take more and more time.
290
291 </p><p>A more efficient approach is to remember the last entry currently
292 displayed and then use a row value comparison in the WHERE
293 clause:
294
295 </p><div class="codeblock"><pre>SELECT * FROM contacts
296 WHERE (lastname,firstname) &gt; (?1,?2)
297 ORDER BY lastname, firstname
298 LIMIT 7;
299 </pre></div>
300
301 <p>If the lastname and firstname on the bottom row of the previous
302 screen are bound to ?1 and ?2, then the query above computes the next
303 7 rows. And, assuming there is an appropriate index, it does so
304 very efficiently &mdash; much more efficiently than OFFSET.
305
306 </p><h2 id="comparison_of_dates_stored_as_separate_fields"><span>3.2. </span>Comparison of dates stored as separate fields</h2>
307
308 <p>The usual way of storing a date in a database table is as a single
309 field, as either a unix timestamp, a julian day number, or an ISO-8601
310 dates string. But some application store dates as three separate
311 fields for the year, month, and day.
312
313 </p><div class="codeblock"><pre>CREATE TABLE info(
314 year INT, -- 4 digit year
315 month INT, -- 1 through 12
316 day INT, -- 1 through 31
317 other_stuff BLOB -- blah blah blah
318 );
319 </pre></div>
320
321 <p>When dates are stored this way, row value comparisons provide a
322 convenient way to compare dates:
323
324 </p><div class="codeblock"><pre>SELECT * FROM info
325 WHERE (year,month,day) BETWEEN (2015,9,12) AND (2016,9,12);
326 </pre></div>
327
328 <h2 id="search_against_multi_column_keys"><span>3.3. </span>Search against multi-column keys</h2>
329
330 <p>Suppose we want to know the order number, product number, and quantity
331 for any item in which the product number and quantity match the product
332 number and quantity of any item in order number 365:
333
334 </p><div class="codeblock"><pre>SELECT ordid, prodid, qty
335 FROM item
336 WHERE (prodid, qty) IN (SELECT prodid, qty
337 FROM item
338 WHERE ordid = 365);
339 </pre></div>
340
341 <p>The query above could be rewritten as a join and without the use
342 of row values:
343
344 </p><div class="codeblock"><pre>SELECT t1.ordid, t1.prodid, t1.qty
345 FROM item AS t1, item AS t2
346 WHERE t1.prodid=t2.prodid
347 AND t1.qty=t2.qty
348 AND t2.ordid=365;
349 </pre></div>
350
351 <p>Because the same query could be written without the use of row values,
352 row values do not provide new capabilities. However, many developers say
353 that the row value format is easier to read, write, and debug.
354
355 </p><p>Even in the JOIN form, the query can be made clearer through the use of
356 row values:
357
358 </p><div class="codeblock"><pre>SELECT t1.ordid, t1.prodid, t1.qty
359 FROM item AS t1, item AS t2
360 WHERE (t1.prodid,t1.qty) = (t2.prodid,t2.qty)
361 AND t2.ordid=365;
362 </pre></div>
363
364 <p>This later query generates exactly the same <a href="opcode.html">bytecode</a> as the previous
365 scalar formulation, but using syntax that it cleaner and
366 easier to read.
367
368 </p><h2 id="update_multiple_columns_of_a_table_based_on_a_query"><span>3.4. </span>Update multiple columns of a table based on a query</h2>
369
370 <p>The row-value notation is useful for updating two or more columns
371 of a table from the result of a single query.
372 An example of this is in the full-text search feature of the
373 <a href="https://www.fossil-scm.org/">Fossil version control system</a>.
374
375 </p><p>In the Fossil full-text search system,
376 documents that participate in the full-text search (wiki pages, tickets,
377 check-ins, documentation files, etc) are tracked by a table called
378 "ftsdocs" (<u>f</u>ull <u>t</u>ext <u>s</u>earch <u>doc</u>ument<u>s</u>).
379 As new documents are added to the repository, they are not indexed right
380 away. Indexing is deferred until there is a search request. The
381 ftsdocs table contains an "idxed" field which is true if the document
382 has been indexed and false if not.
383
384 </p><p>When a search request occurs and pending documents are indexed for the
385 first time, the ftsdocs table must be updated by setting the idxed column
386 to true and also filling in several other columns with information pertinent
387 to the search. That other information is obtained from a join. The
388 query is this:
389
390 </p><div class="codeblock"><pre>UPDATE ftsdocs SET
391 idxed=1,
392 name=NULL,
393 (label,url,mtime) =
394 (SELECT printf('Check-in &#91;%%.16s&#93; on %%s',blob.uuid,
395 datetime(event.mtime)),
396 printf('/timeline?y=ci&amp;c=%%.20s',blob.uuid),
397 event.mtime
398 FROM event, blob
399 WHERE event.objid=ftsdocs.rid
400 AND blob.rid=ftsdocs.rid)
401 WHERE ftsdocs.type='c' AND NOT ftsdocs.idxed
402 </pre></div>
403
404 <p>(See the
405 <a href="https://www.fossil-scm.org/fossil/artifact/e5d6a82d?ln=1594-1605">source code</a>
406 for further detail. Other examples
407 <a href="https://www.fossil-scm.org/fossil/artifact/e5d6a82d?ln=1618-1628">here</a> and
408 <a href="https://www.fossil-scm.org/fossil/artifact/e5d6a82d?ln=1641-1650">here</a>.)
409
410 </p><p>Five out of nine columns in the ftsdocs table are updated. Two of
411 the modified columns, "idxed" and "name", can be updated independently of
412 the query. But the three columns "label", "url", and "mtime" all require
413 a join query against the "event" and "blob" tables. Without row values,
414 the equivalent UPDATE would require that the join be repeated three times,
415 once for each column to be updated.
416
417 </p><h2 id="clarity_of_presentation"><span>3.5. </span>Clarity of presentation</h2>
418
419 <p>Sometimes the use of row values just makes the SQL easier to read
420 and write. Consider the following two UPDATE statements:
421
422 </p><div class="codeblock"><pre>UPDATE tab1 SET (a,b)=(b,a);
423 UPDATE tab1 SET a=b, b=a;
424 </pre></div>
425
426 <p>Both UPDATE statements do exactly the same thing. (They generate
427 identical <a href="opcode.html">bytecode</a>.) But the first form, the row value form, seems
428 to make it clearer that the intent of the statement is to swap the
429 values in columns A and B.
430
431 </p><p>Or consider these identical queries:
432
433 </p><div class="codeblock"><pre>SELECT * FROM tab1 WHERE a=?1 AND b=?2;
434 SELECT * FROM tab1 WHERE (a,b)=(?1,?2);
435 </pre></div>
436
437 <p>Once again, the SQL statements generate identical bytecode and thus
438 do exactly the same job in exactly the same way. But the second form
439 is made easier for humans to read by grouping the query parameters together
440 into a single row value rather than scattering them across the WHERE
441 clause.
442
443 </p><h1 id="backwards_compatibility"><span>4. </span>Backwards Compatibility</h1>
444
445 <p>Row values were added to SQLite
446 <a href="releaselog/3_15_0.html">version 3.15.0</a> (2016-10-14). Attempts to use row values in
447 prior versions of SQLite will generate syntax errors.
448 </p><p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/rowvalue.in?m=97d4d2b36b">2022-04-18 02:55:50</a> UTC </small></i></p>
449