Questions tagged [random]

73 questions
22
votes
3 answers

How can I generate a random bytea

I would like to be able to generate random bytea fields of arbitrary length (<1Gb) for populating test data. What is the best way of doing this?
Jack Douglas
  • 40,517
  • 16
  • 106
  • 178
19
votes
2 answers

How to create column in DB with default value random string

Can I create column in DB table (PostgreSQL) which have default value random string, and how ? If is not possible, please let me know that.
tasmaniski
  • 1,195
  • 4
  • 13
  • 16
18
votes
3 answers

Set random value from set

I need to put some random values into database, but I don't want to end up with completely randomized text (like 7hfg43d3). Instead I would like to randomly pick one of values supplied by myself.
korda
  • 635
  • 2
  • 7
  • 11
16
votes
3 answers

Create hierarchy of multiple levels where each node has a random number of children

I need to create some test data that involves a hierarchy. I could make it easy and do a couple of CROSS JOINs, but that would give me a structure that is completely uniform / without any variation. That not only seems dull, but lack of variation in…
Solomon Rutzky
  • 70,048
  • 8
  • 160
  • 306
11
votes
5 answers

Get a truly RANDOM row from a PostgreSQL table quickly

I always used to do: SELECT column FROM table ORDER BY random() LIMIT 1; For large tables, this was unbearably, impossibly slow, to the point of being useless in practice. That's why I started hunting for more efficient methods. People…
user200642
9
votes
2 answers

Randomizing the Auto Increment in Mysql

We have a database, where we insert data into table whenever we receive an order from our services. We use auto_increment to generate the ID and assign the order with the same. This is usually same everywhere. But the problem is, since this…
RaceBase
  • 203
  • 1
  • 2
  • 7
8
votes
2 answers

Perform TABLESAMPLE with WHERE clause in PostgreSQL

I want to use TABLESAMPLE to randomly sample from rows that meet a certain condition with PostgreSQL. This runs fine: select * from customers tablesample bernoulli (1); But I cannot figure out how to embed the condition in the script. This for…
Reveille
  • 189
  • 1
  • 5
8
votes
4 answers

How do I do a complex GROUP BY in MySQL?

I have a table that contains several keys into other tables (where each key is comprised of multiple columns). I would like to be able to group rows together that have an equal key, but I don't want to group all of them together. It's not a simple…
Michael McGowan
  • 799
  • 2
  • 10
  • 20
7
votes
2 answers

PostgreSQL randomising combinations with LATERAL

In the following example I have a table foo from which I'd like to pick out at random a row per group. CREATE TABLE foo ( line INT ); INSERT INTO foo (line) SELECT generate_series(0, 999, 1); Let's say that I'd like to group by line % 10. I could…
beldaz
  • 1,740
  • 3
  • 16
  • 26
6
votes
4 answers

Primary key with randomly varying increments (so it cannot be guessed easily)

I would like the primary keys to be auto-incremented and generated but in varying increments. For ex, if I have increment range as 100... then the auto generated keys would be something like below: - 20 (random number between 1 and 100) - 30 (add…
5
votes
2 answers

How to Randomly Select a Date?

I am using in a Bash Shell Script this code for retrieving a random date. Is there a way to do that also in Microsoft SQL Server? I can imagine that through a single SELECT query and probably by calling a few functions that can be performed easily,…
user62547
5
votes
1 answer

order by random meaning (postgresql)

One possible way to select random rows in PostgreSQL is this: select * from table order by random() limit 1000; (see also here.) My question is, what does order by random() mean exactly? Is it that somehow a random number is generated and it is…
Attilio
  • 151
  • 1
  • 3
5
votes
1 answer

How do I make MySQL auto increment in random order?

I have a table that contains several keys into other tables (where each key is comprised of multiple columns). I would like to create a new column for each key that would be an integer such that value i represents the ith occurrence of that key…
Michael McGowan
  • 799
  • 2
  • 10
  • 20
5
votes
2 answers

May postgresql's uuid_generate_v1() conflict when we change the computer date/time?

According to postgresql uuid-ossp documentation uuid_generate_v1() is based on Mac address + timestamp: https://www.postgresql.org/docs/9.4/static/uuid-ossp.html On a distributed database scenario where we have hundreds of databases generating…
Thiago Sayão
  • 487
  • 8
  • 16
5
votes
1 answer

Generate fake data for several tables with relationships

I have 3 tables where I want to generate fake data in UsersCategoryLink. How can I insert in the table UserCategoryLink the column UserID with random users from the table User and a random id from the table Categories. In this SQL fiddle you can see…
H. Pauwelyn
  • 930
  • 6
  • 18
  • 35
1
2 3 4 5