Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to design an elegant database ID

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to design an elegant database ID". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to design an elegant database ID".

Self-increasing ID

This method is the easiest to use and is the one that many programmers like to use. Usage: mysql has sequence in auto_increment;oracle.

The shortcomings of this approach are obvious and easy to detect. If I am a blog system, the id=10 of an article, then the address displayed on the browser is like this: www.xxxx.com/article?id=10, for people with some program experience, he will type www.xxxx.com/article?id=11,id=12 directly on the browser, or even use http testing tools such as postman,jmeter, so that all articles can be detected. The blog article system may not matter, but if it is some mall systems, it is easy to reveal important information, such as how many orders the system has, and the amount and status of each order can be tested.

Another disadvantage is that when we do a new operation, the ID is self-increasing in the database, but the code business layer does not know that if we want to do other operations with this ID, we have to re-check the database.

Database UUID

This method solves the problem that self-increasing ID is easy to be detected. Use the uuid () function of mysql to generate 32-bit hexadecimal numbers, which will not be repeated in your lifetime, as shown below:

But it still has a disadvantage, that is, when adding new operations, the business layer does not know the ID, so it has to re-check the database to know.

JAVA generates UUID

This approach solves a problem with database UUID, where ID is generated by JAVA code and reduces one database query. This is the way most projects use, as follows

Public class UuidUtil {

/ * *

* get a UUID

, /

Public static String getUUID () {

String uuid = UUID.randomUUID () .toString ()

/ / remove the "-" symbol

Return uuid.replaceAll ("-", ")

}

}

Elegant short UUID

Although the way JAVA generates UUID is already very common, it still has one small drawback: it takes up too much space, and the ID of all tables takes up 32-bit characters. So I designed a short UUID, the principle is to generate an 8-bit 62-digit number, all the numbers, uppercase and lowercase letters are used (database UUID is hexadecimal, using only numbers and 6 letters). The code is as follows

Public class UuidUtil {

Public static 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 static String getShortUuid () {

StringBuffer shortBuffer = new StringBuffer ()

String uuid = UUID.randomUUID (). ToString (). Replace ("-", ")

For (int I = 0; I < 8; iTunes +) {

String str = uuid.substring (I * 4, I * 4 + 4)

Int x = Integer.parseInt (str, 16)

ShortBuffer.append (chars [x% 0x3E])

}

Return shortBuffer.toString ()

}

}

The generated ID is as follows:

Thank you for reading, the above is the content of "how to design an elegant database ID". After the study of this article, I believe you have a deeper understanding of how to design an elegant database ID, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report