在PostgreSQL表中生成测试数据

在PostgreSQL表中生成测试数据,第1张

概述我想用一个SELECT查询为这个表创建100个测试行数据: CREATE TABLE DOCUMENT_TEMPLATE( ID INTEGER NOT NULL, NAME TEXT, SHORT_DESCRIPTION TEXT, AUTHOR TEXT, DESCRIPTION TEXT, CONTENT TEXT, LAST_UPDATED DAT 我想用一个SELECT查询为这个表创建100个测试行数据:
CREATE table document_TEMPLATE(   ID INTEGER NOT NulL,name TEXT,SHORT_DESCRIPTION TEXT,AUTHOR TEXT,DESCRIPTION TEXT,CONTENT TEXT,LAST_UPDATED DATE,CREATED DATE);

你能举个例子吗?

最抢劫的方法是使用像 SQL Data Generator这样的特定工具.

生成随机数据的简单方法是使用random()和generate_serIEs.

INSERT INTO document_TEMPLATE(ID,name,short_description,author,description,content,last_updated,created)SELECT ID,'name',md5(random()::text),'name2',Now() - '1 day'::INTERVAL * (RANDOM()::int * 100),Now() - '1 day'::INTERVAL * (RANDOM()::int * 100 + 100)FROM generate_serIEs(1,100) ID;

SqlFiddleDemo

您总是可以更自然地编写自定义函数来生成名字/姓氏/数字/城市/ lorem_ipsum等.

这是我快速编写的内联查询:

INSERT INTO document_TEMPLATE(ID,created)WITH base(ID,n1,n2,n3,n4,n5,n6,n7) AS(  SELECT ID,MIN(CASE WHEN rn = 1 THEN nr END),MIN(CASE WHEN rn = 2 THEN nr END),MIN(CASE WHEN rn = 3 THEN nr END),MIN(CASE WHEN rn = 4 THEN nr END),MIN(CASE WHEN rn = 5 THEN nr END),MIN(CASE WHEN rn = 6 THEN nr END),MIN(CASE WHEN rn = 7 THEN nr END)   FROM generate_serIEs(1,100) ID     -- number of rows,LAteraL( SELECT nr,ROW_NUMBER() OVER (ORDER BY ID * random())             FROM generate_serIEs(1,900) nr          ) sub(nr,rn)   GROUP BY ID),dict(lorem_ipsum,names) AS(   SELECT 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris lacus arcu,blandit non semper elementum,fringilla soDales est. Ut porttitor blandit sAPIen pellentesque pretium. Donec ut diam sed urna venenatis hendrerit. Nulla eros arcu,mattis vitae congue cursus,tincIDunt sed turpis. Curabitur non enim diam,eget elementum dolor. Vivamus enim tortor,tempor at vehicula ac,malesuada ID est. Praesent at nibh eget metus dAPIbus dAPIbus. Donec arcu orci,sagittis eu interdum vitae,facilisis quis nibh.Mauris luctus molestIE velit,at vestibulum magna cursus sit amet. Nulla in accumsan libero. Donec sed sem lectus. Mauris congue sAPIen et diam euismod vitae scelerisque diam tincIDunt. Praesent a justo enim,vitae venenatis dolor. Donec in tortor at magna dAPIbus suscipit sit amet a libero. Vivamus porttitor rhoncus tellus,at luctus nisl semper bibendum. Fusce eget accumsan orci. Qout','{"James","John","Jimmy","Jessica","Jeffrey","Jonathan","Justin","Jaclyn","JodIE"}'::text[])SELECT b.ID,sub.*FROM base b,LAteraL (     SELECT names[b.n1 % 9+1],substring(lorem_ipsum::text,b.n2,20),names[b.n3 % 9+1],b.n4,100),b.n5,200),Now() - '1 day'::INTERVAL * (b.n6 % 365),(Now() - '1 day'::INTERVAL * (b.n7 % 365)) - '1 year' :: INTERVAL      FROM dict) AS sub(name,descriptionm,created);

SqlFiddleDemo

警告:我知道它可以大大改善.请将它作为起点处理.

总结

以上是内存溢出为你收集整理的在PostgreSQL表中生成测试数据全部内容,希望文章能够帮你解决在PostgreSQL表中生成测试数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1181355.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存