The data structure in SQL Drip 42-mysql

Source: Internet
Author: User
Tags modifier modifiers ranges truncated

Text: Data structure in SQL Drip 42-mysql

MySQL's numeric data types can be roughly divided into two categories, one is an integer and the other is a floating-point or decimal. Many different subtypes are available for each of these categories, and each subtype supports data of different sizes, and MySQL allows us to specify whether the value in a numeric field is positive or negative or 0 filled.

The table lists the various numeric types, their allowable ranges, and the amount of memory space they occupy.

Type size range (signed) range (unsigned) use
TINYINT 1 bytes (-128,127) (0,255) Small integer value
SMALLINT 2 bytes (-32 768,32 767) (0,65 535) Large integer value
Mediumint 3 bytes (-8 388 608,8 388 607) (0,16 777 215) Large integer value
int or integer 4 bytes (-2 147 483 648,2 147 483 647) (0,4 294 967 295) Large integer value
BIGINT 8 Bytes (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) Maximum integer value
FLOAT 4 bytes ( -3.402 823 466 e+38,1.175 494 351 E-38), 0, (1.175 494 351 e-38,3.402 823 466 351 e+38) 0, (1.175 494 351 e-38,3. 402 823 466 e+38) Single precision
Floating point value
DOUBLE 8 bytes (1.797 693 134 862 315 7 e+308,2.225 073 858 507 201 4 E-308), 0, (2.225 073 858 507 201 4 e-308,1.797 693 134 8 315 7 e+308) 0, (2.225 073 858 507 201 4 e-308,1.797 693 134 862 315 7 e+308) Double precision
Floating point value
Decimal to Decimal (m,d), if m>d, is m+2 otherwise the value of d+2 dependent on M and d is dependent on the value of M and D decimal values

INT type
The 5 major integer types supported in MySQL are Tinyint,smallint,mediumint,int and BIGINT. These types are largely the same, except that the size of the values they store is not the same.

MySQL extends the SQL standard in the form of an optional display width indicator, so that when retrieving a value from the database, it can be extended to a specified length. For example, specifying that a field is of type INT (6) guarantees that values with fewer than 6 contained numbers can be automatically populated with spaces when they are retrieved from a database. It is important to note that using a width indicator does not affect the size of the field and the range of values that it can store.

In case we need to store a number that exceeds the permitted range for a field, MySQL is truncated and then stored according to the one end of the allowable range closest to it. One of the more special areas is that MySQL will automatically change to 0 before the non-compliant value is inserted into the table.

The UNSIGNED modifier specifies that the field only holds positive values. Because you do not need to save the positive and negative symbols of the numbers, you can save a "bit" space at the time of storage. thereby increasing the range of values that this field can store.

The Zerofill modifier specifies a value of 0 (not a space) that can be used to really complement the output. Use this modifier to prevent the MySQL database from storing negative values.

FLOAT, DOUBLE, and DECIMAL types
The three floating-point types supported by MySQL are the float, DOUBLE, and DECIMAL types. Float numeric types are used to represent single-precision floating-point numbers, whereas double numeric types are used to represent double-precision floating-point numbers.

As with integers, these types also have additional parameters: a display width indicator and a decimal point indicator. For example, the statement FLOAT (7,3) specifies that the value displayed will not exceed 7 digits, followed by a 3-digit number after the decimal point.

For the number of digits after the decimal point beyond the allowable range, MySQL automatically rounds it to the nearest value, and then inserts it.

The DECIMAL data type is used in calculations that require very high precision, which allows you to specify the precision and count method of a numeric value as a selection parameter. The precision here refers to the total number of valid digits saved for this value, while the Count method represents the number of digits after the decimal point. For example, the statement decimal (7,3) specifies that the stored value will not exceed 7 digits and no more than 3 digits after the decimal point.

Ignoring the precision and count method modifiers of the DECIMAL data type will cause the MySQL database to set the precision of all fields identified as this data type to 10, and the calculation method set to 0.

The UNSIGNED and Zerofill modifiers can also be used by the FLOAT, DOUBLE, and DECIMAL data types. And the effect is the same as the INT data type.

String type
MySQL provides 8 basic string types that can store ranges from simple one character to huge chunks of text or binary string data.

Type size Purpose
CHAR 0-255-byte fixed-length string
VARCHAR 0-255-byte variable-length string
Tinyblob a binary string of 0-255 bytes and no more than 255 characters
Tinytext 0-255 bytes Short text string
BLOB 0-65 535-byte long text data in binary form
Text 0-65 535-byte long text data
Mediumblob 0-16 777 215 bytes Medium-length text data in binary form
Mediumtext 0-16 777 215 bytes Medium length text data
Logngblob 0-4 294 967 295 byte binary form of large text data
Longtext 0-4 294 967 295 byte maximum text data

CHAR and VARCHAR types
The CHAR type is used for fixed-length strings and must be defined within parentheses with a size modifier. This size modifier ranges from 0-255. A value larger than the specified length will be truncated, and a value smaller than the specified length will be filled with a space.

The CHAR type can use the BINARY modifier. When used for comparison operations, this modifier causes CHAR to participate in the operation in binary mode, rather than in a traditional case-sensitive manner.

