Fun Mysql data types

Catalog
a data type of common sense

Two: Mysql data type classification

III. Integer

Four: floating-point type

Five: Character Types

Six: Time Type

Seven: composite type

VIII. The type of construction of the table use

Nine: field set other properties

A. Some knowledge of data types

  1. Data is stored in MySQL.

2. As long as the data, we will specify the type of data.

3. In a predetermined field of a table used in a specific data type; then, it is necessary to use the corresponding data types in a data insertion; and comply with the requirements of the data types.

II: Classification Mysql data type
value types (integer, floating point)
string type
datetime type
composite type
spatial type (non-scientific work basically do not)
. Three integer
MySQL data types of Bytes Value range
tinyint 1 byte ~ 127 -128
smallint the 2-byte ~ 32767 -32768
MEDIUMINT. 3 ~ byte 8388607 -8388608
int. 4 byte range -2147483648 to 2147483647
18 is power bigint 8 bytes + -9.22 * 10
1. integer length different, the actual use of the process is different.

In an alternative form 2.MySQL display width indicator extend the SQL standard, so that when a value is retrieved from the database, this value can be extended to a specified length. For example, to specify a field type INT (6), it is possible to ensure a space filled automatically when less than 6 contained in the digital value retrieved from the database. It should be noted that the use of a range of values ​​does not affect the width of the indicator field and its size can be stored.

3. Note:

When you create a table field, we can use small gender unsigned integer (tinyint) to represent. Women represented by 0, denoted by 1 male. 2 represents by unknown.
Also the age of mankind is, when you create a table of available fields unsigned integer. Because humans age has not been negative
in actual use. Our business in much the maximum value needs to be stored. When we create a table, what type you choose to store such values.
IV: floating type
MySQL data types of Bytes Value range
float (m, d) 4-byte single-precision floating-point, m the total number, d decimal places
double (m, d) 8-byte double-precision floating-point type, the total number of m, d decimal
decimal (m, d) decimal floating-point number is stored as a string
Note:

Floating-point inexact value, there will be less accurate situation
and called a fixed decimal point. Inside MySQL, essentially using the stored character strings. The actual use of the process, if there is money, money relatively high precision floating-point storage, we recommend using decimal (fixed-point) of this type.
Five: character type
MySQL data types of Bytes Value Range
CHAR 0-255 byte fixed-length string
VARCHAR 0-255 bytes of variable-length string
TINYBLOB 0-255 byte binary strings of no more than 255 characters
TINYTEXT 0 -255 bytes short text strings
BLOB 0-65535 bytes long text data in binary form
tEXT 0-65535 bytes long text data
MEDIUMBLOB 0-16 777 215-byte binary text data in the form of medium length
MEDIUMTEXT 0-16 777 215 medium length byte text data
LOGNGBLOB 0-4 294 967 295 bytes of text data in binary form greatly
LONGTEXT 0-4 294 967 295 bytes of text data greatly
VARBINARY (M) to allow the length of the 0-M-byte fixed-length byte length of the string value of + 1 bytes
BINARY (M) M allowed length of 0-M-byte fixed-length string of bytes

  1. CHAR type for fixed-length string, and must be defined with a size modifier in parentheses. This size range from 0-255 modifier. A value larger than the specified length will be truncated, but smaller than the specified length value will be padded with spaces.

2. VARCHAR   this size as a size value, the absence of sufficient length the situation is not padded with spaces. The VARCHAR type and use it as the maximum length of the string stored only the actual needs of the type of space will not be filled, but still longer than the indicator value will be truncated. Because the length of type VARCHAR stored value can be dynamically changed according to the actual content, the use of type VARCHAR character field can not be determined how many need to save disk space can greatly improve the storage efficiency.

3.text type field length requirements for the type of blob exceeds 255, MySQL provides two types TEXT and BLOB. The size of the stored data, which has different subtypes. These large text block for storing data or images, sound files and other binary data type.

4.TEXT BLOB type and there is a difference in the classification and comparison. BLOB type case sensitive, and TEXT case insensitive. BLOB size modifier is used in a variety of subtypes and TEXT.

