Some introductions to MySQL data types
The types of data fields defined in MySQL are very important for the optimization of your database;
MySQL supports multiple types, which can be roughly divided into three categories: numerical values, date/time and strings (characters) Type;
Integer type
The meaning of N in Int(N)
defines init(5 ) zerofill When joining int (10), the display width does not match, and a temporary table may appear.
N means that the display width is N, but it still occupies 4 bytes of storage, and the storage range remains unchanged;
>create table int_test(a int zerofill NOT NULL auto_increment, PRIMARY KEY (a)); >createtable int_test_4(a int(4) zerofill NOT NULL auto_increment, PRIMARY KEY (a)); >select * from int_test_4; +------------+ |a | +------------+ | 0001 | | 0002 | |2147483648 | +------------+ >select * from int_test; +------------+ |a | +------------+ |0000000001 | |0000000002 | |2147483648 | +------------+
About floating point number types: 1) Try not to use them if possible, 2) Floating point numbers cannot be used in equal sign comparison scenarios
It is recommended that TINYINT replace enum
Date and Time type
The date and time types representing time values are DATETIME, DATE, TIMESTAMP, TIME and YEAR.
MySQL5.6 does not support year(2)
Date type Note
Timestamp, datatime supports automatic update to the current time from MySQL5.6.5: current timestamp
Date conversion: cast (datatime_col as DATE)
> ;select now()+0;
5.6 US support
>select now(4),MICROSECOND(now(4)); +--------------------------+---------------------+ |now(4) |MICROSECOND(now(4)) | +--------------------------+---------------------+ |2016-04-16 08:50:01.6589 | 658900 | +--------------------------+---------------------+
timestamp supports null after 5.6.6
It is recommended that datetime replace timestamp
String type
String type refers to CHAR, VARCHAR, BINARY, VARBINARY, BLOB, TEXT, ENUM and SET. This section describes how these types work and how to use them in queries;
Character type
The difference between varchar and char
char is a fixed-length type, and varchar is a variable-length type. The difference between them is: In the data column of type char(M), each value occupies M bytes. If a certain length is less than M, MySQL will pad it with space characters on the right. (Padding space characters will be removed during the search operation.) In a varchar(M) type data column, each value only takes up just enough bytes plus one byte to record its length ( That is, the total length is L+1 bytes) varchar stores variable-length strings. When it is less than 255 bytes, it requires 1 extra byte (more than 255 requires 2 extra bytes) to store the length. The maximum length is 65532 bytes (all Column sum);
char is stored in fixed length, and the trailing spaces will be truncated when reading. The maximum length is 255 characters;
1) The meaning of CHAR (M):
The actual allocated length is: M * character encoding length = storage space
For example: 255 characters are stored and Chinese characters occupy 3 bytes
255*3 = 765 A total of 765 characters Section
2) The meaning of N in varchar(N)
can store up to N characters; varchar(5) and varchar(200) occupy the same space for storing hello, but the latter is sorted It will consume more memory because order by col uses fixed_length to calculate the col length (the same is true for the memory engine)
For example:
varchar(200)How many bytes are occupied by utf8
200*3+ 2
varchar(64) utf8
64*3=192<255
192+1=193
Recommendation:
Usually use MySQL as the innodb engine. The innodb engine is originally a variable-length storage.
Row storage:
trx_id, row-id,rollback, filed_pointer, null-flag, filed1,....
innodb storage engine recommended varchar
Char is faster because fixed-length allocation of char in a heap table like MyISAM will be faster
Calculation example
Give two examples to illustrate the calculation of the actual length.
a) If a table has only one varchar type, such as defined as
createtable t4(c varchar(N)) charset=gbk;
The maximum value of N here is The value is (65535-1-2)/2= 32766.
The reason for subtracting 1 is that the actual row storage starts from the second byte;
The reason for subtracting 2 is that the 2 bytes of the varchar header represent the length;
The reason for dividing by 2 is that the character encoding is gbk.
b) If a table is defined as
createtable t4(c int, c2 char(30), c3 varchar(N)) charset=utf8;
then here The maximum value of N is (65535-1-2-4-30*3)/3=21812
Subtracting 1 and subtracting 2 is the same as the above example;
The reason for subtracting 4 is int Type c occupies 4 bytes;
The reason for subtracting 30*3 is that char(30) occupies 90 bytes, and the encoding is utf8.
The above is the detailed content of Some introductions to MySQL data types. 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

AI Hentai Generator
Generate AI Hentai for free.

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



In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

Detailed explanation of database ACID attributes ACID attributes are a set of rules to ensure the reliability and consistency of database transactions. They define how database systems handle transactions, and ensure data integrity and accuracy even in case of system crashes, power interruptions, or multiple users concurrent access. ACID Attribute Overview Atomicity: A transaction is regarded as an indivisible unit. Any part fails, the entire transaction is rolled back, and the database does not retain any changes. For example, if a bank transfer is deducted from one account but not increased to another, the entire operation is revoked. begintransaction; updateaccountssetbalance=balance-100wh