A variant of the CHAR type is a VARCHAR type. It is a variable-length string type and must also have an indicator with a range of 0-255. The difference between char and Varchgar is the way the Musql database handles this indicator: Char treats this size as a value and is filled with blanks with no length. The VARCHAR type treats it as the maximum and stores the value using only the length that the string actually needs to be stored (adding an extra byte to store the length of the string itself). So a VARCHAR type that is shorter than the indicator length is not padded with spaces, but the value longer than the indicator will still be truncated.

Because varchar types can dynamically change the length of stored values based on the actual content, the use of varchar type can greatly save disk space and improve storage efficiency when it is not possible to determine how many characters a field requires.

The VARCHAR type is exactly the same as the CHAR type when using the BINARY modifier.

TEXT and BLOB types
For cases where the field length requires more than 255, MySQL provides TEXT and BLOB two types. Depending on the size of the stored data, they all have different subtypes. These large data are used to store binary data types such as text blocks or images, sound files, and so on.

The TEXT and BLOB types differ in classification and comparison. BLOB types are case-sensitive, while TEXT is not case-sensitive. The size modifier is not used for various blobs and TEXT subtypes. A value that is larger than the maximum range supported by the specified type is automatically truncated.

Date and Time type
MySQL has 5 different data types to choose from when working with date and time type values. They can be divided into simple dates, time types, and mixed date and time types. Depending on the accuracy required, subtypes can be used in each category, and MySQL has built-in functionality to turn a variety of input formats into a standard format.

Type size
(byte) Range format purpose
Date 3 1000-01-01/9999-12-31 Yyyy-mm-dd Day value
Time 3 ' -838:59:59 '/' 838:59:59 ' HH:MM:SS value or duration
Year 1 1901/2155 YYYY years value
DateTime 8 1000-01-01 00:00:00/9999-12-31 23:59:59 yyyy-mm-dd HH:MM:SS Blend date and time values
TIMESTAMP 8 1970-01-01 00:00:00/2037 YYYYMMDD HHMMSS Mixed date and time value, timestamp

DATE, time, and TEAR types
MySQL stores simple date values with the date and TEAR types, and time values are stored using the type of times. These types can be described as strings or sequence of integers without delimiters. If the description is a string, the value of the DATE type should be separated by a hyphen, and the value of the time type should be separated by a colon as a delimiter.

Note that the time type value without the colon delimiter will be understood by MySQL as the duration, not the timestamp.

MySQL also maximizes the interpreter of the two numbers in the year of the date, or the two numbers entered in the SQL statement for the TEAR type. Because the values of all TEAR types must be stored in 4 digits. MySQL attempted to convert a 2-digit year to a value of 4 digits. Converts values within the range of 00-69 to 2000-2069. Converts the value within the 70-99 range to 1970-1979. If the MySQL auto-converted value does not meet our needs, please enter a 4-digit year.

Dateyime and TIMESTAMP types
In addition to the date and time data types, MySQL supports both the Dateyime and TIMESTAMP hybrid types. They can store the date and time as a single value. These two types are typically used to automatically store timestamps that contain the current date and time, and can play a role in applications that need to perform a large number of database transactions and audit trails that require a debug and review purpose.

If we have no definite assignment to a field of type TIMESTAMP, or we are assigned a null value. MySQL will automatically populate it with the current date and time of the system.

Composite type
MySQL also supports two composite data types, ENUM and SET, which extend the SQL specification. Although these types are technically string types, they can be treated as different data types. An ENUM type allows only one value to be obtained from a collection, whereas a set type allows any number of values to be taken from a collection.

ENUM type
The ENUM type is somewhat similar to a single option because it allows only one value to be obtained in the collection. It's easy to understand when dealing with data from each other, such as human sexuality. The ENUM Type field can take a value from the collection or use a null value, otherwise the input will cause MySQL to insert an empty string in this field. In addition, if the casing of the inserted value does not match the case of the value in the collection, MySQL automatically converts the casing of the inserted value to a value that is consistent with the case in the collection.

The ENUM type can be stored as a number inside the system, and it is indexed with numbers starting from 1. An ENUM type can contain up to 65,536 elements, one of which is retained by MySQL to store the error message, which is represented by index 0 or an empty string.

MySQL considers that the value that appears in the ENUM type collection is a valid input, except that any other input will fail. This means that it is easy to find the location of the error record by searching for a row that contains an empty string or a corresponding numeric index of 0.

SET type
The SET type is similar but not the same as the ENUM type. A set type can take any number of values from a predefined collection. And the same as the ENUM type, any attempt to insert a non-predefined value in the SET Type field causes MySQL to insert an empty string. If you insert a record that has a valid element and an illegal element, MySQL retains the legitimate element, removing the illegal element.

A SET type can contain up to 64 elements. The value in the SET element is stored as a separate "bit" sequence, which represents the element corresponding to it. A bit is a simple and efficient way to create a collection of ordered elements. And it also removes duplicate elements, so it is not possible to include two identical elements in a SET type.

To find illegal records from the SET type field, simply look for rows that contain an empty string or binary value of 0.

The data structure in SQL Drip 42-mysql

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.