Question

I am coding a messaging system and I don't want to have short IDs shown, is there any way that MySQL can generate a UID which is unique as UID suggests?

I need to know how I can do this with ONLY MySQL, no PHP, JavaScript or anything else, just MySQL, if not possible, fine, I just want to make sure.

Sorry, I should have made it more clear when I say UID I just mean "Unique ID" but I need it in a multidigit number only, e.g. 914888629, 3890692140

Was it helpful?

Solution

If you don't want globally unique IDs, just a unique ID (a different number each time you call it), then you can create a table with only an integer auto_increment non-null column and issue the following two queries:

INSERT INTO tbl1 SET col1 = NULL;
SELECT LAST_INSERT_ID();

To start the number at something other than 1, for example, to start at 10 digits, set it like this:

ALTER TABLE tbl1 AUTO_INCREMENT = 1000000000

OTHER TIPS

MySQL has a function for that. Try:

SELECT UUID();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top