当前位置:网站首页>Mysql database design
Mysql database design
2022-06-22 22:41:00 【xcrj】
theory
E-R (entity-relationship): Entity 、 attribute 、 relation (1:1, 1:n, m:n)
CDM (Conceptual Data Model): Conceptual data model
PDM (Physical Data Model): Physical data model
1NF normal form :
- Field atomicity , Entity attributes cannot be further divided
2NF normal form :
- Part of it depends on , Each non candidate key can be directly distinguished by candidate keys
- stay 1NF On the basis of normal form, the partial dependence of non primary attribute on primary code is eliminated
3NF normal form :
- Transitive dependency , Any non primary attribute does not depend on other non primary attributes
- stay 2NF On the basis of eliminating - Transitive dependency
summary
Do you want to use a database , With what kind of database , How to use database
Monomers , Distributed , data type , Data volume , Concurrency , efficiency , Security
A database is just a way to store data , There is also a certain use cost ; In some cases, you can change the data storage method , such as JSON file 、Excel Forms even txt file
json Storage and proto Storage can be cross language ,json The storage database type is text ,proto Format storage is binary
Conform to the paradigm , Add redundancy ( Speed up data processing )
Suo Yin
Add view ( Increase speed , After the table is split union View )
Security : Master slave copy 、mysql colony
efficiency : Read / write separation 、 Sub database and sub table 、mysql colony
Be careful
no need mysql Keyword as field name , The name of the table
No foreign keys , Use code to ensure consistency between tables
database
database : Storage engine 、 code 、 Sort rule
Storage engine :MyISAM And InnoDB
| name | lock | Business | Indexes |
|---|---|---|---|
| InnoDB | Row lock | Support | There is no limit to |
| MyISAM | Table locking | I won't support it | most 64 An index , The total number of field bytes in the index must not be greater than 1000 bytes |
| Merge | Premise :MYISAM engine && The table structure is consistent , Single table unique indexes are supported |
code :
- utf-8mb4 (most bytes 4bytes): Use of English 1 Bytes , Use of Chinese 3 Two bytes to encode ; Special character expression , Wide characters 4 byte ; Emoticons can be stored
- utf-8: English and symbols account for 1 Bytes , Chinese and symbols account for 3 Bytes , The number is one byte
- unicode: chinese 、 All in English 2 Bytes
- utf-16: In general, both English and Chinese need to occupy 2 Bytes ;unicode The Chinese characters in the extension area occupy 4 Bytes
- utf-32: All characters 4 Bytes
Sort rule :
| Sort rule | advantage | shortcoming | other |
|---|---|---|---|
| utf8_general_ci | More efficient | Poor correctness | Can accept ß = s( single s) instead of ss( double ss) Then use ;ß = s |
| utf8_unicode_ci | Low efficiency | High accuracy | German and French ,utf8_unicode_ci It worked well ;ß = ss |
surface
What data can be put into the table
classification : Basic table 、 Extended table 、 Association table
name
Refer to the website
The data volume of a single table shall be controlled within 500 Within ten thousand lines
- 500 Wanxing is not mysql Database limitations , Modify table structure , Backup , Recovery limits
- Historical data archiving , Solve the problem that the log data is too large
- Sub database and sub table , Solution table is too large
Use caution mysql Partition table , Choose the partition key carefully ??
- Logically, it is represented as a table , Physically, it is represented as multiple files
- It is suggested to use physical sub-table to manage big data
Do not use mysql Large binary files are stored in the database
- Large files often need to be random IO Operation is too time consuming
- mysql Store address in URL that will do
- picture 、 Files are stored on a dedicated file server
Do not do database stress tests online
Relationship
- one-on-one : Either party puts the primary key of the other party
- One to many : Put the primary key of the one party in the multiple party
- Many to many : To create a separate table, you need to create an associated table
surface / name
Prefixes are used to distinguish between system tables and business tables
The suffix represents the classification of the table
Field
Field
- classification : General fields 、 Business fields
- Field type and size 、 The default values are null and index
- Field name , type , length 、 Width 、 With or without symbols , The default value is , Allow null , Indexes
- Primary key 、 Foreign keys
- Redundant fields
Design
- General fields 、 Business fields are separated
- Use programs instead of foreign keys
- The space is right :
- The type is precise , multi-purpose int、char;
- The length is accurate , Fixed length ;
- With or without symbols unsigned
- Avoid using text,blob
- The larger the field type , Indexing takes up more space , The fewer inodes a page box can store , When traversing the disk IO The more
- The inode in the disk will load the page box in memory
- If possible, define all columns as not null
- The generation of a clustered index requires that it be unique and non empty
Field / data type
Refer to the website
data type
| Type name | Storage size | Number of bytes | Introduce |
|---|---|---|---|
| bit | Storage bit value ,0 or 1, The longest 64 position | ||
| char | Width :0~85 | 0-255 bytes | Fixed length characters , The display becomes longer , Space occupation fixed length ;255/3=85 The Chinese characters |
| varchar | Width :0~5000~2 ten thousand | 0-65535 bytes | Variable length characters , The display becomes longer , The space occupation becomes longer ;65535/3=21845 The Chinese characters ; The Ali display is the widest 5000 |
| binary | contain 0 or 1 character | ||
| varbinary | contain 0 or 1 character | ||
| bigint | 8 byte | Unsigned value :0 To about 10 Of 19 Power | Often used as primary key |
| tinyint | 1 byte | Unsigned value :0 To 255 | Often used as a dictionary type |
| int | 4 byte | Unsigned value :0 To about 43 Billion | |
| smallint | 2 byte | Unsigned value :0 To 65535 | |
| mediumint | 3 byte | Unsigned :0 To 16777215(1 thousand 600 ten thousand ) | |
| double | 8bytes | (2.22…E-308,1.79… E+308) | Double precision , Floating point numbers |
| decimal(a,b) | 16bytes | a Maximum 38,b stay 0~a Between | decimal(10, 7): The total length is 10, Decimal length 7 |
| float | 4bytes | (1.17…E-38,3.40…E+38) | Single precision , Floating point numbers |
| tinytext | 255bytes | 0-65 535 bytes | 255/3=85 The Chinese characters |
| text | 65535/1024=64K | 0-65535 bytes | 65535/3=21845 The Chinese characters , about 2 Ten thousand Chinese characters |
| mediumtext | 16777215/1024/1024=16M | 0-16 777 215 bytes | 16777215/3=5592405 The Chinese characters ,559 Ten thousand Chinese characters |
| longtext | 4294967295/1024/1024/1024=4G | 0-4 294 967 295 bytes | 4294967295/3=1431655765 The Chinese characters ,14 Hundred million Chinese characters |
| TINYBLOB | 255bytes | 0-255 bytes | No more than 255 Binary string of characters |
| BLOB | 63.9KB | 0-65 535 bytes | Long text data in binary form |
| MEDIUMBLOB | 15.9MB | 0-16 777 215 bytes | Medium length text data in binary form |
| LONGBLOB | 3.9GB | 0-4 294 967 295 bytes | Maximum text data in binary form |
Be careful :
- mysql varchar(3) The number in describes the number of characters, not bytes :, No matter in Chinese english Numbers All are 3 individual .
- 4.0 Version below ,varchar(100), refer to 100 byte , If stored UTF8 Chinese character time , Can only save 33 individual ( Every Chinese character 3 byte )
- 5.0 Above version ,varchar(100), refer to 100 character , It doesn't matter what the numbers are 、 The letters are still UTF8 Chinese characters ( Every Chinese character 3 byte ), All can be stored 100 individual .
- BINARY and VARBINARY Be similar to CHAR and VARCHAR, The first two contain binary strings, that is, byte strings instead of character strings
- Integer type as long as the type is set , The storage size is determined by the type, not by the display width
- navicat The number in parentheses after the integer type in , Indicates the display width , Does not indicate storage length , With this varchar、char The following numbers have different meanings .
- Longitude and latitude :decimal(10, 7) It only needs to be accurate to the decimal point 7 position , The precision is 1CM, therefore , The longitude and latitude stored in the database are decimal(10, 7) that will do .
- money :decimal(15, 2) One trillion . Corner score ; Or go directly to the integer of the fraction ,
- You can store decimals and integers separately
- varchar: Maximum display width 5000
- bigint(20) Auto increment as primary key
- tinyint(0~255) Make a dictionary
- text:2 Ten thousand Chinese characters
Date time :
| Type name | Range | Format | Introduce | byte |
|---|---|---|---|---|
| datetime | 1000-01-01 00:00:00/9999-12-31 23:59:59 | YYYY-MM-DD HH:MM:SS | Ali rules datatime; Whether to update according to the current time ; by null Stored as null | 8 byte |
| timestamp | 1970-01-01 00:00:00/2038 | YYYYMMDD HHMMSS | Time stamp ;timestamp Will store the time zone ; by null Hour is the current time | 4 byte |
| time | ‘-838:59:59’/‘838:59:59’ | HH:MM:SS | ||
| year | 1901/2155 | YYYY | ||
| date | 1000-01-01/9999-12-31 | YYYY-MM-DD |
Be careful :
- Time default :CURRENT_TIMESTAMP
- create_time Use datetime, Use only when creating
- update_time Use datetime, This field needs to be updated when updating
Field / Public fields
part
id(bigint(20), Primary key , Self increasing , Unsigned )
create_time(datetime) : Create with 1 Time
update_time(datetime) : Update according to the current time , The default value is CURRENT_TIMESTAMP
tenant_id(bigint(20), Unsigned ): Tenant ID
All
id(bigint(20), Primary key , Self increasing , Unsigned )
create_time(datetime) : Create with 1 Time
update_time(datetime) : Update according to the current time , The default value is CURRENT_TIMESTAMP
delete_time(datetime): Use when deleting 1 Time
create_by(char(30)): The creator
update_by(char(30)): Update the
delete_by(char(30)): Deleted by
tenant_id(bigint(20), Unsigned ): Tenant ID
data_scope: Under the same tenant , The range of data
is_enabled(tiny_int(1), Unsigned ): notes : Is it enabled? 1:true Enable 0:flase Discontinue use Default :1
is_deleted(tiny_int(1), Unsigned ): notes : Whether or not to delete 1:true Delete 0:false Not delete Default :0
Field / The default value is
Avoid the business implications of field defaults
Directly set the default value of the field to the common value
Field / name
Primary key
Environmental classification : Monomers 、 Distributed
generating algorithm :UUID、 Snowflake algorithm
Generation method :mysql Self increasing 、java Code generation 、ORM Frame generation
innoDB Database engine , Use int Auto increment as primary key
| classification | explain |
|---|---|
| mysql Since the primary key | innoDB |
| mysql Bring their own uuid | |
| ORM Framework auto insert snowflake algorithm | |
| java Generate uuid、 Snowflake algorithm |
- Design experience :
- Use auto increment primary key ; It is not convenient to obtain the data generated by the database id, The frame returns the inserted data object
- Snowflake algorithm :bigint(20)
- uuid:varchar(36)
Foreign keys
The primary key of the primary table serves as the foreign key of the secondary table
Constraints on deletion : When deleting master table records , From the actions in the table
- CASCADE( cascade ): When deleting the primary key table , Delete from the table
- RESTRICT/NO ACTION: Constrain master table from table - Records in the main table are sometimes in foreign keys , Not delete
- SET NULL: Constrain master table from table - Records in the main table are sometimes in foreign keys , Set the value of the foreign key from the table to null
Indexes
- Index building , Build a good index
- Union index left , Highly differentiated ( Priority index left ), The most frequently queried , The field length is small (1 Pages can store more inodes )
- varchar Field indexing , The length must be set , length =20 Time zone division =90%, count(distinct left( Name , Index length ))/count(*)
- Index usage , Try to use indexes for queries
- Single table index is recommended not to exceed 5 individual
- It is forbidden to create a separate index for each column in a table
- Each table must have a primary key
- General development and use Ordinary index and unique index are OK
- Set a unique index for the mobile number ; Set the ID card as a unique index
- The table corresponding to the data dictionary sys_dict Build more indexes
- Relational query join Field indexing
Index principle
- B+ Trees
- establish B+ The tree process is time-consuming
- Build a good B+ Trees , Help to improve query speed
The index name :
- primary key :pk_ Field name
- unique index :uk_ Field name
- General index :idx_ Field name
- Composite index :idx_ Field name 1_ Field name 2
Business
Sub database and sub table
Time to divide the warehouse and table :523 principle , The number of single table rows exceeds 500 The capacity of ten thousand rows or single table exceeds 2GB; It is estimated that the data volume in three years will not reach this level at all , Please do not divide the database and tables when you create the tables .
sub-treasury : Requests are published to different libraries for processing , The pressure of the same requisition table has not decreased .
table : The quantity is appropriate
table / Horizontal split
Method 1 And methods 3 Combine the methods to divide the tables
- It is estimated that there will be a large amount of data and frequently accessed tables in advance , Divide it into several tables : Mapping method surface id-(hash Algorithm 、 Remainder algorithm )- Sub table name
- I didn't think of what to do in advance
- Changing the algorithm destroys the correspondence
- merge Storage engine :TYPE=MERGE UNION=(user1,user2) INSERT_METHOD=LAST
- First, there are several storage engines MYISAM Table of + Their table structure is consistent
- One merge Tables cannot be maintained on the entire table unique Indexes , Can only be maintained on a single table unique Indexes . When you execute a insert, The data enters the sub table ( Depending on insert_method The value of the option )
table / Split Vertically
Split by business , Split the business into more details
Example
Transitive dependency :employee Table has depart_code,depart_name;depart Table has depart_code,depart_name; If depart In the table depart_name Changed the ,employee In the table depart_name What do I do
object database :mysql Is a structured database , Binary file entities such as images and files are put into the object database ,mysql Just put the link address in
Primary key :mybatis-plus The snowflake algorithm defaults to int type 19 position , So when designing a database bigint(20)
Field : Required fields for tree directory ,id parent
Example / The data dictionary
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for sys_dict_type
-- ----------------------------
DROP TABLE IF EXISTS `sys_dict_type`;
CREATE TABLE `sys_dict_type` (
`dict_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' Dictionary key ',
`dict_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Dictionary name ',
`dict_type` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Dictionary type ',
`status` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT ' state (0 normal 1 Discontinue use )',
`create_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' The creator ',
`create_time` datetime NULL DEFAULT NULL COMMENT ' Creation time ',
`update_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Update the ',
`update_time` datetime NULL DEFAULT NULL COMMENT ' Update time ',
`remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ' remarks ',
PRIMARY KEY (`dict_id`) USING BTREE,
UNIQUE INDEX `dict_type`(`dict_type`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = ' Dictionary type table ' ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Table structure for sys_dict_data
-- ----------------------------
DROP TABLE IF EXISTS `sys_dict_data`;
CREATE TABLE `sys_dict_data` (
`dict_code` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' Dictionary code ',
`dict_type` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Dictionary type ',
`dict_label` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Dictionary labels ',
`dict_value` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Dictionary key value ',
`is_default` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT 'N' COMMENT ' Default (Y yes N no )',
`dict_sort` int(11) NULL DEFAULT 0 COMMENT ' Dictionary sort ',
`status` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT ' state (0 normal 1 Discontinue use )',
`create_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' The creator ',
`create_time` datetime NULL DEFAULT NULL COMMENT ' Creation time ',
`update_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Update the ',
`update_time` datetime NULL DEFAULT NULL COMMENT ' Update time ',
`remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ' remarks ',
PRIMARY KEY (`dict_code`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 40 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = ' Dictionary data table ' ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;
Be careful
- dict_type Add a unique prefix , A dictionary that distinguishes between different businesses
Reference documents
《 Alibaba Taishan version Java Development Manual 》
边栏推荐
- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
- MySQL multi table operation exercise
- R language builds a binary classification model based on H2O package: using H2O GLM constructs regularized logistic regression model and uses H2O AUC value of AUC calculation model
- Makefile:1860: recipe for target ‘cmake_check_build_system‘ failed make: *** [cmake_check_build_syst
- Redis error reporting and common configurations
- Es total number of data queried by criteria
- Core and semiconductor "RF eda/ filter design platform" shines ims2022
- Fundamentals of shell programming (Part 7: branch statement -if)
- 【李沐】 如何读论文【论文精读】
- Lua -- iterator, module, meta table
猜你喜欢

Liunx installing MySQL
![[Li mu] how to read papers [intensive reading of papers]](/img/86/4894bdef31d47d3f9bf3206b997eed.jpg)
[Li mu] how to read papers [intensive reading of papers]

There are 15 necessary knowledge points for the second level cost engineer before the exam! I wish you success!

Developing salary management system based on C language course paper + source code and executable EXE file
![[GWCTF 2019]mypassword XSS](/img/26/3611fd5aae21ea004dcfcc2c623328.png)
[GWCTF 2019]mypassword XSS

In the third week of June, the main growth ranking list (BiliBili platform) of station B single feigua data up was released!
![[geometric vision] 4.2 piecewise linear transformation](/img/1e/a810f4d7e9a6a34647b5cb56fdde67.png)
[geometric vision] 4.2 piecewise linear transformation

自助圖書館系統-Tkinter界面和openpyxl錶格綜合設計案例

What are the indicators, dimensions and models in Business Intelligence BI data warehouse?
![组合总数[标准回溯 + 回溯技巧--降低栈深度]](/img/88/3a07589bf8edab618139b1bf1680e8.png)
组合总数[标准回溯 + 回溯技巧--降低栈深度]
随机推荐
Install MySQL for Linux (package succeeded!!)
A case of 94 SQL optimization (the writing method used is often rejected)
Cvpr2022 𞓜 feature decoupling learning and dynamic fusion for re captured images
AtCoder abc256全题解(区间合并模板、矩阵快速幂优化dp、线段树……)
Eureka service registration and discovery
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields
Es total number of data queried by criteria
【ROS】ROSmsg cakin_make编译错误
Do not know how to choose the development of digital currency wallet?
MySQL functions
Redis error reporting and common configurations
Several methods of changing 91 Oracle common table into partitioned table
Las point cloud data thinning in ArcGIS
Experiment 4 operation comparison between NoSQL and relational database
What if the SQL execution plan of the production system suddenly becomes worse?
How to use the data dictionary function in the low code platform of the Internet of things?
NFT can only be viewed from afar, but not blatantly played
. Net 5.0 realizes the source code analysis of the oidc authentication part of single sign on through identityserver4
Total number of combinations [standard backtracking + backtracking techniques -- reducing stack depth]
【李沐】 如何读论文【论文精读】