当前位置:网站首页>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
边栏推荐
- OJ daily practice - spanning 2020
- DCC888 :SSA (static single assignment form)
- 【ARM】讯为rk3568开发板lvds屏设置横屏显示
- OJ daily exercise - virus proliferation
- RedisTemplate使用遇到\x00的问题
- Leakcanary source code (2)
- JSBridge
- MySQL8.0轻松完成GTID主从复制
- 【GO】go多态
- LeetCode_ Backtracking_ Dynamic programming_ Medium_ 131. split palindrome string
猜你喜欢

异步FIFO

OJ每日一练——过滤多余的空格

KunlunDB查询优化(三)排序下推

What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?

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

Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!

xml转义字符对照表

Leakcanary source code (2)

07 项目成本管理

OLAP ——Druid简介
随机推荐
为什么大家很少使用外键了?
冒泡排序 指针
OJ每日一练——过滤多余的空格
【GO】Go Modules入门
数据库每日一题---第20天:按日期分组销售产品
弱电转职业网工难不难?华为售前工程师分享亲身经历
DML:Data Manipulation Language 数据操纵语言
Php7.3 error undefined function simplexml_ load_ string()
Use smart doc to automatically generate interface documents
Customize multi-level list styles in word
Uniapp modifies array properties, and the view is not updated
Express、路由(Route)、Request对象、Response对象、中间件、EJS模板
程序员接私活兼职选择
再立云计算“昆仑”,联想混合云Lenovo xCloud凭什么?
Flutter outsourcing, undertaking flutter project
Optimization - linear programming
OJ每日一练——跨越2020
双重跨域:Access-Allow-Origin header contains multiple values“*, *”,but only one is allowed
OJ每日一练——整理命名
OJ每日一练——验证子串