VI: Type Time
MySQL data types of Bytes Value range
date 3 byte date in the format: 2014-09-18
Time. 3-byte time in the format: 08: 42 is: 30
datetime. 8-byte date and time format: 2014- 08:42:30 09-18
timestamp stored automatically. 4 byte record modified time of
year 1-byte Year
Note:

Type in time with web systems is relatively small, often a lot of people prefer to use int to store the time. Unix is inserted when inserting a time stamp, as calculated in this way is more convenient. In the front end service with date function of the type, then it can be converted to unix timestamp identified time.
The above type you can actually choose according to the actual situation of
some people for easy viewing in database management, some people use the datetime type to store time.
Seven: composite type
MySQL data type description example
set collection type set ( "Member", "member2", ... "member64")
enum enumeration type enum ( "member1", "member2 ", ... "member65535")
      a type ENUM only It allows to obtain a value from a set; sET and allows to obtain any type from a plurality of set values.

  1. ENUM type

Because ENUM type allows only get a value in the set, somewhat similar to a single option. In dealing with each other and excluded data easy to understand, such as the human gender. ENUM type field can be obtained from a value or a set of null values, in addition to the input will cause MySQL insert an empty string in this field. Further, if a mismatch is inserted in the case and the case of values ​​of the set values, MySQL will automatically insert case conversion value to coincide with the set value of the case.

ENUM type within the system may be stored as a number, and from an index figure is started. ENUM type can contain a maximum of 65536 elements, wherein a retention element is MySQL, used to store the error message, the error value represented by index 0 or an empty string.

MySQL ENUM think the value of the type that appears in the collection is legitimate input, in addition to any other input will fail. This description contains an empty string or by searching the index number corresponding to the row 0 can easily find the position of the error log.

  1. SET SET type type ENUM type similar but not identical. SET type can be obtained from any number of values ​​in a predefined set. And the same type ENUM any attempt to insert a non-predefined type field in the SET value will cause MySQL insert an empty string. If you insert a legitimate element there that is illegal recording elements, MySQL will retain legal elements, remove illegal elements.

A SET type can contain up to 64 elements. In the SET value is stored as a separate element of the "bit" sequence, these "bits" denotes an element corresponding to it. "Bits" is to create a simple and effective way of ordered elements of the collection.
And it also removes duplicate elements, so that SET is not possible type comprising two identical elements.
Hoping to find illegal recording just look for the line that contains an empty string or binary value 0 from SET type field.

VIII. The type of construction of the table using the
type to use when creating the corresponding statement tables

Create Table IF Not EXISTS Demo (
ID int (. 11) the NOT NULL,
username VARCHAR (50) the NOT NULL,
password Cha) the NOT NULL,
Content LONGTEXT the NOT NULL,
CreateTime datetime the NOT NULL,
Sex tinyint (. 4) the NOT NULL
) ENGINE = the InnoDB DEFAULT CHARSET = utf8;
IX: field other properties set
1. uNSIGNED (unsigned) integer and floating-point type is mainly used, unsigned. That is, without the foregoing - (minus).

    The number of bits stored longer. tinyint integer value interval of -128 to 127. After the use of unsigned length may store 0-255.

    When you create after the integer or floating point field connect statement: unsigned;

2. ZEROFILL (0 padding) 0 (not a space) can be used to make the output true value. This modifier can prevent the use of MySQL database storage negative.

    When creating integer or floating point field after the statement connected: zerofill

3. defaultdefault property to ensure that under no value is available, given a constant value, this value must be a constant, because MySQL does not allow insertion of a function or expression values. In addition, this property can not be used BLOB or TEXT columns. If you have already specified for this column NULL attribute, the default value is not specified a default value will be NULL, otherwise default value will depend on the field's data type.

   When you create a connected after the integer or floating point field statement: default 'value'

4. not null if a column is defined as not null, the null value is inserted into the column are not allowed. It recommends always using not null attributes in an important case, because it provides a basic authentication, ensure that you have passed all the necessary values ​​to the query.

  When creating integer or floating point field after the statement connect: not null

5. The null null attribute is specified when a column, the column can remain empty, while the other columns regardless of whether the row has been filled. Remember, null accurate to say "no" rather than an empty string or zero.

When you create do not null statement can be integer or floating point field after the statement.

Guess you like

Origin blog.51cto.com/14545985/2439045