Debian Codegraph @

www/gencol.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>Generated Columns</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 Generated Columns
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="#introduction">1. Introduction</a></div>
129 <div class="fancy-toc1"><a href="#syntax">2. Syntax</a></div>
130 <div class="fancy-toc2"><a href="#virtual_versus_stored_columns">2.1. VIRTUAL versus STORED columns</a></div>
131 <div class="fancy-toc2"><a href="#capabilities">2.2. Capabilities</a></div>
132 <div class="fancy-toc2"><a href="#limitations">2.3. Limitations</a></div>
133 <div class="fancy-toc1"><a href="#compatibility">3. Compatibility</a></div>
134 </div>
135 </div>
136 <script>
137 function toggle_toc(){
138 var sub = document.getElementById("toc_sub")
139 var mk = document.getElementById("toc_mk")
140 if( sub.style.display!="block" ){
141 sub.style.display = "block";
142 mk.innerHTML = "&#x25bc;";
143 } else {
144 sub.style.display = "none";
145 mk.innerHTML = "&#x25ba;";
146 }
147 }
148 </script>
149 </div>
150
151
152
153
154 <h1 id="introduction"><span>1. </span>Introduction</h1>
155
156 <p>Generated columns (also sometimes called "computed columns")
157 are columns of a table whose values are a function of other columns
158 in the same row.
159 Generated columns can be read, but their values can not be directly
160 written. The only way to change the value of a generated column is to
161 modify the values of the other columns used to calculate
162 the generated column.
163
164 </p><h1 id="syntax"><span>2. </span>Syntax</h1>
165
166 <p>Syntactically, generated columns are designated using a
167 "GENERATED ALWAYS" <a href="syntax/column-constraint.html">column-constraint</a>. For example:
168
169 </p><div class="codeblock"><pre>CREATE TABLE t1(
170 a INTEGER PRIMARY KEY,
171 b INT,
172 c TEXT,
173 d INT GENERATED ALWAYS AS (a*abs(b)) VIRTUAL,
174 e TEXT GENERATED ALWAYS AS (substr(c,b,b+1)) STORED
175 );
176 </pre></div>
177
178 <p>The statement above has three ordinary columns, "a" (the PRIMARY KEY),
179 "b", and "c", and two generated columns "d" and "e".
180
181 </p><p>The "GENERATED ALWAYS" keywords at the beginning of the constraint
182 and the "VIRTUAL" or "STORED" keyword at the end are all optional.
183 Only the "AS" keyword and the parenthesized expression are required.
184 If the trailing "VIRTUAL" or "STORED" keyword is omitted, then
185 VIRTUAL is the default. Hence, the example statement above could
186 be simplified to just:
187
188 </p><div class="codeblock"><pre>CREATE TABLE t1(
189 a INTEGER PRIMARY KEY,
190 b INT,
191 c TEXT,
192 d INT AS (a*abs(b)),
193 e TEXT AS (substr(c,b,b+1)) STORED
194 );
195 </pre></div>
196
197 <h2 id="virtual_versus_stored_columns"><span>2.1. </span>VIRTUAL versus STORED columns</h2>
198
199 <p>Generated columns can be either VIRTUAL or STORED. The value of
200 a VIRTUAL column is computed when read, whereas the value of a STORED
201 column is computed when the row is written. STORED columns take up space
202 in the database file, whereas VIRTUAL columns use more CPU cycles when
203 being read.
204
205 </p><p>From the point of view of SQL, STORED and VIRTUAL columns are almost
206 exactly the same. Queries against either class of generated column
207 produce the same results. The only functional difference is that
208 one cannot add new STORED columns using the
209 <a href="lang_altertable.html#altertabaddcol">ALTER TABLE ADD COLUMN</a> command. Only VIRTUAL columns can be added
210 using ALTER TABLE.
211
212 </p><h2 id="capabilities"><span>2.2. </span>Capabilities</h2>
213
214 <ol>
215 <li><p>
216 Generated columns can have a datatype. SQLite attempts to transform
217 the result of the generating expression into that datatype using the
218 same <a href="datatype3.html#affinity">affinity</a> rules as for ordinary columns.
219
220 </p></li><li><p>
221 Generated columns may have NOT NULL, CHECK, and UNIQUE constraints,
222 and foreign key constraints, just like ordinary columns.
223
224 </p></li><li><p>
225 Generated columns can participate in indexes, just like ordinary
226 columns.
227
228 </p></li><li><p>
229 The expression of a generated column can refer to any of the
230 other declared columns in the table, including other generated columns,
231 as long as the expression does not directly or indirectly refer back
232 to itself.
233
234 </p></li><li><p>
235 Generated columns can occur anywhere in the table definition. Generated
236 columns can be interspersed among ordinary columns. It is not necessary
237 to put generated columns at the end of the list of columns in the
238 table definition, as is shown in the examples above.
239 </p></li></ol>
240
241
242 <h2 id="limitations"><span>2.3. </span>Limitations</h2>
243
244 <ol>
245 <li><p>
246 Generated columns may not have a <a href="lang_createtable.html#dfltval">default value</a> (they may not use the
247 "DEFAULT" clause). The value of a generated column is always the value
248 specified by the expression that follows the "AS" keyword.
249
250 </p></li><li><p>
251 Generated columns may not be used as part of the <a href="lang_createtable.html#primkeyconst">PRIMARY KEY</a>.
252 (Future versions of SQLite might relax this constraint for STORED columns.)
253
254 </p></li><li><p>
255 The expression of a generated column may only reference
256 constant literals and columns within the same row, and may only use
257 scalar <a href="deterministic.html">deterministic functions</a>. The expression may not use subqueries,
258 aggregate functions, window functions, or table-valued functions.
259
260 </p></li><li><p>
261 The expression of a generated column may refer to other generated columns
262 in the same row, but no generated column can depend upon itself, either
263 directly or indirectly.
264
265 </p></li><li><p>The expression of a generated column may not directly reference
266 the <a href="lang_createtable.html#rowid">ROWID</a>, though it can reference the <a href="lang_createtable.html#rowid">INTEGER PRIMARY KEY</a> column,
267 which is often the same thing.
268
269 </p></li><li><p>
270 Every table must have at least one non-generated column.
271
272 </p></li><li><p>
273 It is not possible to <a href="lang_altertable.html#altertabaddcol">ALTER TABLE ADD COLUMN</a> a STORED column.
274 One can add a VIRTUAL column, however.
275
276 </p></li><li><p>
277 The datatype and <a href="datatype3.html#collation">collating sequence</a> of the generated column are determined
278 only by the datatype and <a href="lang_createtable.html#collateclause">COLLATE clause</a> on the column definition.
279 The datatype and collating sequence of the GENERATED ALWAYS AS expression
280 have no affect on the datatype and collating sequence of the column itself.
281
282 </p></li><li><p>
283 Generated columns are not included in the list of columns provided by
284 the <a href="pragma.html#pragma_table_info">PRAGMA table_info</a> statement. But they are included in the output of
285 the newer <a href="pragma.html#pragma_table_xinfo">PRAGMA table_xinfo</a> statement.
286 </p></li></ol>
287
288 <h1 id="compatibility"><span>3. </span>Compatibility</h1>
289
290 <p>Generated column support was added with SQLite version 3.31.0
291 (2020-01-22). If an earlier version of SQLite attempts to read
292 a database file that contains a generated column in its schema, then
293 that earlier version will perceive the generated column syntax as an
294 error and will report that the database schema is corrupt.
295
296 </p><p>To clarify: SQLite version 3.31.0 can read and write any database
297 created by any prior version of SQLite going back to
298 SQLite 3.0.0 (2004-06-18). And, earlier versions of SQLite,
299 prior to 3.31.0, can read and write databases created by SQLite
300 version 3.31.0 and later as long
301 as the database schema does not contain features, such as
302 generated columns, that are not understood by the earlier version.
303 Problems only arise if you create a new database that contains
304 generated columns, using SQLite version 3.31.0 or later, and then
305 try to read or write that database file using an earlier version of
306 SQLite that does not understand generated columns.
307 </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/gencol.in?m=1b689d97b3">2022-11-09 20:11:26</a> UTC </small></i></p>
308