user3393982
user3393982

Reputation: 33

How do I create new column with random value( random 6 numeric unique - as serials key) into mysql?

I have a table in MySQL Database with 8 columns. I want to create a new column. This column will automatically insert a random 6 digit value. Each new entry will be created with a unique value (from 000000 to 999999).

Is this possible?

Upvotes: 2

Views: 193

Answers (1)

Up_One
Up_One

Reputation: 5271

What i think shoud do the job is :

  • create a porcedure that will handle the random data insert and check if the values exists. to generate random number

    SELECT FLOOR(10000 + RAND() * 999999) AS random_number;

  • create a trigger that will call this proc every time an insert will be done !

    CREATE TRIGGER bla_bla AFTER INSERT ON your_table
    FOR EACH ROW BEGIN
    call proc();
    END;//

Upvotes: 1

Related Questions