您现在的位置是:网站首页> 编程资料编程资料
postgreSQL自动生成随机数值的实例_PostgreSQL_
2023-05-27
491人已围观
简介 postgreSQL自动生成随机数值的实例_PostgreSQL_
1、 随机生成身份证
新建一个函数,用来生成身份证号码,需要输入两个日期参数
create or replace function gen_id( a date, b date ) returns text as $$ select lpad((random()*99)::int::text, 2, '0') || lpad((random()*99)::int::text, 2, '0') || lpad((random()*99)::int::text, 2, '0') || to_char(a + (random()*(b-a))::int, 'yyyymmdd') || lpad((random()*99)::int::text, 2, '0') || random()::int || (case when random()*10 >9 then 'X' else (random()*9)::int::text end ) ; $$ language sql strict;
生成10个随机身份证号码
select gen_id('1900-01-01', '2017-10-16') from generate_series(1,10); 生成十万条随机身份证号码
insert into testpg SELECT generate_series(1,100000) as xm, gen_id('1900-01-01', '2017-10-16') as num;补充:postgreSql的id设置自动生成随机24位数字与字母组合(uuid)
我就废话不多说了,大家还是直接看代码吧~
@Id @GeneratedValue(generator="system_uuid") @GenericGenerator(name="system_uuid",strategy="uuid") @Column(name = "ID", unique = true, nullable = false, length = 24) public String getId() { return this.id; } 以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
您可能感兴趣的文章:
相关内容
- 使用postgresql 模拟批量数据插入的案例_PostgreSQL_
- postgresql 查询字符串中是否包含某字符的操作_PostgreSQL_
- PostgreSQL 自动Vacuum配置方式_PostgreSQL_
- 浅谈pg_hint_plan定制执行计划_PostgreSQL_
- PostgreSQL 慢查询SQL跟踪操作_PostgreSQL_
- CentOS PostgreSQL 12 主从复制(主从切换)操作_PostgreSQL_
- PostgreSQL 查看表的主外键等约束关系详解_PostgreSQL_
- PostgreSQL 修改视图的操作_PostgreSQL_
- PostgreSQL 更新视图脚本的注意事项说明_PostgreSQL_
- postgreSQL中的row_number() 与distinct用法说明_PostgreSQL_
