Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

postgresql - SQL Populate table with random data

I have a table with two fields:

  1. id(UUID) that is primary Key and
  2. description (var255)

I want to insert random data with SQL sentence. I would like that description would be something random.

PS: I am using PostgreSQL.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I dont know exactly if this fits the requirement for a "random description", and it's not clear if you want to generate the full data: but, for example, this generates 10 records with consecutive ids and random texts:

  test=#  SELECT generate_series(1,10) AS id, md5(random()::text) AS descr;

     id |               descr
    ----+----------------------------------
      1 | 65c141ee1fdeb269d2e393cb1d3e1c09
      2 | 269638b9061149e9228d1b2718cb035e
      3 | 020bce01ba6a6623702c4da1bc6d556e
      4 | 18fad4813efe3dcdb388d7d8c4b6d3b4
      5 | a7859b3bcf7ff11f921ceef58dc1e5b5
      6 | 63691d4a20f7f23843503349c32aa08c
      7 | ca317278d40f2f3ac81224f6996d1c57
      8 | bb4a284e1c53775a02ebd6ec91bbb847
      9 | b444b5ea7966cd76174a618ec0bb9901
     10 | 800495c53976f60641fb4d486be61dc6
    (10 rows)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...