Unique identification of order

Posted by winggundamth on Sat, 02 Nov 2019 21:16:48 +0100

When designing a table structure in a normal development system, it is often necessary to have a field to identify the uniqueness of the record, which is implemented from different dimensions of the java level and the database level

java generates unique identity

Version 1

Unique identifier UUID generated by java

System.out.println(UUID.randomUUID().toString());

output

4880d2b4-bc5f-48d1-ba58-003334276d46

Obviously it's too long and unfriendly

Version two

Further processing to generate 8-bit UUID

    public String[] chars = new String[]
            {
                    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
                    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
            };

    public String getShortUuid() {
        StringBuffer stringBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int strInteger = Integer.parseInt(str, 16);
            stringBuffer.append(chars[strInteger % 0x3E]);
        }

        return stringBuffer.toString();
    }
    @Test
    public void test4(){
        TestDemo2 testDemo2 = new TestDemo2();
        System.out.println(UUID.randomUUID().toString());
        System.out.println(testDemo2.getShortUuid());
    }

output

4880d2b4-bc5f-48d1-ba58-003334276d46
M3EeoAYr

Version three

Timestamp plus generated 8-bit UUID

    @Test
    public void test3() {
        TestDemo2 testDemo2 = new TestDemo2();
        String shortUuid = testDemo2.getShortUuid();
        String create_time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        System.out.println(create_time+shortUuid);
    }

output

20190925101216hh42xxn6

It is recommended to use the timestamp plus the generated 8-bit UUID

oracle generates unique identity

Version 1

Unique identification of serial number generation

Create sequence

-- Create sequence 
create sequence SEQ_ORDER_ID
minvalue 100000
maxvalue 999999
start with 414591
increment by 1
nocache
cycle;

field Explain
minvalue Define the minimum value that a sequence generator can produce
maxvalue Define the maximum value that a sequence generator can produce
start with Defines the initial value of the sequence (that is, the first value generated), which is 1 by default
increment by Defines the step size (increment) of the sequence. If omitted, it defaults to 1. If a negative value appears, the value of the sequence is decremented according to this step size
nocache Indicates that the sequence is not buffered in memory
cycle Indicates whether to loop when the value of the sequence generator reaches the limit value. CYCLE means CYCLE, NOCYCLE means no CYCLE

Get serial number

SELECT SEQ_ORDER_ID.NEXTVAL FROM DUAL;

output

414785

Version two

Time stamp plus serial number

SELECT TO_CHAR(SYSDATE,'YYYYMMDDHH24MISS')||SEQ_ORDER_ID.NEXTVAL FROM DUAL;

output

20190925102115414805

Time stamp plus serial number is recommended

This article is based on the platform of blog one article multiple sending OpenWrite Release!

Topics: Programming Java Database Oracle