Skip to content

Commit

Permalink
Merge pull request #4 from Oreoxmt/review_seq_func
Browse files Browse the repository at this point in the history
Sequence functions: refine wording
  • Loading branch information
dveeden authored Jun 7, 2024
2 parents 32bd22a + 0da159b commit 6095fad
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions functions-and-operators/sequence-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,33 @@ summary: This document introduces sequence functions supported in TiDB.

Sequence functions in TiDB are used to return or set values of sequence objects created using the [`CREATE SEQUENCE`](/sql-statements/sql-statement-create-sequence.md) statement.

| Function name | Feature description |
| Function name | Description |
| :-------------- | :------------------------------------- |
| [`NEXTVAL()`](#nextval) | Returns the next value of a sequence |
| [`NEXT VALUE FOR`](#next-value-for) | Returns the next value of a sequence |
| [`SETVAL()`](#setval) | Sets the current value of a sequence |
| [`LASTVAL()`](#lastval) | Returns the last used value of a sequence |
| [`NEXTVAL()`](#nextval) | Returns the next value of a sequence. |
| [`NEXT VALUE FOR`](#next-value-for) | Returns the next value of a sequence (alias for `NEXTVAL()`). |
| [`SETVAL()`](#setval) | Sets the current value of a sequence. |
| [`LASTVAL()`](#lastval) | Returns the last used value of a sequence in the current session. |

## `NEXTVAL()`

The `NEXTVAL()` functions returns the next value from the sequence
The `NEXTVAL()` function returns the next value of a sequence.

Example:

We create a sequence called `s1`.
Create a sequence named `s1`:

```sql
CREATE SEQUENCE s1;
```

```
Query OK, 0 rows affected (0.01 sec)
```

Then we request the next value from `s1`.
Get the next value from `s1`:

```sql
SELECT NEXTVAL(s1);
```

The output is as follows:

```
+-------------+
| NEXTVAL(s1) |
Expand All @@ -47,12 +45,18 @@ SELECT NEXTVAL(s1);

## `NEXT VALUE FOR`

An alias for [`NEXTVAL()`](#nextval)
The `NEXT VALUE FOR` function is an alias for [`NEXTVAL()`](#nextval).

Example:

Get the next value from `s1` using `NEXTVAL()`:

```sql
SELECT NEXTVAL(s1);
```

The output is as follows:

```
+-------------+
| NEXTVAL(s1) |
Expand All @@ -62,10 +66,14 @@ SELECT NEXTVAL(s1);
1 row in set (0.00 sec)
```

Get the next value from `s1` using `NEXT VALUE FOR`:

```sql
SELECT NEXT VALUE FOR s1;
```

The output is as follows:

```
+-------------------+
| NEXT VALUE FOR s1 |
Expand All @@ -77,14 +85,18 @@ SELECT NEXT VALUE FOR s1;

## `SETVAL()`

The `SETVAL(n)` function can be use to set a sequence to a specific value.
The `SETVAL(n)` function sets the current value of a sequence.

We request the next value from `s1`.
Example:

Get the next value from `s1`:

```sql
SELECT NEXTVAL(s1);
```

The output is as follows:

```
+-------------+
| NEXTVAL(s1) |
Expand All @@ -94,12 +106,14 @@ SELECT NEXTVAL(s1);
1 row in set (0.00 sec)
```

Now we set the value to `10`.
Set the current value of `s1` to `10`:

```sql
SELECT SETVAL(s1, 10);
```

The output is as follows:

```
+----------------+
| SETVAL(s1, 10) |
Expand All @@ -109,12 +123,14 @@ SELECT SETVAL(s1, 10);
1 row in set (0.00 sec)
```

And now we can see that the next value (the value after `10`) is `11`.
Verify the next value after setting it to `10`:

```sql
SELECT NEXTVAL(s1);
```

The output is as follows:

```
+-------------+
| NEXTVAL(s1) |
Expand All @@ -126,12 +142,18 @@ SELECT NEXTVAL(s1);

## `LASTVAL()`

The `LASTVAL()` function returns the last value that was generated by the sequence in **this session**.
The `LASTVAL()` function returns the last value generated by the sequence **in the current session**.

Example:

Get the next value from `s1`:

```sql
SELECT LASTVAL(s1);
```

The output is as follows:

```
+-------------+
| LASTVAL(s1) |
Expand Down

0 comments on commit 6095fad

Please sign in to comment.