当前位置:网站首页>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

原网站

版权声明
本文为[Kunlunbase Kunlun database]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222122589704.html