You need to consider the field data types and lengths during field design. This topic provides recommended data types for fields in several scenarios, to help you standardize field design.
MySQL mode
Numeric fields
We recommend that you use the
BIGINTdata type, rather than integer data types such asINTandSMALLINT, to prevent the value range from being exceeded in the future.Character fields
We recommend that you use the
VARCHAR(N)data type for all dynamic strings.Use
CHAR(1)only for single-character fields. For yes-or-no fields, we recommend that you use theCHAR(1)data type to save space, with1indicating TRUE and0indicating FALSE. The field values must be consistent across all applications. For example, for theis_deletedfield that specifies whether to perform logical deletion,1specifies to perform logical deletion, and0specifies not to.
Notice
NUMBER(1) can also express the yes-or-no concept but takes up more space.
- Do not use
NVARCHARorNCLOBas a column data type.
Notice
A column of the character data type can store all alphanumeric values. However, a column of the
NUMBERdata type can store only numeric values.Datetime fields
For fields requiring time accuracy, you can use the
DATETIME(6)data type.For fields without requiring time accuracy, you can simply use the
DATETIMEdata type.If a field may involve internationalization in the future, we recommend that you use the
TIMESTAMPdata type.We recommend that you do not use characters as the data types of time fields, which may cause implicit type conversion.
Recommendation on data field selection
We recommend that you use the following methods, especially for large tables with millions of rows:
The time fields of all tables in a business must be unified. We recommend that you use the
DATEdata type. For businesses with higher requirements on precision, you can use theTIMESTAMPdata type.We recommend that you use the
NUMBERdata type for numeric data storage to save storage space. Implement conversion at the frontend. Keep value ranges consistent with network segment data in size.You can store IPv4 and IPv6 data in different fields based on service needs by using the
VARCHAR(N)data type.
Oracle mode
Numeric fields
We recommend that you use the
NUMBERdata type for storage. When theNUMBERdata type is used to store fixed-point numbers with a variable length and decimal precision, write the values in the format ofNUMBER(p,s). When theNUMBERdata type is used to store floating-point numbers, write the values in the format ofNUMBER.For decimal fields, we recommend that you use the
DECIMALdata type and do not use theBINARY_FLOATorBINARY_DOUBLEdata type. If you use theBINARY_FLOATandBINARY_DOUBLEdata types, the issue of precision loss may occur in data storage, which may cause incorrect results in value comparison.Priorities in implicit data type conversion
BINARY_DOUBLEhas the highest priority and is followed byBINARY_FLOAT, which precedesNUMBER.Value range of a numeric field
Type Value range Length (in bytes) NUMBER 1.0E-130F to 1.0E+126F (1.0E+126F excluded) 4 to 40 BINARY_FLOAT 1.17549E-38F to 3.40282E+38F 4 BINARY_DOUBLE 2.22507485850720E-308 to 1.79769313486231E+308 8
Character fields
We recommend that you use
VARCHAR2.OceanBase Database compares
VARCHAR2values without padding spaces and comparesCHARvalues with spaces padded.Datetime fields
The
TIMESTAMP WITH TIME ZONEandTIMESTAMP WITH LOCAL TIME ZONEdata types store time zone information. Pay attention to the time zone difference.We recommend that you do not use characters as the data types of time fields, which may cause implicit type conversion.
Others
Auto-increment columns: You must use the BIGINT data type for auto-increment columns. It is prohibited to use the
INTdata type. This avoids storage overflow.It is prohibited to update constraint definitions of table columns through foreign key self-referencing or cascade deletion. This avoids repeated deletion.
Avoid using the
ENUMdata type as far as possible, for example,enum('x','y','z'),. You can use string types instead.We recommend that you update the field comment in time after you modify the definition of a field or append a status value to a field.
When you design fields, maintain some redundancy to improve performance. However, you also need to take into consideration the synchronization of data. A redundancy field cannot be:
A field subject to frequent modification
An excessively long field
During implicit type conversion, numeric types have a lower priority than time types but a higher priority than characters and all other data types.
An appropriate character storage length saves database tablespace and index storage space, and more importantly, improves the retrieval speed.
Unsigned values avoid the storage of negative numbers by mistake and increase the expression range. We recommend that you use different data types for different value ranges. Here are some examples:
Object Age range Type Expression range Humankind Within 150 years old Unsigned TINYINT Unsigned values: 0 to 255 Turtle Hundreds of years old Unsigned SMALLINT Unsigned values: 0 to 65535 Dinosaur fossil Tens of millions of years old Unsigned INT Unsigned values: 0 to 4.3 billion Sun 5 billion years old Unsigned BIGINT Unsigned values: 0 to 1e+19