2

I apologize if this has been answered elsewhere. I found similar posts but nothing that exactly matches what I was looking for. I am wondering if it is possible to randomly assign a number as data is entered into a table. For example, for this table

CREATE TABLE test (
id_number INTEGER NOT NULL,
second_number INTEGER NOT NULL)

I would like to be able to do the following:

INSERT INTO test (second_number)
VALUES (1234567)

where id_number is then populated with a random 10-digit number. I guess I want this similar to SERIAL in the way it populates but it needs to be 10 digits and random. Thanks very much

quite_cool
  • 21
  • 1
  • 3
  • 2
    Does this answer your question? [Generate a random number in the range 1 - 10](https://stackoverflow.com/questions/1400505/generate-a-random-number-in-the-range-1-10) – Henry Woody Sep 22 '20 at 01:03

1 Answers1

4

You can try this expression:

CAST(1000000000 + floor(random() * 9000000000) AS bigint)

This will give you a random number between 1000000000 and 9999999999.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263