当前位置:网站首页>"Nonsense" database primary key design
"Nonsense" database primary key design
2022-06-10 09:53:00 【51CTO】

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

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 .

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 .

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 ?

边栏推荐
- 【边缘检测】基于matlab八方向sobel图像边缘检测【含Matlab源码 1865期】
- Monthly 50 f cocktails (thinking)
- Only this is the most true reason why leaders promote you. The rest is nonsense!
- Pointnet: deep learning on point sets for 3D classi "location and segmentation
- 小bai月赛49 E 禅(DP)
- Milvus安装
- PostgreSQL cost model
- Local browser access remote server jupyter configuration
- How to send asynchronous requests gracefully in wechat applets?
- pip install . And pip install -e Differences between
猜你喜欢

416. 分割等和子集

有人用这份抖音电商营销玩法拿下offer 月薪2W

5G 聯通網管設計思路

四种最简单的防反接电路

Design of smart home control system (onenet) based on stm32_ two thousand and twenty-two

Flutter 一行Row中显示RadioListTile

利用requests库爬取网页获取数据

领导提拔你的原因,只有这点最真实,其他都是瞎扯!

【黑马早报】蚂蚁集团暂无启动IPO计划;薇娅老公成立新直播公司;微博CEO质问顺丰;京东将试点餐饮外卖业务;腾讯人才管理体系大调整...

Concurrency - create thread
随机推荐
无心剑中译里尔克《秋日》
618来袭,大量优惠券生成太耗时?ThreadPoolTaskExecutor线程池帮你来搞定
Stock issues - 121 The best time to buy and sell stocks
Milvus installation
PostMessage,SendMessage跨不同权限进程通信
Scanpy downloading data is slow or fails to solve the problem
聯調這夜,我把同事打了...
Using requests library to crawl web pages to obtain data
Cf1304c air conditioner
基于STM32设计智能家居控制系统(OneNet)_2022
Lambda表达式例二
mmclassification
如何在微信小程序中优雅地发送异步请求?
On how T-level interactive development shines on our hands
R language plot visualization: plot visualization of stacked histograms and custom setting of the spacing between bars
R语言plotly可视化:plotly可视化箱图、使用quartilemethod参数自定义设置箱图分位数的计算方法(Basic Boxplot)
5G 联通网管设计思路
The new domestic golang framework is ready to be released. Come and have a look
Google Earth Engine(GEE)——GPWv411:平均行政单位面积数据集
Lambda expression