新建一個(gè)函數(shù),用來(lái)生成身份證號(hào)碼,需要輸入兩個(gè)日期參數(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個(gè)隨機(jī)身份證號(hào)碼
select gen_id('1900-01-01', '2017-10-16') from generate_series(1,10);
生成十萬(wàn)條隨機(jī)身份證號(hào)碼
insert into testpg SELECT generate_series(1,100000) as xm, gen_id('1900-01-01', '2017-10-16') as num;
補(bǔ)充:postgreSql的id設(shè)置自動(dòng)生成隨機(jī)24位數(shù)字與字母組合(uuid)
我就廢話(huà)不多說(shuō)了,大家還是直接看代碼吧~
@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; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
標(biāo)簽:溫州 寶雞 昭通 濮陽(yáng) 海西 杭州 榆林 辛集
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《postgreSQL自動(dòng)生成隨機(jī)數(shù)值的實(shí)例》,本文關(guān)鍵詞 postgreSQL,自動(dòng)生成,隨機(jī),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。