Files
bl/help/修改自增id.sql
xinian d545093124
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
编辑文件 修改自增id.sql
2026-03-01 01:16:26 +08:00

49 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

select setval('base_sys_user_id_seq', 10000000, false);
-- 清理旧函数
ALTER TABLE base_sys_user
ALTER COLUMN id SET DEFAULT nextval('base_sys_user_id_seq');
DROP FUNCTION IF EXISTS shuffle_8digit() CASCADE;
-- 8位自增 纯数字位置互换 依旧8位 100%不重复
CREATE OR REPLACE FUNCTION shuffle_8digit()
RETURNS bigint
LANGUAGE plpgsql
VOLATILE
AS $$
DECLARE
seq bigint;
d0 int; d1 int; d2 int; d3 int;
d4 int; d5 int; d6 int; d7 int;
BEGIN
seq := nextval('base_sys_user_id_seq'); -- 原本就是8位
-- 8 位数字拆出来d7 d6 d5 d4 d3 d2 d1 d0
d7 := (seq / 10000000) % 10;
d6 := (seq / 1000000) % 10;
d5 := (seq / 100000) % 10;
d4 := (seq / 10000) % 10;
d3 := (seq / 1000) % 10;
d2 := (seq / 100) % 10;
d1 := (seq / 10) % 10;
d0 := seq % 10;
-- 固定位置互换一对一置换绝对不重复
RETURN
d3*10000000 +
d7*1000000 +
d1*100000 +
d5*10000 +
d2*1000 +
d6*100 +
d0*10 +
d4;
END;
$$;
-- 启用
ALTER TABLE base_sys_user
ALTER COLUMN id SET DEFAULT shuffle_8digit();