| 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>Quirks, Caveats, and Gotchas In SQLite</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 |
Quirks, Caveats, and Gotchas In SQLite |
| 122 |
</div> |
| 123 |
<div class="fancy_toc"> |
| 124 |
<a onclick="toggle_toc()"> |
| 125 |
<span class="fancy_toc_mark" id="toc_mk">►</span> |
| 126 |
Table Of Contents |
| 127 |
</a> |
| 128 |
<div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div> |
| 129 |
<div class="fancy-toc1"><a href="#sqlite_is_embedded_not_client_server">2. SQLite Is Embedded, Not Client-Server</a></div> |
| 130 |
<div class="fancy-toc1"><a href="#flexible_typing">3. Flexible Typing</a></div> |
| 131 |
<div class="fancy-toc2"><a href="#no_separate_boolean_datatype">3.1. No Separate BOOLEAN Datatype</a></div> |
| 132 |
<div class="fancy-toc2"><a href="#no_separate_datetime_datatype">3.2. No Separate DATETIME Datatype</a></div> |
| 133 |
<div class="fancy-toc2"><a href="#the_datatype_is_optional">3.3. The datatype is optional</a></div> |
| 134 |
<div class="fancy-toc1"><a href="#foreign_key_enforcement_is_off_by_default">4. Foreign Key Enforcement Is Off By Default</a></div> |
| 135 |
<div class="fancy-toc1"><a href="#primary_keys_can_sometimes_contain_nulls">5. PRIMARY KEYs Can Sometimes Contain NULLs</a></div> |
| 136 |
<div class="fancy-toc1"><a href="#aggregate_queries_can_contain_non_aggregate_result_columns_that_are_not_in_the_group_by_clause">6. Aggregate Queries Can Contain Non-Aggregate Result Columns |
| 137 |
That Are Not In The GROUP BY Clause</a></div> |
| 138 |
<div class="fancy-toc1"><a href="#sqlite_does_not_do_full_unicode_case_folding_by_default">7. SQLite Does Not Do Full Unicode Case Folding By Default</a></div> |
| 139 |
<div class="fancy-toc1"><a href="#double_quoted_string_literals_are_accepted">8. Double-quoted String Literals Are Accepted</a></div> |
| 140 |
<div class="fancy-toc1"><a href="#keywords_can_often_be_used_as_identifiers">9. Keywords Can Often Be Used As Identifiers</a></div> |
| 141 |
<div class="fancy-toc1"><a href="#dubious_sql_is_allowed_without_any_error_or_warning">10. Dubious SQL Is Allowed Without Any Error Or Warning</a></div> |
| 142 |
<div class="fancy-toc1"><a href="#autoincrement_does_not_work_the_same_as_mysql">11. AUTOINCREMENT Does Not Work The Same As MySQL</a></div> |
| 143 |
<div class="fancy-toc1"><a href="#nul_characters_are_allowed_in_text_strings">12. NUL Characters Are Allowed In Text Strings</a></div> |
| 144 |
<div class="fancy-toc1"><a href="#sqlite_distinguishes_between_integer_and_text_literals">13. SQLite Distinguishes Between Integer And Text Literals</a></div> |
| 145 |
<div class="fancy-toc1"><a href="#sqlite_gets_the_precedence_of_comma_joins_wrong">14. SQLite Gets The Precedence Of Comma-Joins Wrong</a></div> |
| 146 |
</div> |
| 147 |
</div> |
| 148 |
<script> |
| 149 |
function toggle_toc(){ |
| 150 |
var sub = document.getElementById("toc_sub") |
| 151 |
var mk = document.getElementById("toc_mk") |
| 152 |
if( sub.style.display!="block" ){ |
| 153 |
sub.style.display = "block"; |
| 154 |
mk.innerHTML = "▼"; |
| 155 |
} else { |
| 156 |
sub.style.display = "none"; |
| 157 |
mk.innerHTML = "►"; |
| 158 |
} |
| 159 |
} |
| 160 |
</script> |
| 161 |
</div> |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
<h1 id="overview"><span>1. </span>Overview</h1> |
| 168 |
|
| 169 |
<p> |
| 170 |
The SQL language is a "standard". |
| 171 |
Even so, no two SQL database engines work exactly alike. |
| 172 |
Every SQL implementation has it own peculiarities and oddities, |
| 173 |
and SQLite is no exception to this rule. |
| 174 |
|
| 175 |
</p><p> |
| 176 |
This document strives to highlight the principal differences |
| 177 |
between SQLite and other SQL implementations, as an aid to developers |
| 178 |
that are porting to or from SQLite or who are trying to build a |
| 179 |
system that works across multiple database engines. |
| 180 |
|
| 181 |
</p><p> |
| 182 |
If you are an SQLite user whose has stumbled over some quirk of |
| 183 |
SQLite that is not mentioned here, please let the developers know |
| 184 |
by posting a brief message on the |
| 185 |
<a href="https://sqlite.org/forum/forum">SQLite Forum</a>. |
| 186 |
|
| 187 |
</p><h1 id="sqlite_is_embedded_not_client_server"><span>2. </span>SQLite Is Embedded, Not Client-Server</h1> |
| 188 |
|
| 189 |
<p> |
| 190 |
Whenever comparing SQLite to other SQL database engines like |
| 191 |
SQL Server, PostgreSQL, MySQL, or Oracle, it is important first of all |
| 192 |
to realize that SQLite is not intended as a replacement or competitor to |
| 193 |
any of those systems. SQLite is <a href="serverless.html">serverless</a>. There is no separate |
| 194 |
server process that manages the database. An application interacts |
| 195 |
with the database engine using function calls, not by sending messages |
| 196 |
to a separate process or thread. |
| 197 |
|
| 198 |
</p><p> |
| 199 |
The fact that SQLite is embedded and <a href="serverless.html">serverless</a> instead of being |
| 200 |
client/server is a feature, not a bug. |
| 201 |
|
| 202 |
</p><p> |
| 203 |
Client/server databases like MySQL, PostgreSQL, SQL Server, Oracle, and |
| 204 |
others are an important component of modern systems. |
| 205 |
These systems solve an important problem. |
| 206 |
But SQLite solves a different problem. |
| 207 |
Both SQLite and client/server databases have their role. |
| 208 |
Developers who are comparing SQLite against other SQL database engines need |
| 209 |
to clearly understand this distinction. |
| 210 |
|
| 211 |
</p><p> |
| 212 |
See the <a href="whentouse.html">Appropriate Uses For SQLite</a> document for additional information. |
| 213 |
|
| 214 |
</p><h1 id="flexible_typing"><span>3. </span>Flexible Typing</h1> |
| 215 |
|
| 216 |
<p> |
| 217 |
SQLite is flexible with regard to datatypes. Datatypes are advisory |
| 218 |
rather than mandatory. |
| 219 |
|
| 220 |
</p><p> |
| 221 |
Some commentators say that SQLite is "weakly typed" and that other |
| 222 |
SQL databases are "strongly typed". We consider these terms to be |
| 223 |
inaccurate and even pejorative. We prefer to say that SQLite is |
| 224 |
"flexibly typed" and that other SQL database engines are |
| 225 |
"rigidly typed". |
| 226 |
|
| 227 |
</p><p> |
| 228 |
See the <a href="datatype3.html">Datatypes in SQLite</a> document for a detailed |
| 229 |
discussion of the type system in SQLite. |
| 230 |
|
| 231 |
</p><p> |
| 232 |
The key point is that SQLite is very forgiving of the type of data that |
| 233 |
you put into the database. For example, if a column has a datatype of |
| 234 |
"INTEGER" and the application inserts a text string into that column, |
| 235 |
SQLite will first try to convert the text string into an integer, just like |
| 236 |
every other SQL database engine. Thus, if one inserts <b>'1234'</b> into |
| 237 |
an INTEGER column, that value is converted into an integer 1234 and stored. |
| 238 |
But, if you insert a non-numeric string like <b>'wxyz'</b> into an INTEGER |
| 239 |
column, unlike other SQL databases, SQLite does not throw an error. Instead, |
| 240 |
SQLite stores the actual string value in the column. |
| 241 |
|
| 242 |
</p><p> |
| 243 |
Similarly, SQLite allows you to store a 2000-character string into a |
| 244 |
column of type VARCHAR(50). Other SQL implementations would either throw |
| 245 |
an error or truncate the string. SQLite stores the entire 2000-character |
| 246 |
string with no loss of information and without complaint. |
| 247 |
|
| 248 |
</p><p> |
| 249 |
Where this ends up causing problems is when developers do some initial |
| 250 |
coding work using SQLite and get their application working, but then try |
| 251 |
to convert to another database like PostgreSQL or SQL Server for deployment. |
| 252 |
If the application is initially taking advantage of SQLite's flexible typing, |
| 253 |
then it will fail when moved to another database that is more judgmental |
| 254 |
about data types. |
| 255 |
|
| 256 |
</p><p> |
| 257 |
<a href="flextypegood.html">Flexible typing is a feature</a> of SQLite, not a bug. Flexible typing |
| 258 |
is about freedom. |
| 259 |
Nevertheless, we recognize that this feature does sometimes cause |
| 260 |
confusion for developers who are accustomed to working with |
| 261 |
other databases that are more strict with regard to data type rules. |
| 262 |
In retrospect, perhaps it would have been less confusing if SQLite had merely |
| 263 |
implemented an ANY datatype so that developers could explicitly state |
| 264 |
when they wanted to use flexible typing, rather than making flexible |
| 265 |
typing the default. |
| 266 |
As an accommodation for those who expect rigid typing, SQLite version 3.37.0 |
| 267 |
(2021-11-27) introduced the option of <a href="stricttables.html">STRICT tables</a>. |
| 268 |
These either impose |
| 269 |
the mandatory datatype constraints found in other SQL database engines, |
| 270 |
or allow the explicit ANY datatype to retain SQLite's flexible typing. |
| 271 |
|
| 272 |
</p><h2 id="no_separate_boolean_datatype"><span>3.1. </span>No Separate BOOLEAN Datatype</h2> |
| 273 |
|
| 274 |
<p> |
| 275 |
Unlike most other SQL implementations, |
| 276 |
SQLite does not have a separate BOOLEAN data type. |
| 277 |
Instead, TRUE and FALSE are (normally) represented as integers 1 and 0, |
| 278 |
respectively. |
| 279 |
This does not seem to cause many problems, as we seldom get complaints |
| 280 |
about it. But it is important to recognize. |
| 281 |
|
| 282 |
</p><p> |
| 283 |
Beginning with SQLite <a href="releaselog/3_23_0.html">version 3.23.0</a> (2018-04-02), SQLite also |
| 284 |
recognizes TRUE and FALSE keywords as aliases for integer values 1 and 0, |
| 285 |
respectively. |
| 286 |
This provides better compatibility with other SQL implementations. |
| 287 |
But for backwards compatibility, if there are columns named TRUE or |
| 288 |
FALSE, then the keywords are treated as identifiers referencing those |
| 289 |
columns, rather than BOOLEAN literals. |
| 290 |
|
| 291 |
</p><h2 id="no_separate_datetime_datatype"><span>3.2. </span>No Separate DATETIME Datatype</h2> |
| 292 |
|
| 293 |
<p> |
| 294 |
SQLite has no DATETIME datatype. |
| 295 |
Instead, dates and times can be stored in any of these ways: |
| 296 |
|
| 297 |
</p><ul> |
| 298 |
<li> As a TEXT string in the ISO-8601 format. Example: '2018-04-02 12:13:46'. |
| 299 |
</li><li> As an INTEGER number of seconds since 1970 (also known as "unix time"). |
| 300 |
</li><li> As a REAL value that is the fractional |
| 301 |
<a href="https://en.wikipedia.org/wiki/Julian_day">Julian day number</a>. |
| 302 |
</li></ul> |
| 303 |
|
| 304 |
<p> |
| 305 |
The built-in <a href="lang_datefunc.html">date and time functions</a> of SQLite understand date/times in |
| 306 |
all of the formats above, and can freely change between them. |
| 307 |
Which format you use, is entirely up to your application. |
| 308 |
|
| 309 |
</p><h2 id="the_datatype_is_optional"><span>3.3. </span>The datatype is optional</h2> |
| 310 |
|
| 311 |
<p> |
| 312 |
Because SQLite is flexible and forgiving with regard to datatypes, |
| 313 |
table columns can be created that have no specified datatype at all. |
| 314 |
For example: |
| 315 |
</p><div class="codeblock"><pre>CREATE TABLE t1(a,b,c,d); |
| 316 |
</pre></div> |
| 317 |
<p>The table "t1" has four columns "a", "b", "c", and "d" that have |
| 318 |
no particular datatype assigned. You can store anything you want in |
| 319 |
any of those columns.</p> |
| 320 |
|
| 321 |
<h1 id="foreign_key_enforcement_is_off_by_default"><span>4. </span>Foreign Key Enforcement Is Off By Default</h1> |
| 322 |
|
| 323 |
<p>SQLite has parsed foreign key constraints for time out of mind, |
| 324 |
but added the ability to actually enforce those constraints much later, |
| 325 |
with <a href="releaselog/3_6_19.html">version 3.6.19</a> (2009-10-14). By the time foreign key constraint |
| 326 |
enforcement was added, there were already countless millions of databases |
| 327 |
in circulation that contained foreign key constraints, some of which |
| 328 |
were not correct. To avoid breaking those legacy databases, foreign key |
| 329 |
constraint enforcement is turned off by default in SQLite. |
| 330 |
|
| 331 |
</p><p>Applications can activate foreign key enforcement at run-time using |
| 332 |
the <a href="pragma.html#pragma_foreign_keys">PRAGMA foreign_keys</a> statement. Or, foreign key enforcement can |
| 333 |
be activated at compile-time using the |
| 334 |
<a href="compile.html#default_foreign_keys">-DSQLITE_DEFAULT_FOREIGN_KEYS=1</a> compile-time option. |
| 335 |
|
| 336 |
</p><h1 id="primary_keys_can_sometimes_contain_nulls"><span>5. </span>PRIMARY KEYs Can Sometimes Contain NULLs</h1> |
| 337 |
|
| 338 |
<p> |
| 339 |
A PRIMARY KEY in an SQLite table is usually just |
| 340 |
a UNIQUE constraint. Due to an historical oversight, |
| 341 |
the column values of a PRIMARY KEY are allowed to be NULL. |
| 342 |
This is a bug, but by the time the problem was discovered there |
| 343 |
where so many databases in circulation that depended on the bug that |
| 344 |
the decision was made to support the buggy behavior moving forward. |
| 345 |
You can work around this problem by adding a NOT NULL constraint on |
| 346 |
each column of the PRIMARY KEY. |
| 347 |
</p><p> |
| 348 |
Exceptions: |
| 349 |
</p><ul> |
| 350 |
<li><p> |
| 351 |
The value of an <a href="lang_createtable.html#rowid">INTEGER PRIMARY KEY</a> column must always be a |
| 352 |
non-NULL integer because the INTEGER PRIMARY KEY is an alias for |
| 353 |
the <a href="lang_createtable.html#rowid">ROWID</a>. If you try to insert a NULL into an INTEGER PRIMARY |
| 354 |
KEY column, SQLite automatically convert the NULL into a unique integer. |
| 355 |
|
| 356 |
</p></li><li><p> |
| 357 |
The <a href="withoutrowid.html">WITHOUT ROWID</a> and <a href="stricttables.html">STRICT</a> features was added after this bug was |
| 358 |
discovered, and so WITHOUT ROWID and STRICT tables work correctly: |
| 359 |
They disallow NULLs in the PRIMARY KEY. |
| 360 |
</p></li></ul> |
| 361 |
|
| 362 |
<h1 id="aggregate_queries_can_contain_non_aggregate_result_columns_that_are_not_in_the_group_by_clause"><span>6. </span>Aggregate Queries Can Contain Non-Aggregate Result Columns |
| 363 |
That Are Not In The GROUP BY Clause</h1> |
| 364 |
|
| 365 |
<p> |
| 366 |
In most SQL implementations, output columns of an aggregate query |
| 367 |
may only reference aggregate functions or columns named in the |
| 368 |
GROUP BY clause. It does not make good sense to reference an ordinary |
| 369 |
column in an aggregate query because each output row might be composed |
| 370 |
from two or more rows in the input table(s). |
| 371 |
|
| 372 |
</p><p> |
| 373 |
SQLite does not enforce this restriction. |
| 374 |
The output columns from an aggregate query can be arbitrary expressions |
| 375 |
that include columns not found in GROUP BY clause. |
| 376 |
This feature has two uses: |
| 377 |
|
| 378 |
</p><ol> |
| 379 |
<li><p> |
| 380 |
With SQLite (but not any other SQL implementation that we know of) if |
| 381 |
an aggregate query contains a single min() or max() function, then the |
| 382 |
values of columns used in the output are taken from the row where |
| 383 |
the min() or max() value was achieved. If two or more rows have the |
| 384 |
same min() or max() value, then the columns values will be chosen arbitrarily |
| 385 |
from one of those rows. |
| 386 |
</p><p> |
| 387 |
For example to find the highest paid employee: |
| 388 |
</p><div class="codeblock"><pre>SELECT max(salary), first_name, last_name FROM employee; |
| 389 |
</pre></div> |
| 390 |
<p> |
| 391 |
In the query above, the values for the first_name and last_name columns |
| 392 |
will correspond to the row that satisfied the max(salary) condition. |
| 393 |
|
| 394 |
</p></li><li><p> |
| 395 |
If a query contains no aggregate functions at all, then a GROUP BY |
| 396 |
clause can be added as a substitute for the DISTINCT ON clause. In other words, |
| 397 |
output rows are filtered so that only one row is shown for each distinct |
| 398 |
set of values in the GROUP BY clause. If two or more output rows would |
| 399 |
have otherwise had the same set of values for the GROUP BY columns, then |
| 400 |
one of the rows is chosen arbitrarily. (SQLite supports DISTINCT but not |
| 401 |
DISTINCT ON, whose functionality is provided instead by GROUP BY.) |
| 402 |
</p></li></ol> |
| 403 |
|
| 404 |
<h1 id="sqlite_does_not_do_full_unicode_case_folding_by_default"><span>7. </span>SQLite Does Not Do Full Unicode Case Folding By Default</h1> |
| 405 |
|
| 406 |
<p> |
| 407 |
SQLite does not know about the upper-case/lower-case distinction |
| 408 |
for all unicode characters. SQL functions like |
| 409 |
upper() and lower() only work on ASCII characters. There are two |
| 410 |
reasons for this: |
| 411 |
</p><ol> |
| 412 |
<li><p> Though stable now, when SQLite was first designed, the rules for |
| 413 |
unicode case folding were still in flux. That means that the |
| 414 |
behavior might have changed with each new unicode release, disrupting |
| 415 |
applications and corrupting indexes in the process. |
| 416 |
</p></li><li><p> The tables necessary to do full and proper unicode case folding are |
| 417 |
larger than the whole SQLite library. |
| 418 |
</p></li></ol> |
| 419 |
<p> |
| 420 |
Full unicode case folding is supported in SQLite if it is compiled |
| 421 |
with the <a href="compile.html#enable_icu">-DSQLITE_ENABLE_ICU</a> option and linked against the |
| 422 |
<a href="https://icu.unicode.org">International Components for Unicode</a> |
| 423 |
library. |
| 424 |
|
| 425 |
<a name="dblquote"></a> |
| 426 |
|
| 427 |
</p><h1 id="double_quoted_string_literals_are_accepted"><span>8. </span>Double-quoted String Literals Are Accepted</h1> |
| 428 |
|
| 429 |
<p> |
| 430 |
The SQL standard requires double-quotes around identifiers |
| 431 |
and single-quotes around string literals. For example: |
| 432 |
</p><ul> |
| 433 |
<li> <tt>"this is a legal SQL column name"</tt> |
| 434 |
</li><li> <tt>'this is an SQL string literal'</tt> |
| 435 |
</li></ul> |
| 436 |
<p> |
| 437 |
SQLite accepts both of the above. But, in an effort to be compatible |
| 438 |
with MySQL 3.x (which was one of the most widely used RDBMSes |
| 439 |
when SQLite was first being designed) SQLite will also interpret |
| 440 |
a double-quotes string as |
| 441 |
string literal if it does not match any valid identifier. |
| 442 |
</p><p> |
| 443 |
This misfeature means that a misspelled double-quoted |
| 444 |
identifier will be interpreted as a string literal, rather than generating |
| 445 |
an error. |
| 446 |
It also lures developers who are new to the SQL language into the |
| 447 |
bad habit of using double-quoted string literals when they |
| 448 |
really need to learn to use the correct single-quoted string literal form. |
| 449 |
</p><p> |
| 450 |
In hindsight, we should not have tried to make SQLite accept MySQL 3.x |
| 451 |
syntax, and should have never allowed double-quoted string literals. |
| 452 |
However, there are countless applications that make use of |
| 453 |
double-quoted string literals and so we continue to support |
| 454 |
that capability to avoid breaking legacy. |
| 455 |
</p><p> |
| 456 |
As of SQLite 3.27.0 (2019-02-07) the use of a double-quoted |
| 457 |
string literal causes a warning message to be sent to the <a href="errlog.html">error log</a>. |
| 458 |
</p><p> |
| 459 |
As of SQLite 3.29.0 (2019-07-10) the use of double-quoted |
| 460 |
string literals can be disabled at run-time using the |
| 461 |
<a href="c3ref/c_dbconfig_defensive.html#sqlitedbconfigdqsddl">SQLITE_DBCONFIG_DQS_DDL</a> and <a href="c3ref/c_dbconfig_defensive.html#sqlitedbconfigdqsdml">SQLITE_DBCONFIG_DQS_DML</a> actions |
| 462 |
to <a href="c3ref/db_config.html">sqlite3_db_config()</a>. The default settings can be altered |
| 463 |
at compile-time using the <a href="compile.html#dqs">-DSQLITE_DQS=<i>N</i></a> compile-time |
| 464 |
option. Application developers are encouraged to compile using |
| 465 |
-DSQLITE_DQS=0 in order to disable the double-quoted string literal |
| 466 |
misfeature by default. If that is not possible, then disable |
| 467 |
double-quoted string literals for individual database connections |
| 468 |
using C-code like this: |
| 469 |
</p><blockquote><pre> |
| 470 |
sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DDL, 0, (void*)0); |
| 471 |
sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DML, 0, (void*)0); |
| 472 |
</pre></blockquote> |
| 473 |
<p>Or, if double-quoted string literals are disabled by default, but need |
| 474 |
to be selectively enabled for some historical database connections, |
| 475 |
that can be done using the same C-code as shown above except with the |
| 476 |
third parameter changed from 0 to 1. |
| 477 |
</p><p> |
| 478 |
As of SQLite 3.41.0 (2023-02-21) SQLITE_DBCONFIG_DQS_DDL and |
| 479 |
SQLITE_DBCONFIG_DQS_DML are disabled by default in the <a href="cli.html">CLI</a>. Use |
| 480 |
the ".dbconfig" dot-command to reenable the legacy behavior if |
| 481 |
desired. |
| 482 |
|
| 483 |
</p><h1 id="keywords_can_often_be_used_as_identifiers"><span>9. </span>Keywords Can Often Be Used As Identifiers</h1> |
| 484 |
|
| 485 |
<p> |
| 486 |
The SQL language is rich in keywords. |
| 487 |
Most SQL implementations do not allow keywords to be used as identifiers |
| 488 |
(names of tables or columns) unless they are enclosed in double-quotes. |
| 489 |
But SQLite is more flexible. Many keywords can be used as identifiers without |
| 490 |
needing to be quoted, as long as those keywords are used in a context where |
| 491 |
it is clear that they are intended to be an identifier. |
| 492 |
</p><p> |
| 493 |
For example, the following statement is valid in SQLite: |
| 494 |
</p><div class="codeblock"><pre>CREATE TABLE union(true INT, with BOOLEAN); |
| 495 |
</pre></div> |
| 496 |
<p> |
| 497 |
The same SQL statement will fail on every other SQL implementation that |
| 498 |
we know of due to the use of keywords "union", "true", and "with" as |
| 499 |
identifiers. |
| 500 |
</p><p> |
| 501 |
The ability to use keywords as identifiers promotes backwards compatibility. |
| 502 |
As new keywords are added, legacy schemas that just happen to use those |
| 503 |
keywords as table or column names continue to work. However, the ability |
| 504 |
to use a keyword as an identifier sometimes leads to surprising outcomes. |
| 505 |
For example: |
| 506 |
</p><div class="codeblock"><pre>CREATE TRIGGER AFTER INSERT ON tableX BEGIN |
| 507 |
INSERT INTO tableY(b) VALUES(new.a); |
| 508 |
END; |
| 509 |
</pre></div> |
| 510 |
<p>The trigger created by the previous statement is named "AFTER" |
| 511 |
and it is a "BEFORE" trigger. The "AFTER" token is used as an identifier |
| 512 |
instead of as a keyword, as that is the only way to parse the statement. |
| 513 |
Another example: |
| 514 |
</p><div class="codeblock"><pre>CREATE TABLE tableZ(INTEGER PRIMARY KEY); |
| 515 |
</pre></div> |
| 516 |
<p>The tableZ table has a single column named "INTEGER". That column |
| 517 |
has no datatype specified, but it is the PRIMARY KEY. |
| 518 |
The column is <em>not</em> the <a href="lang_createtable.html#rowid">INTEGER PRIMARY KEY</a> for the table because |
| 519 |
it has no datatype. The "INTEGER" token is used as an identifier for the |
| 520 |
column name, not as a datatype keyword.</p> |
| 521 |
|
| 522 |
<h1 id="dubious_sql_is_allowed_without_any_error_or_warning"><span>10. </span>Dubious SQL Is Allowed Without Any Error Or Warning</h1> |
| 523 |
|
| 524 |
<p> |
| 525 |
The original implementation of SQLite sought to follow |
| 526 |
<a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's Law</a> which |
| 527 |
states in part "Be liberal in what you accept". |
| 528 |
This used to be considered good design - that a system would accept |
| 529 |
dodgy inputs and try to do the best it could without complaining too much. |
| 530 |
But lately, people have come to realize that it is sometimes better to |
| 531 |
be strict in what you accept, so as to more easily find errors in the |
| 532 |
input. |
| 533 |
</p><p> |
| 534 |
|
| 535 |
</p><h1 id="autoincrement_does_not_work_the_same_as_mysql"><span>11. </span>AUTOINCREMENT Does Not Work The Same As MySQL</h1> |
| 536 |
|
| 537 |
<p>The <a href="autoinc.html">AUTOINCREMENT</a> feature in SQLite works differently than |
| 538 |
it does in MySQL. This often causes confusion for people who |
| 539 |
initially learned SQL on MySQL and then start using SQLite, and |
| 540 |
expect the two systems to work identically. |
| 541 |
|
| 542 |
</p><p>See the <a href="autoinc.html">SQLite AUTOINCREMENT documentation</a> for |
| 543 |
detailed instructions on what AUTOINCREMENT does and does not do |
| 544 |
in SQLite. |
| 545 |
|
| 546 |
</p><h1 id="nul_characters_are_allowed_in_text_strings"><span>12. </span>NUL Characters Are Allowed In Text Strings</h1> |
| 547 |
|
| 548 |
<p>NUL characters (ASCII code 0x00 and Unicode \u0000) may appear in |
| 549 |
the middle of strings in SQLite. This can lead to unexpected behavior. |
| 550 |
See the "<a href="nulinstr.html">NUL characters in strings</a>" document for further information. |
| 551 |
|
| 552 |
</p><h1 id="sqlite_distinguishes_between_integer_and_text_literals"><span>13. </span>SQLite Distinguishes Between Integer And Text Literals</h1> |
| 553 |
|
| 554 |
<p>SQLite says that the following query returns false: |
| 555 |
</p><div class="codeblock"><pre>SELECT 1='1'; |
| 556 |
</pre></div> |
| 557 |
<p>It does this because an integer is not a string. |
| 558 |
Every other major SQL database engine says this is true, for reasons |
| 559 |
that the creator of SQLite does not understand. |
| 560 |
|
| 561 |
</p><h1 id="sqlite_gets_the_precedence_of_comma_joins_wrong"><span>14. </span>SQLite Gets The Precedence Of Comma-Joins Wrong</h1> |
| 562 |
|
| 563 |
<p>SQLite gives all join operators equal precedence and processes them |
| 564 |
from left to right. But this is not quite correct. It should be that |
| 565 |
comma-joins have lower precedence than all others join operators. |
| 566 |
In other words, a FROM clause like this: |
| 567 |
|
| 568 |
</p><blockquote><p> |
| 569 |
... FROM a, b RIGHT JOIN c, d ... |
| 570 |
</p></blockquote> |
| 571 |
|
| 572 |
<p>Should be parsed as follows: |
| 573 |
|
| 574 |
</p><div class="imgcontainer"> |
| 575 |
<div style="max-width:153px;"><svg xmlns='http://www.w3.org/2000/svg' class="pikchr" viewBox="0 0 153.328 245.544"> |
| 576 |
<path d="M67,32L120,32L120,2L67,2Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 577 |
<text x="93" y="17" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">JOIN</text> |
| 578 |
<polygon points="53,72 58,61 65,67" style="fill:rgb(0,0,0)"/> |
| 579 |
<path d="M93,32L57,68" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 580 |
<polygon points="134,72 122,67 128,61" style="fill:rgb(0,0,0)"/> |
| 581 |
<path d="M93,32L129,68" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 582 |
<path d="M27,102L80,102L80,72L27,72Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 583 |
<text x="53" y="87" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">JOIN</text> |
| 584 |
<path d="M121,102L146,102L146,72L121,72Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 585 |
<text x="134" y="87" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">D</text> |
| 586 |
<polygon points="13,142 18,131 24,137" style="fill:rgb(0,0,0)"/> |
| 587 |
<path d="M53,102L17,138" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 588 |
<polygon points="93,142 82,137 88,131" style="fill:rgb(0,0,0)"/> |
| 589 |
<path d="M53,102L89,138" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 590 |
<path d="M36,173L151,173L151,142L36,142Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 591 |
<text x="93" y="157" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">RIGHTÂ JOIN</text> |
| 592 |
<path d="M2,173L25,173L25,142L2,142Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 593 |
<text x="13" y="157" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">A</text> |
| 594 |
<polygon points="53,213 58,201 65,208" style="fill:rgb(0,0,0)"/> |
| 595 |
<path d="M93,173L57,209" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 596 |
<polygon points="134,213 122,208 128,201" style="fill:rgb(0,0,0)"/> |
| 597 |
<path d="M93,173L129,209" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 598 |
<path d="M42,243L65,243L65,213L42,213Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 599 |
<text x="53" y="228" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">B</text> |
| 600 |
<path d="M121,243L146,243L146,213L121,213Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 601 |
<text x="134" y="228" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">C</text> |
| 602 |
</svg> |
| 603 |
</div> |
| 604 |
</div> |
| 605 |
|
| 606 |
<p>But SQLite instead parses the FROM clause like this: |
| 607 |
|
| 608 |
</p><div class="imgcontainer"> |
| 609 |
<div style="max-width:188px;"><svg xmlns='http://www.w3.org/2000/svg' class="pikchr" viewBox="0 0 188.691 245.544"> |
| 610 |
<path d="M107,32L160,32L160,2L107,2Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 611 |
<text x="134" y="17" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">JOIN</text> |
| 612 |
<polygon points="93,72 99,61 105,67" style="fill:rgb(0,0,0)"/> |
| 613 |
<path d="M134,32L98,68" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 614 |
<polygon points="174,72 162,67 169,61" style="fill:rgb(0,0,0)"/> |
| 615 |
<path d="M134,32L170,68" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 616 |
<path d="M36,102L151,102L151,72L36,72Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 617 |
<text x="93" y="87" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">RIGHTÂ JOIN</text> |
| 618 |
<path d="M161,102L186,102L186,72L161,72Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 619 |
<text x="174" y="87" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">D</text> |
| 620 |
<polygon points="53,142 58,131 65,137" style="fill:rgb(0,0,0)"/> |
| 621 |
<path d="M93,102L57,138" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 622 |
<polygon points="134,142 122,137 128,131" style="fill:rgb(0,0,0)"/> |
| 623 |
<path d="M93,102L129,138" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 624 |
<path d="M27,173L80,173L80,142L27,142Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 625 |
<text x="53" y="157" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">JOIN</text> |
| 626 |
<path d="M121,173L146,173L146,142L121,142Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 627 |
<text x="134" y="157" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">C</text> |
| 628 |
<polygon points="13,213 18,201 24,208" style="fill:rgb(0,0,0)"/> |
| 629 |
<path d="M53,173L17,209" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 630 |
<polygon points="93,213 82,208 88,201" style="fill:rgb(0,0,0)"/> |
| 631 |
<path d="M53,173L89,209" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 632 |
<path d="M2,243L25,243L25,213L2,213Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 633 |
<text x="13" y="228" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">A</text> |
| 634 |
<path d="M82,243L105,243L105,213L82,213Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 635 |
<text x="93" y="228" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">B</text> |
| 636 |
</svg> |
| 637 |
</div> |
| 638 |
</div> |
| 639 |
|
| 640 |
<p>The problem can only makes a difference in the result when using |
| 641 |
RIGHT OUTER JOIN or FULL OUTER JOIN in the same FROM clause with |
| 642 |
comma-joins, which rarely happens in practice. And |
| 643 |
the problem can be easily overcome using parentheses in the FROM clause: |
| 644 |
|
| 645 |
</p><blockquote><p> |
| 646 |
... FROM a, (b RIGHT JOIN c), d ... |
| 647 |
</p></blockquote> |
| 648 |
<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/quirks.in?m=4b8c32b617">2024-08-10 18:17:30</a> UTC </small></i></p> |
| 649 |
|