You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|First value produced by `NEXT VALUE FOR __seq_name__`.
50
+
A 64-bit integer from -2^-63^ to 2^63^-1.
51
+
Default is `1`.
51
52
52
53
|increment
53
-
|Increment of the sequence (when using `NEXT VALUE FOR __seq_name__`);
54
+
|Increment of the sequence when using `NEXT VALUE FOR __seq_name__`;
54
55
cannot be `0`.
55
-
Default is 1.
56
+
Default is `1`.
56
57
|===
57
58
58
-
The statements `CREATE SEQUENCE` and `CREATE GENERATOR` are synonymous -- both create a new sequence.
59
-
Either can be used, but `CREATE SEQUENCE` is recommended as that is the syntax defined in the SQL standard.
60
-
61
-
When a sequence is created, its current value is set so that the next value obtained from `NEXT VALUE FOR __seq_name__` is equal to _start_value_.
59
+
When a sequence is created, its current value is set so that the next value produced by `NEXT VALUE FOR __seq_name__` is equal to _start_value_.
62
60
In other words, the current value of the sequence is set to (`__start_value__ - __increment__`).
63
-
By default, the _start_value_ is 1 (one).
64
61
65
-
The optional `INCREMENT [BY]` clause allows you to specify an increment for the <<fblangref40-commons-nxtvlufor,`NEXT VALUE FOR _seq_name_`>> expression.
66
-
By default, the increment is 1 (one).
67
-
The increment cannot be set to 0 (zero).
68
-
The `GEN_ID(seq_name, <step>)` function can be called instead, to "`step`" the series by a different integer number.
69
-
The increment specified through `INCREMENT [BY]` is not used for `GEN_ID`.
62
+
The optional `INCREMENT [BY]` clause allows you to specify a non-zero increment for the <<fblangref40-commons-nxtvlufor,`NEXT VALUE FOR __seq_name__`>> expression.
63
+
64
+
The <<fblangref40-scalarfuncs-gen-id,`GEN_ID(seq_name, __step__)`>> function can be called instead, to "`step`" the sequence by a different increment.
65
+
The increment specified through `INCREMENT [BY]` is not used by `GEN_ID`.
66
+
Using both `NEXT VALUE FOR` and `GEN_ID`, especially when the sequence has an increment other than `1`, may result in values you did not expect.
67
+
For example, if you execute `CREATE SEQUENCE x START WITH 10 INCREMENT BY 10`, and then use `GEN_ID(x, 1)`, the value returned is `1`, and if you then call `NEXT VALUE FOR x`, you get `11`.
70
68
71
69
.Non-standard behaviour for negative increments
72
70
[NOTE]
73
71
====
74
72
The SQL standard specifies that sequences with a negative increment should start at the maximum value of the sequence (2^63^ - 1) and count down.
75
-
Firebird does not do that, and instead starts at `1`.
73
+
Firebird does not do that, and instead starts at `1` unless you specify a `START WITH` value.
76
74
77
75
This may change in a future Firebird version.
78
76
====
79
77
78
+
The statements `CREATE SEQUENCE` and `CREATE GENERATOR` are synonymous -- both create a new sequence.
79
+
Either can be used, but `CREATE SEQUENCE` is recommended as that is the syntax defined in the SQL standard.
80
+
80
81
[[fblangref40-ddl-sequence-create-who]]
81
82
=== Who Can Create a Sequence?
82
83
@@ -85,7 +86,7 @@ The `CREATE SEQUENCE` (`CREATE GENERATOR`) statement can be executed by:
* Users with the `CREATE SEQUENCE` (`CREATE GENERATOR`) privilege
87
88
88
-
The user executing the `CREATE SEQUENCE` (`CREATE GENERATOR`) statement becomes its owner.
89
+
The user executing `CREATE SEQUENCE` (`CREATE GENERATOR`) becomes its owner.
89
90
90
91
[[fblangref40-ddl-sequence-create-example]]
91
92
=== Examples of `CREATE SEQUENCE`
@@ -137,7 +138,7 @@ DSQL
137
138
[listing,subs=+quotes]
138
139
----
139
140
ALTER {SEQUENCE | GENERATOR} _seq_name_
140
-
[RESTART [WITH _newvalue_]]
141
+
[RESTART [WITH _start_value_]]
141
142
[INCREMENT [BY] _increment_]
142
143
----
143
144
@@ -149,49 +150,46 @@ ALTER {SEQUENCE | GENERATOR} _seq_name_
149
150
^| Description
150
151
151
152
|seq_name
152
-
|Sequence (generator) name
153
+
|Sequence name
153
154
154
-
|newvalue
155
-
|New sequence (generator) value.
155
+
|start_value
156
+
|Next value produced by `NEXT VALUE FOR __seq_name__`.
156
157
A 64-bit integer from -2^-63^ to 2^63^-1.
158
+
Default is the start value in the metadata.
157
159
158
160
|increment
159
161
|Increment of the sequence (when using `NEXT VALUE FOR __seq_name__`);
160
162
cannot be `0`.
161
163
|===
162
164
163
-
The `ALTER SEQUENCE` statement sets the current value of a sequence to the specified value
164
-
and/or changes the increment of the sequence.
165
+
The `ALTER SEQUENCE` statement sets the next value of the sequence, and/or changes the increment of the sequence.
165
166
166
-
The `RESTART WITH __newvalue__` clause allows you to set the next value generated by `NEXT VALUE FOR __seq_name__`.
167
-
To achieve this, the current value of the sequence is set to (`__newvalue__ - __increment__`) with _increment_ either as specified in the statement, or stored in the metadata of the sequence.
168
-
The `RESTART` clause (without `WITH`) restarts the sequence with the initial value stored in the metadata of the sequence.
167
+
The `RESTART WITH __start_value__` clause sets the current value of the sequence so that the next value obtained from <<fblangref40-commons-nxtvlufor,`NEXT VALUE FOR __seq_name__`>> is equal to _start_value_.
168
+
To achieve this, the current value of the sequence is set to (`__start_value__ - __increment__`) with _increment_ either as specified in the statement, or from the metadata of the sequence.
169
+
The `RESTART` clause without `WITH __start_value__` behaves as if `WITH __start_value__` is specified with the start value from the metadata of the sequence.
169
170
170
171
[NOTE]
171
172
====
172
-
Contrary to Firebird 3.0, in Firebird 4.0 `RESTART WITH __newvalue__` only restarts the sequence with the specified value, and does not store _newvalue_ as the new initial value of the sequence.
173
-
A subsequent `ALTER SEQUENCE RESTART` will use the initial value specified when the sequence was created, and not the _newvalue_ of this statement.
173
+
Contrary to Firebird 3.0, since Firebird 4.0 `RESTART WITH __start_value__` only restarts the sequence with the specified value, and does not store _start_value_ as the new start value of the sequence.
174
+
A subsequent `ALTER SEQUENCE RESTART` will use the start value specified when the sequence was created, and not the _start_value_ of this statement.
174
175
This behaviour is specified in the SQL standard.
175
176
176
-
It is currently not possible to change the initial value stored in the metadata.
177
+
It is currently not possible to change the start value stored in the metadata.
177
178
====
178
179
179
180
[WARNING]
180
181
====
181
-
Incorrect use of the `ALTER SEQUENCE` statement (changing the current value of the sequence or generator) is likely to break the logical integrity of data, or result in primary key or unique constraint violations.
182
+
Incorrect use of `ALTER SEQUENCE` -- changing the current value of the sequence or generator -- is likely to break the logical integrity of data, or result in primary key or unique constraint violations.
182
183
====
183
184
184
185
`INCREMENT [BY]` allows you to change the sequence increment for the `NEXT VALUE FOR` expression.
185
186
186
-
[NOTE]
187
-
====
188
187
Changing the increment value takes effect for all queries that run after the transaction commits.
189
188
Procedures that are called for the first time after changing the commit, will use the new value if they use `NEXT VALUE FOR`.
190
-
Procedures that were already used (and cached in the metadata cache) will continue to use the old increment.
189
+
Procedures that were already cached in the metadata cache will continue to use the old increment.
191
190
You may need to close all connections to the database for the metadata cache to clear, and the new increment to be used.
192
191
Procedures using `NEXT VALUE FOR` do not need to be recompiled to see the new increment.
193
192
Procedures using `GEN_ID(gen, expression)` are not affected when the increment is changed.
194
-
====
195
193
196
194
[[fblangref40-ddl-sequence-alter-who]]
197
195
=== Who Can Alter a Sequence?
@@ -211,7 +209,7 @@ The `ALTER SEQUENCE` (`ALTER GENERATOR`) statement can be executed by:
211
209
----
212
210
ALTER SEQUENCE EMP_NO_GEN RESTART WITH 145;
213
211
----
214
-
. Resetting the base value of the sequence `EMP_NO_GEN` to the initial value stored in the metadata
212
+
. Resetting the sequence `EMP_NO_GEN` to the start value stored in the metadata
215
213
+
216
214
[source]
217
215
----
@@ -252,23 +250,24 @@ CREATE OR ALTER {SEQUENCE | GENERATOR} _seq_name_
252
250
^| Description
253
251
254
252
|seq_name
255
-
|Sequence (generator) name.
253
+
|Sequence name.
256
254
The maximum length is 63 characters
257
255
258
256
|start_value
259
-
|Initial value of the sequence.
260
-
Default is 1.
257
+
|First or next value produced by `NEXT VALUE FOR __seq_name__`.
258
+
A 64-bit integer from -2^-63^ to 2^63^-1.
259
+
Default is `1`.
261
260
262
261
|increment
263
-
|Increment of the sequence (when using `NEXT VALUE FOR __seq_name__`);
262
+
|Increment of the sequence when using `NEXT VALUE FOR __seq_name__`;
264
263
cannot be `0`.
265
-
Default is 1.
264
+
Default is `1`.
266
265
|===
267
266
268
-
If the sequence does not exist, it will be created.
267
+
If the sequence does not exist, it will be created as documented under <<fblangref40-ddl-sequence-create>>.
269
268
An existing sequence will be changed:
270
269
271
-
- If `RESTART` is specified, the sequence will restarted with the initial value stored in the metadata
270
+
- If `RESTART` is specified, the sequence is restarted with the start value stored in the metadata
272
271
- If the `START WITH` clause is specified, the sequence is restarted with _start_value_, but the _start_value_ is not stored.
273
272
In other words, it behaves as `RESTART WITH` in <<fblangref40-ddl-sequence-alter>>.
274
273
- If the `INCREMENT [BY]` clause is specified, _increment_ is stored as the increment in the metadata, and used for subsequent calls to `NEXT VALUE FOR`
@@ -310,14 +309,14 @@ DROP {SEQUENCE | GENERATOR} _seq_name_
310
309
^| Description
311
310
312
311
|seq_name
313
-
|Sequence (generator) name.
312
+
|Sequence name.
314
313
The maximum length is 63 characters
315
314
|===
316
315
317
-
The statements `DROP SEQUENCE` and `DROP GENERATOR` statements are equivalent: both drop (delete) an existing sequence (generator).
316
+
The statements `DROP SEQUENCE` and `DROP GENERATOR` are equivalent: both drop (delete) an existing sequence.
318
317
Either is valid but `DROP SEQUENCE`, being defined in the SQL standard, is recommended.
319
318
320
-
The statements will fail if the sequence (generator) has dependencies.
319
+
The statements will fail if the sequence has dependencies.
0 commit comments