nipponese Posted April 2, 2008 Share Posted April 2, 2008 Hi everyone, I am wondering, how can I create a column with a unique auto-generated random integer in each row. Obviously, something like this won't work: ALTER TABLE contact ADD COLUMN invoice_id INT(SELECT FLOOR(1000 + (RAND() * 9000))); But that's the idea... Quote Link to comment https://forums.phpfreaks.com/topic/99230-creating-a-new-column-with-a-random-int/ Share on other sites More sharing options...
fenway Posted April 2, 2008 Share Posted April 2, 2008 You could use a trigger.... Quote Link to comment https://forums.phpfreaks.com/topic/99230-creating-a-new-column-with-a-random-int/#findComment-507789 Share on other sites More sharing options...
gluck Posted April 2, 2008 Share Posted April 2, 2008 Hi everyone, I am wondering, how can I create a column with a unique auto-generated random integer in each row. Obviously, something like this won't work: ALTER TABLE contact ADD COLUMN invoice_id INT(SELECT FLOOR(1000 + (RAND() * 9000))); But that's the idea... use a timestamp column for invoice id and default it to current_timestamp. You can convert it to string when using in php or whatever language you user. Quote Link to comment https://forums.phpfreaks.com/topic/99230-creating-a-new-column-with-a-random-int/#findComment-507800 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.