将100万随机数据插入PostgreSQL

将100万随机数据插入PostgreSQL,postgresql,Postgresql,表结构如下所示: milliontable( name varchar(10), age integer, joindate date ) 我想在这个表中随机插入一百万个数据。有什么方法可以做到这一点吗?您可以使用递归查询。首先是生成从1到1M的行。然后使用随机函数生成1到99岁之间的随机年龄 with recursive cte as ( select 0 as ctr union all select ctr + 1 from cte where ct

表结构如下所示:

milliontable(
   name varchar(10),
   age integer,
   joindate date
)
我想在这个表中随机插入一百万个数据。有什么方法可以做到这一点吗?

您可以使用递归查询。首先是生成从1到1M的行。然后使用随机函数生成1到99岁之间的随机年龄

with recursive cte as (
   select 0 as ctr
   union all 
   select ctr + 1 from cte where ctr < 1000000
)
insert into milliontable (name, age, joindate) 
select 'name'||cast(ctr as varchar(30)) as name, floor(random()*(99)) as age, current_timestamp as joindate 
from cte;
试试。

您可以使用递归查询。首先是生成从1到1M的行。然后使用随机函数生成1到99岁之间的随机年龄

with recursive cte as (
   select 0 as ctr
   union all 
   select ctr + 1 from cte where ctr < 1000000
)
insert into milliontable (name, age, joindate) 
select 'name'||cast(ctr as varchar(30)) as name, floor(random()*(99)) as age, current_timestamp as joindate 
from cte;
试试。

使用随机函数生成随机值:

INSERT INTO milliontable (name, age, joindate)
SELECT substr(md5(random()::text), 1, 10),
       (random() * 70 + 10)::integer,
       DATE '2018-01-01' + (random() * 700)::integer
FROM generate_series(1, 1000000);
将一个人的年龄存储在一张表中通常是一个愚蠢的想法,因为随着时间的推移,这个数字会自动出错。使用生日。

使用随机函数生成随机值:

INSERT INTO milliontable (name, age, joindate)
SELECT substr(md5(random()::text), 1, 10),
       (random() * 70 + 10)::integer,
       DATE '2018-01-01' + (random() * 700)::integer
FROM generate_series(1, 1000000);
将一个人的年龄存储在一张表中通常是一个愚蠢的想法,因为随着时间的推移,这个数字会自动出错。利用生日