Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 6046

With MySql is there any way to create a global endless sequence integer, other than the trick of using a "replace" table? [duplicate]

$
0
0

In most systems you need a global, endlessly increasing sequence number. With MySql the only way I know to do it is, make a new small table for the purpose:

mysql>  CREATE TABLE `ubersequence` (    ->  `id` int NOT NULL AUTO_INCREMENT,    ->  `unused` varchar(32) not null unique,    ->  PRIMARY KEY (`id`) );Query OK, 0 rows affected (0.05 sec)mysql> insert into ubersequence (unused) values ('snap');Query OK, 1 row affected (0.01 sec)mysql> select * from ubersequence ;+----+--------+| id | unused |+----+--------+|  1 | snap   |+----+--------+1 row in set (0.00 sec)

Every time you need the next value, just replace in to it and then read the new id.

mysql> replace into ubersequence (unused) values ('snap') ;Query OK, 2 rows affected (0.01 sec)mysql> select * from ubersequence ;+----+--------+| id | unused |+----+--------+|  2 | snap   |+----+--------+1 row in set (0.00 sec)mysql> replace into ubersequence (unused) values ('snap') ;Query OK, 2 rows affected (0.01 sec)mysql> select * from ubersequence ;+----+--------+| id | unused |+----+--------+|  3 | snap   |+----+--------+1 row in set (0.00 sec)

Don't do this more than two billion times!

(Of course, those two steps and likely more will have to be atomic transaction in your code.)

In short, is there a way in MySql I don't know about, other than using a utility table as above, to get a global increasing seq?

(By global I mean database-wide. But indeed if it was installation wide that would obviously be equally as good and work the same.)

Purely FTR I use

/home/ubuntu/main# mysql -Vmysql  Ver 8.0.39-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))

typically on Ubuntu, just in case it matters.


PS, of course within one table it will indeed within the table increment the the id for you if using the "replace" technique; my question is about, as I say, database-wide (or even myssql install wide would be fine).


Viewing all articles
Browse latest Browse all 6046

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>