這篇文章將為大家詳細(xì)講解有關(guān)如何在postgreSQL數(shù)據(jù)庫中自動生成隨機(jī)數(shù)值,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
新建一個函數(shù),用來生成身份證號碼,需要輸入兩個日期參數(shù)
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個隨機(jī)身份證號碼
select gen_id('1900-01-01', '2017-10-16') from generate_series(1,10);
生成十萬條隨機(jī)身份證號碼
insert into testpg SELECT generate_series(1,100000) as xm, gen_id('1900-01-01', '2017-10-16') as num;
補(bǔ)充:postgreSql的id設(shè)置自動生成隨機(jī)24位數(shù)字與字母組合(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; }
關(guān)于如何在postgreSQL數(shù)據(jù)庫中自動生成隨機(jī)數(shù)值就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。