当前位置:网站首页>How to use enum data types
How to use enum data types
2022-06-22 23:49:00 【Kunlunbase Kunlun database】
When doing database design , We often need to add one to many tables ‘ type ’ Field , For example, the gender of people has ‘ male ’, ‘ Woman ’, The types of schools are ‘ The kindergarten ’,‘ Primary school ’,‘ Middle school ’,‘ university ’, The types of cars are ‘ Sedan ’,‘suv’,‘mpv’ etc. , The essential feature of these fields is that they contain “ Finite discrete values ”.
For this field , Use enum The data type is the most appropriate ,mysql and PostgreSQL All have this data type .enum Types have some characteristics :
1、 In the table enum What a field stores is actually its sequential value ( Integers ), Instead of strings .
2、 according to enum Value to find and compare , Find and compare by ordinal values , Not according to enum The string of the item . however mysql There is a problem in this respect ( See screenshot below ): With a enum Items of the same string for range comparison (>, <, >=, <=) When , There is no sequential value comparison , Instead, it compares them in literal terms . however mysql In accordance with enum When sorting columns , But it can also correctly follow enum The order value of the item .
3、postgresql Of enum The type needs to be predefined , And then in create table Use this type when . The advantage of this is that multiple tables can use the same enum type , There is no need to define multiple times to cause inconsistency and other problems .
4、 It can be done to enum Column definition index , Indexes key Sorting is also based on enum Order value sort , And index key It's also enum Sequence value ( Integers ).
The following screenshot is an example using Kunlun distributed database . Inside mysql The example of is the storage node using Kunlun distributed database (mysql8.0.15) It's done .

Create using enum Tables of data types , And then insert the data .

Yes enum Type column to do range search and equivalent search , The order value determines enum Item size relationship .

according to enum When sorting columns of type , Will follow its sequential values instead of literal strings Sort . Query the metadata table to see enum Type metadata .

mysql Of enum usage : You can do equivalent search , However, the range search is not based on sequential values, but on enum Term Literal string To compare , This is actually wrong .

mysql in enum Definition of columns , according to enum term The order of appearance is given to each enum Item assignment Sequence value , from 0 Start .

according to enum When sorting columns , Is in accordance with the enum term Of Sort by order value . From the metadata dictionary, you can see ‘ Rank ’ This enum Type of enum The order value of the item .

After inserting more rows , Sort again , Still according to enum The sequence value of the column, not the literal string To sort .

mysql Yes enum Columns are also sorted by ordinal values , But with the enum String size comparison cannot be intelligently compared according to sequential values .
Give an example of enum After the usage of , Let me give you an example of not using enum Bad design scheme in database design of . These schemes should be abandoned , Never imitate .
1、 Some people use string types directly , such as varchar(N) Column of type , To store such field values , The question is , It is possible that the error of the upper application will input unexpected field values , For example, the school type field in the above example , If the application layer data processing is insufficient , Causes a row to be inserted Rx.type = ‘ Big learn ’, So this line Rx It can also be inserted into the table . So when you look type =‘ university ’ When you go , You can't find it. Rx 了 . Another problem is space utilization . stay mysql and postgresql in ,enum Fields actually store enum The value of the value , It usually saves more space than storing strings , And when searching and comparing , Numeric comparisons are also faster than string comparisons .
2、 Others will be db Use numbers to represent types in the table of , Then complete the conversion between number and type string in the application layer . For example, in the school type above , use 1 representative ‘ The kindergarten ’,2 representative ‘ Primary school ’ etc. , Then complete the conversion between numbers and strings in the application layer , Show the string to the end user , towards db In the corresponding field of . The problem with this is , It increases the workload and maintenance cost of application layer development . Imagine that you need to add more type values later , Also modify the application layer code . and , No application layer code , The meaning of the field value is completely incomprehensible ( Of course , Notes can be added ), Affect the readability of data .
You can see it here enum Several advantages of type :
1、 data verification , Reject illegal values .
2、 Efficient storage and computing .
3、 Strong data readability , There is no need to rely on application code to explain .
4、 There is no need for the application to do any numerical interpretation and conversion , Reduce the cost of application software development and maintenance .
therefore , It is strongly recommended that database design and application system design , Use... When appropriate enum type .
KunlunDB The project is open source
【GitHub:】
https://github.com/zettadb
【Gitee:】
https://gitee.com/zettadb
END
边栏推荐
猜你喜欢
![[STM32 skill] use the hardware I2C of STM32 Hal library to drive rx8025t real-time clock chip](/img/32/88321db57afb50ccc096d687ff9c41.png)
[STM32 skill] use the hardware I2C of STM32 Hal library to drive rx8025t real-time clock chip

使用GetX构建更优雅的Flutter页面结构

Safe and reliable! Tianyi cloud data security management platform passed the evaluation

【STM32技巧】使用STM32 HAL库的硬件I2C驱动RX8025T实时时钟芯片
![[go] go array and slice (dynamic array)](/img/63/9a3fb70b202ca45828cd1b62897eec.jpg)
[go] go array and slice (dynamic array)

Customize multi-level list styles in word

Canvas generate Poster

【GO】Go数组和切片(动态数组)

To establish a cloud computing "Kunlun", why should Lenovo hybrid cloud Lenovo xcloud?

MySQL8.0轻松完成GTID主从复制
随机推荐
冒泡排序 指针
IPV4的未来替代品!一文读懂IPV6的优势特点和地址类型
tp5.1上传excel文件并读取其内容
Web Caching Technology
OJ每日一练——验证子串
Redistemplate encountered problems with \x00
[go] getting started with go modules
Enterprise digitalization is not a separate development, but a comprehensive SaaS promotion
瑞达期货安全么?期货开户都是哪些流程?期货手续费怎么降低?
1. class inheritance (point)
Oracle ASM uses the CP command in asmcmd to perform remote replication
Canvas generate Poster
[go] go language interface
【GO】go语言interface
语义分割新范式!StructToken:对per-pixel 分类范式的重新思考
XML escape character cross reference table
Redis缓存
Ecmascript6 new features
tp5.1解决跨域
KunlunDB备份和恢复