You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by bp...@apache.org on 2016/08/14 00:51:32 UTC
svn commit: r1756297 - in /db/derby/docs/trunk/src/ref: rrefsqlj24513.dita
rrefsqlj37836.dita rrefsqljcreatesequence.dita
Author: bpendleton
Date: Sun Aug 14 00:51:31 2016
New Revision: 1756297
URL: http://svn.apache.org/viewvc?rev=1756297&view=rev
Log:
DERBY-6852: Allow identity columns to cycle
This patch was contributed by Danoja Dias (danojadias at gmail dot com)
This change adds documentation about the new CYCLE option for a
generated identity column. The pages that are modified are the reference
manual pages for CREATE TABLE, for the GENERATED column spec, and for
the CREATE SEQUENCE statement.
Modified:
db/derby/docs/trunk/src/ref/rrefsqlj24513.dita
db/derby/docs/trunk/src/ref/rrefsqlj37836.dita
db/derby/docs/trunk/src/ref/rrefsqljcreatesequence.dita
Modified: db/derby/docs/trunk/src/ref/rrefsqlj24513.dita
URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/ref/rrefsqlj24513.dita?rev=1756297&r1=1756296&r2=1756297&view=diff
==============================================================================
--- db/derby/docs/trunk/src/ref/rrefsqlj24513.dita (original)
+++ db/derby/docs/trunk/src/ref/rrefsqlj24513.dita Sun Aug 14 00:51:31 2016
@@ -79,10 +79,10 @@ CREATE TABLE PEOPLE
CONSTRAINT PEOPLE_PK PRIMARY KEY, PERSON VARCHAR(26));
<ph>-- assign an identity column attribute to a SMALLINT
-- column with an initial value of 5 and an increment value
--- of 5.</ph>
+-- of 5 and with cycle option.</ph>
CREATE TABLE GROUPS
(GROUP_ID SMALLINT NOT NULL GENERATED ALWAYS AS IDENTITY
- (START WITH 5, INCREMENT BY 5), ADDRESS VARCHAR(100), PHONE VARCHAR(15));</b></codeblock><note>For
+ (START WITH 5, INCREMENT BY 5, CYCLE), ADDRESS VARCHAR(100), PHONE VARCHAR(15));</b></codeblock><note>For
more examples of CREATE TABLE statements using the various constraints, see <xref
href="rrefsqlj13590.dita#rrefsqlj13590"></xref>.</note> </example>
<section><title>CREATE TABLE ... AS ... </title>
Modified: db/derby/docs/trunk/src/ref/rrefsqlj37836.dita
URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/ref/rrefsqlj37836.dita?rev=1756297&r1=1756296&r2=1756297&view=diff
==============================================================================
--- db/derby/docs/trunk/src/ref/rrefsqlj37836.dita (original)
+++ db/derby/docs/trunk/src/ref/rrefsqlj37836.dita Sun Aug 14 00:51:31 2016
@@ -26,7 +26,8 @@ limitations under the License.
<refbody>
<example> <codeblock><b>[ GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY
[ ( START WITH <i>integerConstant</i>
-[ , INCREMENT BY <i>integerConstant</i> ] ) ] ] ]
+ | INCREMENT BY <i>integerConstant</i>
+ | CYCLE ) ] ]
</b></codeblock></example>
<section id="sqlj69411"><title>Identity column attributes</title>
<p>A table can have at most one identity column.</p><p>For SMALLINT,
@@ -82,9 +83,9 @@ WITH value which corresponds to the firs
assign. To check for this condition and disallow it, you can use a primary
key or unique constraint on the GENERATED BY DEFAULT identity column.</p></dd>
</dlentry></dl></p><p>By default, the initial value of an identity column
-is 1, and the amount of the increment is 1. You can specify non-default values
-for both the initial value and the interval amount when you define the column
-with the key words START WITH and INCREMENT BY. And if you specify a negative
+is 1, the amount of the increment is 1, and does not cycle. You can specify non-default values
+for the initial value, the interval amount and the cycle option when you define the column
+with the key words START WITH, INCREMENT BY and CYCLE. And if you specify a negative
number for the increment value, <ph conref="../conrefs.dita#prod/productshortname"></ph> <i>decrements</i> the
value with each insert. If this value is positive, <ph conref="../conrefs.dita#prod/productshortname"></ph> increments
the value with each insert. A value of 0 raises a statement exception. </p><p>The maximum and minimum values allowed in
@@ -142,7 +143,14 @@ table</xref>. Rolling back a transaction
transactions can leave "gaps" in the values automatically inserted into an
identity column. <ph conref="../conrefs.dita#prod/productshortname"></ph> behaves
this way to avoid locking a row in SYS.SYSCOLUMNS for the duration
-of a transaction and keeping concurrency high. </p><p><indexterm>Identity
+of a transaction and keeping concurrency high.</p><p>
+The CYCLE clause controls what happens when the identity column exhausts
+its range and wraps around. If CYCLE is specified, the wraparound behavior is to
+reinitialize the the value of identity column to its minimum or maximum value. If CYCLE is not
+specified <ph conref="../conrefs.dita#prod/productshortname"></ph> throws an
+exception when the generator wraps around. In default behavior identity column
+does not cycle. Note that cycling restarts from the minimum or maximum value,
+not from the start value.</p><p><indexterm>Identity
columns <indexterm>and triggers</indexterm></indexterm>When an insert happens
within a <i>triggeredSQLStatement</i>, the value inserted by the
<i>triggeredSQLStatement</i>
@@ -162,7 +170,7 @@ all the way to the top-level SQL stateme
You can only have 16 levels of trigger recursion.</p> </section>
-<section><title>Example</title><codeblock>create table greetings
+<section><title>Example 1</title><codeblock>create table greetings
(i int generated by default as identity (START WITH 2, INCREMENT BY 1),
ch char(50));
-- specify value "1":
@@ -171,5 +179,12 @@ insert into greetings values (1, 'hi');
insert into greetings values (DEFAULT, 'salut');
-- use generated default
insert into greetings(ch) values ('bonjour'); </codeblock></section>
+
+<section><title>Example 2 with cycle option</title><codeblock>create table greetings
+ (i int generated by default as identity (START WITH 2147483647, INCREMENT BY 1, CYCLE),
+ j int);
+-- when values are inserted, the identity column value restarts with minimum value:
+insert into t( b ) values ( 1 ), ( 2 ), ( 3 );
+</codeblock></section>
</refbody>
</reference>
Modified: db/derby/docs/trunk/src/ref/rrefsqljcreatesequence.dita
URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/ref/rrefsqljcreatesequence.dita?rev=1756297&r1=1756296&r2=1756297&view=diff
==============================================================================
--- db/derby/docs/trunk/src/ref/rrefsqljcreatesequence.dita (original)
+++ db/derby/docs/trunk/src/ref/rrefsqljcreatesequence.dita Sun Aug 14 00:51:31 2016
@@ -81,9 +81,11 @@ not specified, the initial value default
<li>MAXVALUE if INCREMENT is negative</li></ul>
<p>The CYCLE clause controls what happens when the sequence generator exhausts
its range and wraps around. If CYCLE is specified, the wraparound behavior is to
-reinitialize the sequence generator to its START value. If NO CYCLE is
+reinitialize the sequence generator to its MINIMUM or MAXIMUM value. If NO CYCLE is
specified, <ph conref="../conrefs.dita#prod/productshortname"></ph> throws an
-exception when the generator wraps around. The default behavior is NO CYCLE.</p>
+exception when the generator wraps around. The default behavior is NO CYCLE.
+Note that cycling restarts from the minimum or maximum value,
+not from the start value.</p>
<p>To retrieve the next value from a sequence generator, use a
<xref href="rrefsqljnextvaluefor.dita#rrefsqljnextvaluefor">NEXT VALUE FOR
expression</xref>.