Preamble
MySQL data types – Be sure to include the data type when choosing the columns for the table. Each column must have a specific data type. What values can be stored in a column and how much memory they will take up depend on the data type.
The following data types are available through MySQL and can be divided into different categories.
The various data types that MySQL can handle are listed below, including string, numeric, date/time, and large object.
String data types
The MySQL String data types are listed below:
Syntax | Maximum size | Explanation |
CHAR(size) | Size 255 characters | where size is the total number of characters saved. fixed-length strings. The additional characters in the area to the right are equal in number. |
VARCHAR(size) | Size 255 characters | where size is the overall character count saved. variable length string |
TINYTEXT(size) | Size 255 characters | where size is the overall character count saved. |
TEXT(size) | Size 65535 characters | where size is the overall character count saved. |
MEDIUMTEXT(size) | Size 16777215 characters | size is the number of characters that have been saved. |
LONGTEXT(size) | Size 4Gb or 4294967295 characters | where size is the overall character count saved. |
BINARY(size) | Size 255 characters | where size denotes how many binary characters should be stored. strings of a set length. A similar number of characters are added to the right-hand space. (Supported by MySQL 4.1.2.) |
VARBINARY(size) | Size 255 characters | Size refers to the total number of characters saved. length-variable string (Shown in MySQL 4.1.2.) |
Starting with MySQL 5.6, the CHAR and VARCHAR data types use UTF-8 encoding by default. This lets you store up to 3 bytes per character, depending on the language (for example, 1 byte per character for many European languages, 2 bytes per character for some Eastern European and Middle Eastern languages, and 3 bytes per character for Chinese, Japanese, and Korean).
Numeric data types
The numeric data types in MySQL are listed below:
Syntax | Maximum size | Explanation |
BIT | very small integer value, equivalent to TINYINT (1). Numerical expression from -128 to 127 Positive number values range from 0 to 255. | |
TINYINT(m) | It’s a very small integer value. Numerical expression from -128 to 127 Positive number values range from 0 to 255. | |
SMALLINT(m) | small integer value. Numerical expression from -32768 to 32767 Positive numbers begin with 0 and go up to 65535. | |
MEDIUMINT(m) | average integer value. numeric expression from -8388608 to 8388607. Positive numbers begin with 0 and go up to 16777215. | |
INT(m) | standard integer value. Numerical expression from -2147483648 to 2147483647 Positive numbers begin with 0 and go up to 4294967295. | |
INTEGER(m) | standard integer value. The signed values range from -2147483648 to 2147483647. Positive numbers begin with 0 and go up to 4294967295. | This is synonymous with the INT data type. |
BIGINT(m) | a large integer value. The signed values range from -9223372036854775808 to 9223372036854775807. Positive values of numbers range from 0 to 18446744073709551615. | |
DECIMAL(m,d) | a number with a fixed point. m by default—10 if not specified. d is 0 by default if not specified. | where m is the total number of digits and d is the number of digits after the decimal place. |
DEC(m,d) | a number with a fixed point. m by default—10 if not specified. d is 0 by default if not specified. | where m is the total number of digits and d is the number of digits after the decimal place. This is synonymous with the decimal type. |
NUMERIC(m,d) | a number with a fixed point. m by default—10 if not specified. d is 0 by default if not specified. | where m is the total number of digits and d is the number of digits after the decimal place. This is synonymous with the decimal type. |
FIXED(m,d) | a number with a fixed point. m by default—10 if not specified. d is 0 by default if not specified. | where m is the total number of digits and d is the number of digits after the decimal place. (represented in MySQL 4.1) This is synonymous with the decimal type. |
FLOAT(m,d) | a floating-point number with single precision. | where m is the total number of digits and d is the number of digits after the decimal place. |
DOUBLE(m,d) | A floating-point number with double precision | where m is the total number of digits and d is the number of digits after the decimal place. This is synonymous with the double data type. |
DOUBLE PRECISION(m,d) | A floating-point number with double precision | where m is the total number of digits and d is the number of digits after the decimal place. |
REAL(m,d) | A floating-point number with double precision | where m is the total number of digits and d is the number of digits after the decimal place. This is synonymous with the double data type. |
FLOAT(p) | a floating-point number. | where p is precision. |
BOOL | TINYINT synonyms (1) | considered a boolean data type, where the value 0 is considered FALSE and any other value is considered TRUE. |
BOOLEAN | TINYINT synonyms (1) | considered a boolean data type, where the value 0 is considered FALSE and any other value is considered TRUE. |
Date/Time data types
Below are the date / time data types in MySQL:
Syntax | Maximum size | Conclusion value |
DATE | The values range from ‘1000-01-01’ to ‘9999-12-31’. | ‘YYYY-MM-DD’. |
DATETIME | The values range from ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’. | ‘YYYY-MM-DD HH:MM:SS’. |
TIMESTAMP(m) | The values range from ‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ UTC. | ‘YYYY-MM-DD HH:MM:SS’. |
TIME | The values range from ‘-838: 59: 59’ to ‘838: 59’. | ‘HH:MM:SS’. |
YEAR[(2|4)] | The value of the year as 2 or 4 digits. | The default value is 4 digits. |
Large Object (LOB) data types
The following is a list of large object (LOB) data types in MySQL:
Syntax | Maximum size | Explanation |
TINYBLOB | Maximum size 255 bytes. | |
BLOB(size) | Maximum size 65 535 bytes. | Where size – number of stored characters (size is optional and was entered in MySQL 4.1) |
MEDIUMBLOB | Maximum size 16,777,215 bytes. | |
LONGTEXT | Maximum size 4 GB or 4 294 967 295 characters. |
What to use when choosing a data type of MySQL
Choose the general data class—such as numeric, string, or temporary—that is most appropriate for the column before choosing the data type for the column;
Next, you need to select a specific data type from those presented in the class. The same type of data can be stored using a number of different MySQL data types with different accuracy levels, value ranges, and physical space requirements (on disk or in memory). Some types display distinctive traits or behaviors.
The DATETIME and TIMESTAMP columns, for instance, can both store date and time information to the nearest second. On the other hand, the TIMESTAMP type takes up half as much space, allows for the use of time zones, and has special mechanisms for automatic updating. On the other hand, it has a much smaller acceptable range.
What to consider when selecting the data type:
- In general, smaller data types are faster because they occupy less CPU cache, memory, and disk space. Try to use data types with a minimum size sufficient for their correct storage and presentation.
- Since different encodings and sorting rules make character comparison more difficult, it makes sense to use integer data types for IP addresses. It is also preferable to store date and time values in built-in MySQL data types rather than in strings.
- The optimization of queries containing NULL columns causes additional difficulties for MySQL as they complicate the indexes, index statistics, and comparison of values. A column that allows NULL, takes more disk space, and requires special processing within MySQL. If there is a need to display in the table the fact that the value is absent, you can avoid using NULL by using other values, such as 0, a special value, or an empty row.
- It makes sense to use the same types of data in linked columns. Using different types of data in linked columns may slow down the processing of your request.
About Enteros
Enteros offers a patented database performance management SaaS platform. It finds the root causes of complex database scalability and performance problems that affect business across a growing number of cloud, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Enhancing Identity and Access Management in Healthcare with Enteros
- 19 November 2024
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Maximizing Efficiency with Enteros: Revolutionizing Cost Allocation Through a Cloud Center of Excellence
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Driving Efficiency in the Transportation Sector: Enteros’ Cloud FinOps and Database Optimization Solutions
- 18 November 2024
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Empowering Nonprofits with Enteros: Optimizing Cloud Resources Through AIOps Platform
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…