-- Target only an explicitly authorized local/dev connection and baiyue_iam. -- Preserve existing customer rows and never reset an existing sequence backwards. USE baiyue_iam; CREATE TABLE IF NOT EXISTS iam_number_sequence ( sequence_key VARCHAR(64) NOT NULL COMMENT '业务编号类型', next_value BIGINT UNSIGNED NOT NULL COMMENT '下一待分配数字', updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', PRIMARY KEY (sequence_key) ) ENGINE=InnoDB COMMENT='IAM业务编号序列表'; START TRANSACTION; INSERT IGNORE INTO iam_number_sequence(sequence_key, next_value) VALUES ('CUSTOMER_CODE', 1); SELECT next_value FROM iam_number_sequence WHERE sequence_key='CUSTOMER_CODE' FOR UPDATE; SET @customer_code_floor = ( SELECT COALESCE(MAX(CAST(tenant_code AS UNSIGNED)), 0) + 1 FROM iam_tenant WHERE tenant_code REGEXP '^[0-9]{5}$' ); UPDATE iam_number_sequence SET next_value=GREATEST(next_value,@customer_code_floor) WHERE sequence_key='CUSTOMER_CODE'; COMMIT; SHOW CREATE TABLE iam_number_sequence; SELECT sequence_key,next_value FROM iam_number_sequence;