当前位置:网站首页>"Nonsense" database primary key design

"Nonsense" database primary key design

2022-06-10 09:53:00 51CTO


“ Nonsense ” DATABASE Primary key design _ Primary key

This is nonsense , In quotation marks, of course ,So How do we design the primary key in tables ? That's a complecated question.


Why?  Here is an example The primary key is higher “Joke”


Draw a picture first

“ Nonsense ” DATABASE Primary key design _ Primary key _02


There are four tables in the figure , At the top is the basic table , The following three tables have different functions , Some data are different , But the basic data is given by the top table .


“ Nonsense ” DATABASE Primary key design _ Primary key _03

Let's emphasize that this table is based on ORACLE 11G Of , And it is self incrementing , On the column , On the column , Say it three times .

At the beginning of the program design , The primary keys of the four tables should be the same , And the problem is ,HOW ,HOW ,HOW could you use the sequence ? 


know ORACLE The principle of , We all know ORACLE All my watches are HEAP surface , And the self increasing sequence is ORALCE Medium to physical , It doesn't make any sense , You want to add columns to the primary keys of the four tables “ binding ”, This is obviously “ A giant baby ”. The problem arises from this , call ORACLE The sequence then gets a value , Insert the value into the location of the primary key , There seems to be nothing wrong , Many people have designed it this way , But the key problem in this system is , You still have tables that need public primary keys , How do you guarantee the sequence of three tables ANY TIME ANY WHERE Can be consistent , Naive is too fake .


So when the auto increment sequence of one of the tables for some reason , become “ alternative ”, And the procedure is not rigorous , The data of the three tables are not consistent , And the inconsistency is great . The program has a duplicate primary key of one of the tables , Unable to insert data after ,( If three tables are inserted in one transaction , The data will not be inconsistent , Only report mistakes , Roll back , The mistake is not so bad , Unfortunately, third-party development doesn't matter whether you live or die , Once the function is completed ), The program inserts data repeatedly , The result is that there is no data in the table , The other two tables are all data that should not be inserted .


So when I heard this , Questions that arise at ease in my heart , Why use auto increasing sequence ?


In this case, if Non self increasing sequence , And generated by some mature frameworks “ID”, for example GUID, Or add some other values based on the timestamp , Through the algorithm “ The only value ”, It is estimated that even if the program has BUG , There will be no inconsistency in the data of the three tables , Because people can't imitate at will ID value , If you imitate this behavior, then “ Not just ” 了 .


OK , Words belong to the subject , Database primary key design is actually a very important thing , He's about , The performance of your program , High availability of your program , Does your program need to separate databases and tables , The logic of your program , Big data fusion of your data , It can be said that a primary key is related to , It's not that simple .


ORACLE ,MYSQL , SQL SERVER , POSTGRESQL ,MONGODB, Each database has its own primary key recommendation , for example MYSQL strong , strong , strong , It is recommended to use auto increment ID Or orderly ID , Because this is related to his current data storage structure , Of course you can MYSQL On the use of UUID , Or other irregularities , Out of order ID Generator as MYSQL Primary key of , But you violated MYSQL (5.7, Not to mention 8.0), The underlying data storage structure , You must pay the price . Turn over a previous article of mine , It is also developed by a third party MYSQL surface , horrible , Poor performance , Can be on the negative teaching materials “ model ”.


Again , similar ORACLE , POSTGRESQL, You really don't have to MYSQL Think about you like that ID Physical order of , because HEAP Tables are not sequential at all . But that doesn't mean that , Your primary key settings , Can be very willful .

I've seen the primary key set to CHAR(255) The case of , I am powerless to roast , Designed like this , I suggest changing careers , Thinking is too beyond , Oil painting is more suitable for , Impressionism , Very impressive .


It is estimated that someone has asking, How to design the primary key


One 、 Start basic divergent thinking based on the properties of your database ( First of all, you should know that the underlying storage of the database has a preference for your primary key )

Two 、 According to business , After conforming to the principle of database , It's about to be business oriented , If you use a table, it may be distributed in the future , Then your primary key must not be a simple sequential increment of numbers

3、 ... and 、 Whether your primary key is involved in business or not ,( Those who support participation need not say that there are many reasons ), There is also a method that does not support primary key and business binding . The primary key does not participate in business binding , The main reasons may be as follows

1  At the beginning of development , Or business logic instability period , The setting of a primary key , Especially for SQL SERVER and MYSQL , If you don't choose well , It is very difficult to change it later , The wrong choice of other database primary keys does not mean that you can change them at will , Even if the OK, Your business logic can't stand much scrutiny .

2  The primary key is bound to the database type , May restrict your use of primary keys , For example, you would like to MYSQL Irregular primary keys are used as business primary keys in , and MYSQL Primary key design , It is not recommended that you do this , Do you want to change the design method of database or primary key , Actually, you don't have to , You need to understand , Business primary key , And logical primary key , The problems that bother you can be avoided .

3 Later data fusion , Know how to use MYSQL After the database and table are divided , There may be a later step , It's data fusion , For example, your sub Treasury has solved , Business high concurrency insertion , And business simple logic query , In the later stage, you need to do some data summary queries , Then you must be stupid , The original primary key design was unreasonable , Let your data fusion become the same as taking rat medicine , I can't go to heaven , I can't get down , Properly stuck in the middle , That taste is quite "enjoyment".

Four 、 As small as possible , Try not to worry about inserting and querying primary keys .


In fact, the topic of primary keys can continue , For example, the primary key generator , Primary key generation pool , Database and table primary key design , That primary key can be inserted faster at high concurrency .

“ Nonsense ” DATABASE Primary key design _mysql_04

Leave a question at the end

Here is SQL SERVER Of the three ID Automatic generation method , The first is similar to UUID, The second is similar GUID, The third is needless to say , Everyone on earth knows that .The question , which one is better than others ?

createtable tblGUID(RecID uniqueidentifierdefaultnewid(),  SessionID int)

createtable tblSeqGUID(RecID uniqueidentifierdefaultnewsequentialid(),  SessionID int)

createtable tblBigINT(RecID intidentity(1,1),, SessionID int)

Are you smart enough to have an answer ?


“ Nonsense ” DATABASE Primary key design _mysql_05


原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100932022436.html