| 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.svg" 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 |
<details class="fancy_toc"> |
| 124 |
<summary>Table Of Contents</summary> |
| 125 |
<div id="toc_sub"><div class="fancy-toc1"><a href="#definitions">1. Definitions</a></div> |
| 126 |
<div class="fancy-toc1"><a href="#syntax">2. Syntax</a></div> |
| 127 |
<div class="fancy-toc2"><a href="#row_value_comparisons">2.1. Row Value Comparisons</a></div> |
| 128 |
<div class="fancy-toc2"><a href="#row_value_in_operators">2.2. Row Value IN Operators</a></div> |
| 129 |
<div class="fancy-toc2"><a href="#row_values_in_update_statements">2.3. Row Values In UPDATE Statements</a></div> |
| 130 |
<div class="fancy-toc1"><a href="#example_uses_of_row_values">3. Example Uses Of Row Values</a></div> |
| 131 |
<div class="fancy-toc2"><a href="#scrolling_window_queries">3.1. Scrolling Window Queries</a></div> |
| 132 |
<div class="fancy-toc2"><a href="#comparison_of_dates_stored_as_separate_fields">3.2. Comparison of dates stored as separate fields</a></div> |
| 133 |
<div class="fancy-toc2"><a href="#search_against_multi_column_keys">3.3. Search against multi-column keys</a></div> |
| 134 |
<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> |
| 135 |
<div class="fancy-toc2"><a href="#clarity_of_presentation">3.5. Clarity of presentation</a></div> |
| 136 |
<div class="fancy-toc1"><a href="#backwards_compatibility">4. Backwards Compatibility</a></div> |
| 137 |
</div> |
| 138 |
</details> |
| 139 |
</div> |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
<h1 id="definitions"><span>1. </span>Definitions</h1> |
| 146 |
|
| 147 |
<p>A "value" is a single number, string, BLOB or NULL. |
| 148 |
Sometimes the qualified name "scalar value" is used to emphasize that |
| 149 |
only a single quantity is involved. |
| 150 |
|
| 151 |
</p><p>A "row value" is an ordered list of two or more scalar values. |
| 152 |
In other words, a "row value" is a vector or tuple. |
| 153 |
|
| 154 |
</p><p>The "size" of a row value is the number of scalar values the row value contains. |
| 155 |
The size of a row value is always at least 2. |
| 156 |
A row value with a single column is just a scalar value. |
| 157 |
A row value with no columns is a syntax error. |
| 158 |
|
| 159 |
</p><h1 id="syntax"><span>2. </span>Syntax</h1> |
| 160 |
|
| 161 |
<p>SQLite allows row values to be expressed in two ways: |
| 162 |
</p><ol> |
| 163 |
<li>A parenthesized, comma-separated list of scalar values. |
| 164 |
</li><li>A subquery expression with two or more result columns. |
| 165 |
</li></ol> |
| 166 |
|
| 167 |
<p>SQLite can use row values in two contexts: |
| 168 |
</p><ol> |
| 169 |
<li>Two row values of the same size |
| 170 |
can be compared using operators <, <=, >, >=, |
| 171 |
=, <>, IS, IS NOT, IN, NOT IN, BETWEEN, or CASE. |
| 172 |
</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 |
| 173 |
the same size. |
| 174 |
</li></ol> |
| 175 |
|
| 176 |
<p>The syntax for row values and the circumstances in which row values |
| 177 |
can be used are illustrated in examples below. |
| 178 |
|
| 179 |
</p><h2 id="row_value_comparisons"><span>2.1. </span>Row Value Comparisons</h2> |
| 180 |
|
| 181 |
<p>Two row values are compared by looking at the constituent scalar |
| 182 |
values from left to right. |
| 183 |
A NULL means "unknown". |
| 184 |
The overall result of comparison is NULL if it is possible to make the |
| 185 |
result either true or false by substituting alternative values in place |
| 186 |
of the constituent NULLs. |
| 187 |
The following query demonstrates some row value comparisons: |
| 188 |
|
| 189 |
</p><div class="codeblock"><pre>SELECT |
| 190 |
(1,2,3) = (1,2,3), -- 1 |
| 191 |
(1,2,3) = (1,NULL,3), -- NULL |
| 192 |
(1,2,3) = (1,NULL,4), -- 0 |
| 193 |
(1,2,3) < (2,3,4), -- 1 |
| 194 |
(1,2,3) < (1,2,4), -- 1 |
| 195 |
(1,2,3) < (1,3,NULL), -- 1 |
| 196 |
(1,2,3) < (1,2,NULL), -- NULL |
| 197 |
(1,3,5) < (1,2,NULL), -- 0 |
| 198 |
(1,2,NULL) IS (1,2,NULL); -- 1 |
| 199 |
</pre></div> |
| 200 |
|
| 201 |
<p>The result of "(1,2,3)=(1,NULL,3)" is NULL because the result might be |
| 202 |
true if we replaced NULL→2 or false if we replaced NULL→9. |
| 203 |
The result of "(1,2,3)=(1,NULL,4)" is not NULL because there is no |
| 204 |
substitution of the constituent NULL that will make the expression true, |
| 205 |
since 3 will never equal 4 in the third column. |
| 206 |
|
| 207 |
</p><p>Any of the row values in the previous example could be replaced by a |
| 208 |
subquery that returns three columns and the same answer would result. |
| 209 |
For example: |
| 210 |
|
| 211 |
</p><div class="codeblock"><pre>CREATE TABLE t1(a,b,c); |
| 212 |
INSERT INTO t1(a,b,c) VALUES(1,2,3); |
| 213 |
SELECT (1,2,3)=(SELECT * FROM t1); -- 1 |
| 214 |
</pre></div> |
| 215 |
|
| 216 |
<a name="rvinop"></a> |
| 217 |
|
| 218 |
<h2 id="row_value_in_operators"><span>2.2. </span>Row Value IN Operators</h2> |
| 219 |
|
| 220 |
<p>For a row-value <a href="lang_expr.html#in_op">IN operator</a>, the left-hand side (hereafter "LHS") can be either |
| 221 |
a parenthesized list of values or a subquery with multiple columns. But the |
| 222 |
right-hand side (hereafter "RHS") must be a subquery expression. |
| 223 |
|
| 224 |
</p><div class="codeblock"><pre>CREATE TABLE t2(x,y,z); |
| 225 |
INSERT INTO t2(x,y,z) VALUES(1,2,3),(2,3,4),(1,NULL,5); |
| 226 |
SELECT |
| 227 |
(1,2,3) IN (SELECT * FROM t2), -- 1 |
| 228 |
(7,8,9) IN (SELECT * FROM t2), -- 0 |
| 229 |
(1,3,5) IN (SELECT * FROM t2); -- NULL |
| 230 |
</pre></div> |
| 231 |
|
| 232 |
<h2 id="row_values_in_update_statements"><span>2.3. </span>Row Values In UPDATE Statements</h2> |
| 233 |
|
| 234 |
<p>Row values can also be used in the SET clause of an <a href="lang_update.html">UPDATE</a> statement. |
| 235 |
The LHS must be a list of column names. The RHS can be any row value. |
| 236 |
For example: |
| 237 |
|
| 238 |
</p><div class="codeblock"><pre>UPDATE tab3 |
| 239 |
SET (a,b,c) = (SELECT x,y,z |
| 240 |
FROM tab4 |
| 241 |
WHERE tab4.w=tab3.d) |
| 242 |
WHERE tab3.e BETWEEN 55 AND 66; |
| 243 |
</pre></div> |
| 244 |
|
| 245 |
<h1 id="example_uses_of_row_values"><span>3. </span>Example Uses Of Row Values</h1> |
| 246 |
|
| 247 |
<h2 id="scrolling_window_queries"><span>3.1. </span>Scrolling Window Queries</h2> |
| 248 |
|
| 249 |
<p>Suppose an application wants to display a list of contacts |
| 250 |
in alphabetical order by lastname, firstname, in a scrolling window |
| 251 |
that can only show 7 contacts at a time. Initializing the scrolling |
| 252 |
window to the first 7 entries is easy: |
| 253 |
|
| 254 |
</p><div class="codeblock"><pre>SELECT * FROM contacts |
| 255 |
ORDER BY lastname, firstname |
| 256 |
LIMIT 7; |
| 257 |
</pre></div> |
| 258 |
|
| 259 |
<p>When the user scrolls down, the application needs to find the |
| 260 |
second set of 7 entries. One way to do this is to use the OFFSET clause: |
| 261 |
|
| 262 |
</p><div class="codeblock"><pre>SELECT * FROM contacts |
| 263 |
ORDER BY lastname, firstname |
| 264 |
LIMIT 7 OFFSET 7; |
| 265 |
</pre></div> |
| 266 |
|
| 267 |
<p>OFFSET gives the correct answer. However, OFFSET requires time |
| 268 |
proportional to the offset value. What really happens |
| 269 |
with "LIMIT x OFFSET y" is that SQLite computes the query as |
| 270 |
"LIMIT x+y" and discards the first y values without returning them |
| 271 |
to the application. So as the window scrolls down toward |
| 272 |
the bottom of a long list, and the y value becomes larger and larger, |
| 273 |
successive offset computations take more and more time. |
| 274 |
|
| 275 |
</p><p>A more efficient approach is to remember the last entry currently |
| 276 |
displayed and then use a row value comparison in the WHERE |
| 277 |
clause: |
| 278 |
|
| 279 |
</p><div class="codeblock"><pre>SELECT * FROM contacts |
| 280 |
WHERE (lastname,firstname) > (?1,?2) |
| 281 |
ORDER BY lastname, firstname |
| 282 |
LIMIT 7; |
| 283 |
</pre></div> |
| 284 |
|
| 285 |
<p>If the lastname and firstname on the bottom row of the previous |
| 286 |
screen are bound to ?1 and ?2, then the query above computes the next |
| 287 |
7 rows. And, assuming there is an appropriate index, it does so |
| 288 |
very efficiently — much more efficiently than OFFSET. |
| 289 |
|
| 290 |
</p><h2 id="comparison_of_dates_stored_as_separate_fields"><span>3.2. </span>Comparison of dates stored as separate fields</h2> |
| 291 |
|
| 292 |
<p>The usual way of storing a date in a database table is as a single |
| 293 |
field, as either a unix timestamp, a julian day number, or an ISO-8601 |
| 294 |
dates string. But some applications store dates as three separate |
| 295 |
fields for the year, month, and day. |
| 296 |
|
| 297 |
</p><div class="codeblock"><pre>CREATE TABLE info( |
| 298 |
year INT, -- 4 digit year |
| 299 |
month INT, -- 1 through 12 |
| 300 |
day INT, -- 1 through 31 |
| 301 |
other_stuff BLOB -- blah blah blah |
| 302 |
); |
| 303 |
</pre></div> |
| 304 |
|
| 305 |
<p>When dates are stored this way, row value comparisons provide a |
| 306 |
convenient way to compare dates: |
| 307 |
|
| 308 |
</p><div class="codeblock"><pre>SELECT * FROM info |
| 309 |
WHERE (year,month,day) BETWEEN (2015,9,12) AND (2016,9,12); |
| 310 |
</pre></div> |
| 311 |
|
| 312 |
<h2 id="search_against_multi_column_keys"><span>3.3. </span>Search against multi-column keys</h2> |
| 313 |
|
| 314 |
<p>Suppose we want to know the order number, product number, and quantity |
| 315 |
for any item in which the product number and quantity match the product |
| 316 |
number and quantity of any item in order number 365: |
| 317 |
|
| 318 |
</p><div class="codeblock"><pre>SELECT ordid, prodid, qty |
| 319 |
FROM item |
| 320 |
WHERE (prodid, qty) IN (SELECT prodid, qty |
| 321 |
FROM item |
| 322 |
WHERE ordid = 365); |
| 323 |
</pre></div> |
| 324 |
|
| 325 |
<p>The query above could be rewritten as a join and without the use |
| 326 |
of row values: |
| 327 |
|
| 328 |
</p><div class="codeblock"><pre>SELECT t1.ordid, t1.prodid, t1.qty |
| 329 |
FROM item AS t1, item AS t2 |
| 330 |
WHERE t1.prodid=t2.prodid |
| 331 |
AND t1.qty=t2.qty |
| 332 |
AND t2.ordid=365; |
| 333 |
</pre></div> |
| 334 |
|
| 335 |
<p>Because the same query could be written without the use of row values, |
| 336 |
row values do not provide new capabilities. However, many developers say |
| 337 |
that the row value format is easier to read, write, and debug. |
| 338 |
|
| 339 |
</p><p>Even in the JOIN form, the query can be made clearer through the use of |
| 340 |
row values: |
| 341 |
|
| 342 |
</p><div class="codeblock"><pre>SELECT t1.ordid, t1.prodid, t1.qty |
| 343 |
FROM item AS t1, item AS t2 |
| 344 |
WHERE (t1.prodid,t1.qty) = (t2.prodid,t2.qty) |
| 345 |
AND t2.ordid=365; |
| 346 |
</pre></div> |
| 347 |
|
| 348 |
<p>This later query generates exactly the same <a href="opcode.html">bytecode</a> as the previous |
| 349 |
scalar formulation, but using syntax that is cleaner and |
| 350 |
easier to read. |
| 351 |
|
| 352 |
</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> |
| 353 |
|
| 354 |
<p>The row-value notation is useful for updating two or more columns |
| 355 |
of a table from the result of a single query. |
| 356 |
An example of this is in the full-text search feature of the |
| 357 |
<a href="https://www.fossil-scm.org/">Fossil version control system</a>. |
| 358 |
|
| 359 |
</p><p>In the Fossil full-text search system, |
| 360 |
documents that participate in the full-text search (wiki pages, tickets, |
| 361 |
check-ins, documentation files, etc) are tracked by a table called |
| 362 |
"ftsdocs" (<u>f</u>ull <u>t</u>ext <u>s</u>earch <u>doc</u>ument<u>s</u>). |
| 363 |
As new documents are added to the repository, they are not indexed right |
| 364 |
away. Indexing is deferred until there is a search request. The |
| 365 |
ftsdocs table contains an "idxed" field which is true if the document |
| 366 |
has been indexed and false if not. |
| 367 |
|
| 368 |
</p><p>When a search request occurs and pending documents are indexed for the |
| 369 |
first time, the ftsdocs table must be updated by setting the idxed column |
| 370 |
to true and also filling in several other columns with information pertinent |
| 371 |
to the search. That other information is obtained from a join. The |
| 372 |
query is this: |
| 373 |
|
| 374 |
</p><div class="codeblock"><pre>UPDATE ftsdocs SET |
| 375 |
idxed=1, |
| 376 |
name=NULL, |
| 377 |
(label,url,mtime) = |
| 378 |
(SELECT printf('Check-in [%%.16s] on %%s',blob.uuid, |
| 379 |
datetime(event.mtime)), |
| 380 |
printf('/timeline?y=ci&c=%%.20s',blob.uuid), |
| 381 |
event.mtime |
| 382 |
FROM event, blob |
| 383 |
WHERE event.objid=ftsdocs.rid |
| 384 |
AND blob.rid=ftsdocs.rid) |
| 385 |
WHERE ftsdocs.type='c' AND NOT ftsdocs.idxed |
| 386 |
</pre></div> |
| 387 |
|
| 388 |
<p>(See the |
| 389 |
<a href="https://www.fossil-scm.org/fossil/artifact/e5d6a82d?ln=1594-1605">source code</a> |
| 390 |
for further detail. Other examples |
| 391 |
<a href="https://www.fossil-scm.org/fossil/artifact/e5d6a82d?ln=1618-1628">here</a> and |
| 392 |
<a href="https://www.fossil-scm.org/fossil/artifact/e5d6a82d?ln=1641-1650">here</a>.) |
| 393 |
|
| 394 |
</p><p>Five out of nine columns in the ftsdocs table are updated. Two of |
| 395 |
the modified columns, "idxed" and "name", can be updated independently of |
| 396 |
the query. But the three columns "label", "url", and "mtime" all require |
| 397 |
a join query against the "event" and "blob" tables. Without row values, |
| 398 |
the equivalent UPDATE would require that the join be repeated three times, |
| 399 |
once for each column to be updated. |
| 400 |
|
| 401 |
</p><h2 id="clarity_of_presentation"><span>3.5. </span>Clarity of presentation</h2> |
| 402 |
|
| 403 |
<p>Sometimes the use of row values just makes the SQL easier to read |
| 404 |
and write. Consider the following two UPDATE statements: |
| 405 |
|
| 406 |
</p><div class="codeblock"><pre>UPDATE tab1 SET (a,b)=(b,a); |
| 407 |
UPDATE tab1 SET a=b, b=a; |
| 408 |
</pre></div> |
| 409 |
|
| 410 |
<p>Both UPDATE statements do exactly the same thing. (They generate |
| 411 |
identical <a href="opcode.html">bytecode</a>.) But the first form, the row value form, seems |
| 412 |
to make it clearer that the intent of the statement is to swap the |
| 413 |
values in columns A and B. |
| 414 |
|
| 415 |
</p><p>Or consider these identical queries: |
| 416 |
|
| 417 |
</p><div class="codeblock"><pre>SELECT * FROM tab1 WHERE a=?1 AND b=?2; |
| 418 |
SELECT * FROM tab1 WHERE (a,b)=(?1,?2); |
| 419 |
</pre></div> |
| 420 |
|
| 421 |
<p>Once again, the SQL statements generate identical bytecode and thus |
| 422 |
do exactly the same job in exactly the same way. But the second form |
| 423 |
is made easier for humans to read by grouping the query parameters together |
| 424 |
into a single row value rather than scattering them across the WHERE |
| 425 |
clause. |
| 426 |
|
| 427 |
</p><h1 id="backwards_compatibility"><span>4. </span>Backwards Compatibility</h1> |
| 428 |
|
| 429 |
<p>Row values were added to SQLite |
| 430 |
<a href="releaselog/3_15_0.html">version 3.15.0</a> (2016-10-14). Attempts to use row values in |
| 431 |
prior versions of SQLite will generate syntax errors. |
| 432 |
</p><p align="center"><small><i>This page was last updated on 2025-05-31 13:08:22Z </small></i></p> |
| 433 |
|