What are the data types of mysql?
Mysql data types include: BOOL, TINY INT, INT, BIG INT, FLOAT, DOUBLE, DECIMAL, CHAR, VARCHAR, TINY TEXT, TEXT, Date, DateTime, TimeStamp, Year, etc.
1. MySQL data types
Mainly include the following five categories:
Integer types: BIT, BOOL, TINY INT, SMALL INT, MEDIUM INT, INT, BIG INT
Floating point type: FLOAT, DOUBLE, DECIMAL
String type: CHAR, VARCHAR, TINY TEXT, TEXT, MEDIUM TEXT, LONGTEXT , TINY BLOB, BLOB, MEDIUM BLOB, LONG BLOB
Date type: Date, DateTime, TimeStamp, Time, Year
Other data types: BINARY, VARBINARY, ENUM, SET, Geometry, Point , MultiPoint, LineString, MultiLineString, Polygon, GeometryCollection, etc.
1. Integer type
MySQL data type | Meaning (signed) |
tinyint(m) | 1 byte range (-128~127) |
smallint(m) | 2 bytes range (-32768~32767) |
mediumint(m) | 3 bytes range ( -8388608~8388607) |
int(m) | 4 bytes range (-2147483648~2147483647) |
bigint(m) | 8 bytes range (-9.22*10 to the 18th power) |
Value range If unsigned is added, the maximum The value is doubled. For example, the value range of tinyint unsigned is (0~256).
The m in int(m) represents the display width in the SELECT query result set. It does not affect the actual value range or the display width. I don’t know what the use of this m is.
2. Floating point type (float and double)
Meaning | |
Single precision floating point type 8-bit precision (4 bytes) m total number, d decimal places | |
Double precision floating point type 16-bit precision (8 bytes) m total number, d decimal places |
3, fixed point number
Floating point Types store approximate values in the database, while fixed-point types store exact values in the database. decimal(m,d) The parameter m<65 is the total number, d<30 and d4. String (char, varchar, _text)
Meaning | ||||||||||||||||||||||||||
Fixed length, up to 255 characters | ||||||||||||||||||||||||||
Fixed Length, up to 65535 characters | ||||||||||||||||||||||||||
Variable length, up to 255 characters | ||||||||||||||||||||||||||
Variable length, up to 65535 characters | ||||||||||||||||||||||||||
Variable length, up to 2 to the 24th power - 1 character | ||||||||||||||||||||||||||
Variable length, up to 2 to the 32nd power - 1 character |
MySQL data type | Meaning |
date | Date'2008-12-2' |
time | Time'12:25:36' |
datetime | Date time '2008-12-2 22:06:44' |
timestamp | Automatic storage Record modification time |
#If you define a field as timestamp, the time data in this field will be automatically refreshed when other fields are modified, so fields of this data type can store this The time when the record was last modified.
Data type attributes
MySQL keywords | Meaning | |||||||||||||||||||||||||
NULL | Data columns can contain NULL values | |||||||||||||||||||||||||
NOT NULL | Data columns are not allowed to contain NULL values | |||||||||||||||||||||||||
Default value | ||||||||||||||||||||||||||
Primary key | ||||||||||||||||||||||||||
Auto increment , suitable for integer types | ||||||||||||||||||||||||||
Unsigned | ||||||||||||||||||||||||||
Specifies a character set |
Data type | Byte length | Range or usage | ||||||||||||||||||||||||
Bit | 1 | Unsigned [0,255], signed [-128,127] , Tianyuan Blog Note: BIT and BOOL Boolean types both occupy 1 byte | ||||||||||||||||||||||||
TinyInt | 1 | Integer [0,255] | ||||||||||||||||||||||||
SmallInt | 2 | Unsigned [0,65535], signed [-32768,32767] | ||||||||||||||||||||||||
MediumInt | 3 | Unsigned[0,2^24-1], signed[-2^23,2^23-1]] | ||||||||||||||||||||||||
Int | 4 | Unsigned [0,2^32-1], signed [-2^31,2^31-1] | ||||||||||||||||||||||||
BigInt | 8 | Unsigned [0,2^64-1], signed [-2^63 ,2^63 -1] | ||||||||||||||||||||||||
Float(M,D) | 4 | Single precision floating point number. Tianyuan Blog reminds that D here is precision. If D<=24, it is the default FLOAT. If D>24, it will be automatically converted to DOUBLE type. | ||||||||||||||||||||||||
Double(M,D) | 8 | Double precision floating point. | ||||||||||||||||||||||||
Decimal(M,D) | M 1 or M 2 | Unpacked floating point number, usage is similar to FLOAT and DOUBLE, Tianyuan The blog reminds you that if the Decimal data type is used in ASP, the Decimal read directly from the database may need to be converted into a Float or Double type before operation. | ||||||||||||||||||||||||
Date | 3 | is displayed in the format of YYYY-MM-DD, for example: 2009-07-19 | ||||||||||||||||||||||||
Date Time | 8 | is displayed in the format of YYYY-MM-DD HH:MM:SS, for example: 2009-07-19 11:22:30 | ||||||||||||||||||||||||
TimeStamp | 4 | is displayed in the format of YYYY-MM-DD, for example: 2009-07-19 | ||||||||||||||||||||||||
Time | 3 | is displayed in the format of HH:MM:SS. For example: 11:22:30 | ||||||||||||||||||||||||
1 | is displayed in the format of YYYY. For example: 2009 | |||||||||||||||||||||||||
M | fixed-length string. | |||||||||||||||||||||||||
M | Variable length string, requires M<=255 | |||||||||||||||||||||||||
M | Char-like binary storage, characterized by inserting fixed-length shortfalls and padding 0 | |||||||||||||||||||||||||
M | Variable length binary storage similar to VarChar, characterized by fixed length without padding 0 | |||||||||||||||||||||||||
Max :255 | Case insensitive | |||||||||||||||||||||||||
Max:64K | Case insensitive | |||||||||||||||||||||||||
Max:16M | Case insensitive | |||||||||||||||||||||||||
Max:4G | Case insensitive | |||||||||||||||||||||||||
Max:255 | Case sensitive | |||||||||||||||||||||||||
Max:64K | Case sensitive | |||||||||||||||||||||||||
Max:16M | Case Sensitive | |||||||||||||||||||||||||
Max:4G | Case Sensitive | |||||||||||||||||||||||||
1 or 2 | Up to 65535 different enumeration values | |||||||||||||||||||||||||
Up to 8 | Up to 64 different values | |||||||||||||||||||||||||
##LineString | ||||||||||||||||||||||||||
##Polygon | ||||||||||||||||||||||||||
##MultiPoint | ||||||||||||||||||||||||||
MultiLineString | ||||||||||||||||||||||||||
##MultiPolygon | ||||||||||||||||||||||||||
GeometryCollection | ||||||||||||||||||||||||||
3. Usage Suggestions1. When specifying the data type, the principle of small size is generally adopted. For example, if you can use TINY INT, it is best not to use INT, and if you can use FLOAT type, it is best not to use DOUBLE type, so It will greatly improve the operating efficiency of MYSQL, especially under large data volume testing conditions. 2. There is no need to design the data table too complicated. The distinction between functional modules may be more convenient for later maintenance. Be careful when presenting a hodgepodge of data tables. 3. The classification of data tables and fields Naming is also a skill 4. Before designing the data table structure, please imagine it is your room. Maybe the result will be more reasonable and efficient 5. The final design result of the database must be It is a trade-off between efficiency and scalability. It is inappropriate to favor either side. Basic principles for selecting data typesPrerequisite: Use a suitable storage engine. Selection principle: Determine how to choose the appropriate data type based on the selected storage engine. The following selection methods are classified by storage engine:
For InnoDB data tables, the internal row storage format does not distinguish between fixed-length and variable-length columns (all data rows use head pointers pointing to data column values), so in essence , using fixed-length CHAR columns is not necessarily simpler than using variable-length VARCHAR columns. Therefore, the main performance factor is the total storage used by the data rows. Since CHAR takes up more space on average than VARCHAR, it is better to use VARCHAR to minimize the total storage and disk I/O of data rows that need to be processed. Let’s talk about fixed-length data columns and variable-length data columns. char is similar to varcharThe CHAR and VARCHAR types are similar, but they are saved and retrieved differently. They also differ in terms of their maximum length and whether trailing spaces are preserved. No case conversion is performed during storage or retrieval. The following table shows the results of saving various string values into CHAR(4) and VARCHAR(4) columns, illustrating the difference between CHAR and VARCHAR:
Please note that the value in the last row in the above table only applies when strict mode is not used ; if MySQL is running in strict mode, the value that exceeds the column length will not be saved , and an error will occur. The values retrieved from CHAR(4) and VARCHAR(4) columns are not always the same because trailing spaces are removed from the CHAR column when retrieving. The following examples illustrate this difference:mysql> CREATE TABLE vc (v VARCHAR(4), c CHAR(4)); Query OK, 0 rows affected (0.02 sec) mysql> INSERT INTO vc VALUES ('ab ', 'ab '); Query OK, 1 row affected (0.00 sec) mysql> SELECT CONCAT(v, '+'), CONCAT(c, '+') FROM vc; +----------------+----------------+ | CONCAT(v, '+') | CONCAT(c, '+') | +----------------+----------------+ | ab + | ab+ | +----------------+----------------+ 1 row in set (0.00 sec) Copy after login
mysql> CREATE TABLE test (c1 float(10,2),c2 decimal(10,2)); Query OK, 0 rows affected (0.29 sec) mysql> insert into test values(131072.32,131072.32); Query OK, 1 row affected (0.07 sec) mysql> select * from test; +-----------+-----------+ | c1 | c2 | +-----------+-----------+ | 131072.31 | 131072.32 | +-----------+-----------+ 1 row in set (0.00 sec) Copy after login
|
The above is the detailed content of What are the data types of mysql?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
