1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu MySQL Administrator''''s Bible- P5 doc

50 466 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 407,22 KB

Nội dung

MySQL Data Types 5 TABLE 5-1 Summary of MySQL Character String Types Data Type Name SQL Standard? Fixed/Variable Length Range Size Attributes CHAR Yes Fixed Length of 0–255, depends on character set M*x bytes ASCII BINARY CHARACTER SETCOLLATION DEFAULT UNICODE VARCHAR Yes Variable Length of 0–255, depends on character set L*x+1 if L<255 L*x+2 if L>255 ASCII BINARY CHARACTER SETCOLLATION DEFAULT UNICODE TINYTEXT No Variable Max length of 255 bytes L+1 bytes 1 byte stores length ASCII BINARY CHARACTER SETCOLLATION UNICODE TEXT No Variable Max length of 65,535 bytes (64 Kb) L+2 bytes 2 bytes store length ASCII BINARY CHARACTER SETCOLLATION UNICODE MEDIUMTEXT No Variable Max length of 16,777,215 bytes (16 Mb) L+3 bytes 3 bytes store length ASCII BINARY CHARACTER SETCOLLATION UNICODE LONGTEXT No Variable Max length of 4,294,967,295 bytes (4 Gb) L+4 bytes 2 bytes store length ASCII BINARY CHARACTER SETCOLLATION NOT NULL NULL UNICODE 167 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Part II Developing with MySQL Like character string types, MySQL supports the SQL standard for fixed- and variable-length strings, but not for character objects. The SQL standard states that the NATIONAL equivalents of character string types are the same as the character string types, except that a specific character set is used. In MySQL, this character set is utf8: mysql> CREATE TABLE nchar_test (nchar_fld NCHAR(10)); Query OK, 0 rows affected (0.55 sec) mysql> SHOW CREATE TABLE nchar_test\G *************************** 1. row *************************** Table: nchar_test Create Table: CREATE TABLE `nchar_test` ( `nchar_fld` char(10) CHARACTER SET utf8 DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) The characteristics and usage of national character string types is exactly the same as character string types, with one exception: the ASCII and UNICODE attributes are not proper syntax. This is because the ASCII and UNICODE attributes set the character set, which conflicts with the NATIONAL keyword. For details on character string types, see the section ‘‘Character String Types’’ earlier in this chapter. Binary Large Object String Types A binary string type is the least restrictive data type. There is one binary large object type in the ISO SQL:2003 standard, with two aliases: ■ BINARY LARGE OBJECT(length) ■ BLOB(length) MySQL supports only the second standard syntax, BLOB(length). However, MySQL extends the SQL standard for binary large object string types with five additional binary types: ■ TINYBLOB ■ MEDIUMBLOB ■ LONGBLOB ■ BINARY(length) ■ VARBINARY(length) 168 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. MySQL Data Types 5 Binary string types are byte strings. Character strings are ordered lexically; binary strings are ordered by each byte’s value. The standard does not specify what makes an object ‘‘large,’’ and there is no standard equivalent for smaller binary strings, so we have included the smaller BINARY and VARBINARY byte string types into this category. BLOB values The four BLOB types are very similar to each other — the only differences are the maximum amount of data each can store and the overhead involved in storing the size of each record: ■ TINYBLOB — Up to 255 bytes, 1 byte overhead ■ BLOB — Up to 64 Kb, 2 bytes overhead ■ MEDIUMBLOB — Up to 16 Mb, 3 bytes overhead ■ LONGBLOB — Up to 4 Gb, 4 bytes overhead A BLOB data type field is a separately allocated object than the table that contains it, like the TEXT data type fields. BINARY values BINARY and VARBINARY are similar to the CHAR and VARCHAR data types, respectively. For the BINARY and VARBINARY data types, the length is an integer representing the length, in bytes,ofastring.Adatatypeof BINARY or VARBINARY with a length of 0 is valid, but can hold only two strings: the empty string and NULL.NotethatBINARY and VARBINARY are different from CHAR BINARY and VARCHAR BINARY — BINARY and VARBINARY arebytestrings,and CHAR BINARY and VARCHAR BINARY are case-sensitive character strings. BINARY length The length of BINARY is an integer from 0–255. If a string is stored as a BINARY and is smaller than the length, binary spaces (represented by \0) are appended to the string. A binary space is different from a regular space character; a binary space has an ASCII value of 0 and the regular space character has an ASCII value of 32: mysql> SELECT ’ ’,ASCII(’ ’), ’\0’, ASCII(’\0’); + + + + + | | ASCII(’ ’) | | ASCII(’\0’) | + + + + + || 32|| 0| + + + + + 1 row in set (0.02 sec) Because of this, a value of ’a’ appears before a value of ’a ’ in an ascending sort. This also means that the BINARY value ’a’ is the same as the BINARY value ’a\0’ for the purpose of unique constraints. There is no removal of trailing spaces when a BINARY string is retrieved from a table. 169 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Part II Developing with MySQL VARBINARY length The maximum length of a VARBINARY is restricted only by the maximum row length. In most storage engines, the maximum row length is the maximum allowed by MySQL, which is 65,535 bytes. Only the NDB storage engine has a different maximum value. Like a VARCHAR,intheory, the maximum length of a VARBINARY is 65,535 bytes. In practice, there is some overhead in storing the VARBINARY data type, which further limits the actual possible size of a VARBINARY. If the length of VARBINARY is less than 255 bytes, one byte per row is used to store the actual length of the string. If the length of VARBINARY is greater than 255 bytes, the overhead cost of storing the string length is two bytes per row. There is also per-table overhead — every table allocates one byte for every set of eight potentially nullable fields, regardless of field types. Thus, the maximum length of a VARBINARY is 65,532 bytes, and that is only if the VARBINARY field is the only field in the table. For example, another field with a type of INT uses 4 bytes, so the maximum length of a VARBINARY in that table would be 65,528 bytes. For VARBINARY strings larger than the maximum allowed, use the BLOB data type. If you try to define a table that exceeds the maximum row length, you will get the following error: mysql> CREATE TABLE max_len_varbin(fld VARBINARY(65533)); ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs Table 5-2 shows a summary of the MySQL binary data types. As with the character string data types, the numbers given in Table 5-2 are the basic storage requirements of MySQL. The storage engine used may add additional overhead or provide data compression that reduces the storage required. See Chapter 11 for more details about storage engines. Numeric Types The ISO SQL:2003 standard defines two numeric types. Each numeric type has a few different data types. The standard numeric types and their associated keywords are: ■ Exact numeric type: ■ NUMERIC(g,f) ■ DECIMAL(g,f) can be abbreviated as DEC ■ SMALLINT ■ INTEGER can be abbreviated as INT ■ BIGINT 170 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. MySQL Data Types 5 TABLE 5-2 Summary of MySQL Binary Data Types Data Type Name SQL Standard? Fixed/Variable Length Range Size Attributes BINARY No Fixed Length of 0–255 bytes M bytes DEFAULT NOT NULL NULL VARBINARY No Variable Length of 0–65,532 bytes L*x+1 if L<255 L*x+2 if L>255 DEFAULT NOT NULL NULL TINYBLOB No Variable Max length of 255 bytes L+1 bytes 1 byte stores length NOT NULL NULL BLOB No Variable Max length of 65,535 bytes (64 Kb) L+2 bytes 2 bytes store length NOT NULL NULL MEDIUMBLOB No Variable Max length of 16,777,215 bytes (16 Mb) L+3 bytes 3 bytes store length NOT NULL NULL LONGBLOB No Variable Max length of 4,294,967,295 bytes (4 Gb) L+4 bytes 2 bytes store length NOT NULL NULL ■ Approximate numeric type: ■ FLOAT(p) ■ REAL ■ DOUBLE PRECISION MySQL supports these data types with one exception — the DOUBLE PRECISION data type is simply named DOUBLE. In addition, the NUMERIC data type is an alias for the DECIMAL data type. The standard SQL has been extended to add these additional numeric data types: ■ Exact numeric types: ■ TINYINT ■ MEDIUMINT ■ BIT(x) ■ SERIAL In MySQL, the SERIAL numeric data type is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE KEY . 171 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Part II Developing with MySQL By default, the REAL numeric data type is an alias for DOUBLE. However, you can change that behavior by changing the sql_mode to include REAL_AS_FLOAT, which causes the REAL numeric data type to be an alias for FLOAT. See ‘‘Choosing SQL Modes’’ later in this chapter for more detail. Numeric data sizes and ranges Each numeric data type can store a limited range of values, and each numeric data type stores its values in a certain size. DECIMAL size and range A DECIMAL field is defined using the syntax DECIMAL(g,f). The first argument (g) is the total number of digits, and the second argument ( f) is the number of digits after the decimal point. For example, the data type DECIMAL(5,2) can store values between –999.99 and 999.99. The default value for g is 10 and the default value for f is 0; the maximum value for g is 65 and the maximum value for f is 30. The size of a DECIMAL field is variable. MySQL stores DECIMAL in a binary format, where each group of 9 digits is stored in 4 bytes. The size of a DECIMAL field is determined by the num- ber of digits in the integer part (the value of p-s) and the number of digits in the fractional part (the value of s). The integer and fractional parts are stored separately in 4-byte, 9-digit groups. If the number of digits in each group is not divisible by nine, the remaining digits are stored in CEILING(digits/2) bytes. As an example, the size of DECIMAL(12,2) can be calculated as follows: Integer part = (12-2) digits = 10 digits = 9 digits + 1 digit 9 digits = 4 bytes 1 digit left over CEILING(1/2) = 1 byte Total integer part = 5 bytes Fractional part = 2 digits CEILING(2/2) = 1 byte Total size = 6 bytes Integer sizes and ranges The integer data types are TINYINT, SMALLINT, INT, MEDIUMINT,andBIGINT.Table5-3 shows the data sizes and ranges for the integer data types: Note that the size of the field is the size of the data type, not the size of the value stored. For example, the value 123 stored in a BIGINT field is stored in 8 bytes. The same value 123 stored in a TINYINT field is stored in 1 byte. 172 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. MySQL Data Types 5 TABLE 5-3 Data Sizes and Ranges for Integer Data Types Data Type SIGNED Range UNSIGNED Range Size TINYINT –128 to 127 0 to 255 1 byte SMALLINT –32,768 to 32,767 0 to 65,535 2 bytes MEDIUMINT –8,388,608 to 8,388,607 0 to 16,777,215 3 bytes INT –2,147,483,648 to 2,147,483,647 0 to 4,294,967,295 4 bytes BIGINT –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0to 18,446,744,073,709,551,615 8 bytes MySQL allows a minimum display width to be set for integer types. If an integer value is less than this width, the value will be left-padded with enough spaces so the value is displayed as this width. This is only for the display and does not change the actual value returned. This can be specified by giving the width as an argument to the integer data type, for example INT(4). This does not change the range nor the size of the data type, just the minimum display width. MySQL performs calculations and comparisons using double-precision floating-point numbers. Calculations using unsigned BIGINT values larger than 63 bits (9,223,372,036,854,775,807) should only be done via bit functions. BIT size and range The BIT data type stores integers as a series of bits. The range of a BIT field is determined by the argument to BIT(x). The default range is 1 bit and the range can be set from 1 to 64 bits. The BIT values are stored in binary format (that is, it is stored in base 2, as opposed to deci- mal format, which is stored in base 10). Unlike other data types, a BIT value needs to be con- verted upon retrieval to produce a human-readable result. How to retrieve BIT values depends on whether you want to retrieve integers or bit strings: mysql> USE test; Database changed mysql> CREATE TABLE bit_test (bt BIT(10)); Query OK, 0 rows affected (0.64 sec) mysql> INSERT INTO bit_test (bt) VALUES (0),(1),(2),(3),(4); Query OK, 5 rows affected (11.78 sec) Records: 5 Duplicates: 0 Warnings: 0 173 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Part II Developing with MySQL mysql> SELECT bt,bt+0,BIN(bt) FROM bit_test; + + + + | bt | bt+0 | BIN(bt) | + + + + ||0|0| |  |1|1 | |  |2|10 | | ♥ |3|11 | | ♦ | 4 | 100 | + + + + 5 rows in set (0.09 sec) The BIT value, as it is stored, is shown in the first field of the result. As you can see, it is not in human-readable format. The second field of the result casts the result as an integer, and the third field casts the result as a bit string. FLOAT size and range The FLOAT data type is a single-precision floating-point number. Floating-point means that unlike the DECIMAL data type, the decimal point can be anywhere in the number — the decimal point floats.A FLOAT is limited in how many significant digits it can store. In the SQL standard, this limitation can be specified as the argument p (p stands for precision). In MySQL, this limitation depends on the hardware and operating system, but is usually a precision of 24 bits. This translates to 6 or 7 significant digits, and a storage cost of 4 bytes per FLOAT.IfaFLOAT field is defined with a larger value of p, it is changed into a DOUBLE field: mysql> USE test; Database changed mysql> CREATE TABLE float_double_test ( -> f1 FLOAT(1), f2 FLOAT(10), f3 FLOAT(23), -> f4 FLOAT(24), f5 FLOAT(53)); Query OK, 0 rows affected (0.16 sec) mysql> SHOW CREATE TABLE float_double_test\G *************************** 1. row *************************** Table: float_double_test Create Table: CREATE TABLE `float_double_test` ( `f1` float DEFAULT NULL, `f2` float DEFAULT NULL, `f3` float DEFAULT NULL, `f4` float DEFAULT NULL, `f5` double DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.06 sec) mysql> ALTER TABLE float_double_test ADD COLUMN f6 FLOAT(54); ERROR 1063 (42000): Incorrect column specifier for column ’f6’ 174 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. MySQL Data Types 5 A significant digit is a digit that signifies precision, and not a power of ten. The following example was done on a system where the number of significant digits is six: mysql> CREATE TABLE float_test (ft float, ft_text varchar(10)); Query OK, 0 rows affected (0.59 sec) mysql> INSERT INTO float_test (ft,ft_text) VALUES -> (1234567,’1234567’), (123456,’123456’), (12345.6,’12345.6’), -> (123.456,’123.456’), (1.23456,’1.23456’), -> (0.00123456,’0.00123456’), -> (1.23456e-3,’1.23456e-3’), (123456000,’123456000’), -> (1.23456e8,’1.23456e8’), (123456e3,’123456e3’); Query OK, 10 rows affected (0.08 sec) Records: 10 Duplicates: 0 Warnings: 0 mysql> SELECT ft, ft_text FROM float_test ORDER BY ft; + + + | ft | ft_text | + + + | 0.00123456 | 0.00123456 | | 0.00123456 | 1.23456e-3 | | 1.23456 | 1.23456 | | 123.456 | 123.456 | | 12345.6 | 12345.6 | | 123456 | 123456 | | 1234570 | 1234567 | | 123456000 | 123456000 | | 123456000 | 1.23456e8 | | 123456000 | 123456e3 | + + + 10 rows in set (0.06 sec) Note that the values 123456000, 1.23456e8 and 123456e3 are all the same floating-point number. The numbers including e indicate scientific notation, replacing e with *10 ˆ . Indeed, 123456000, 1.23456*10 ˆ 8 and 123456*10 ˆ 3 all signify the same number. MySQL automatically rounded the value with more than six significant digits to have exactly six significant digits — the value 1234567 was rounded to 1234570. As mentioned previously, MySQL supports the SQL standard FLOAT(p) syntax. It also supports a syntax similar to the DECIMAL syntax. A FLOAT field can be defined as in the preceding example, or it can be defined as FLOAT(g,f). The first argument (g) is the total number of digits, and the second argument ( f) is the number of digits after the decimal point. The FLOAT(g,f) syntax can be used to override the default amount of significant digits. Therefore, higher or lower precision can be specified using the two-argument syntax. It should be noted that this is not an exact substitute for defining precision, because the number of digits after the decimal point is fixed. 175 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Part II Developing with MySQL MySQL performs calculations and comparisons using double-precision floating-point numbers. Queries involving a FLOAT field may return unexpected results; rounding an (internal) DOUBLE number often does not yield the same result as rounding a FLOAT number. Therefore, comparing a FLOAT field to a calculated number will often produce incorrect results, because the calculated number is a DOUBLE. The following example shows that calculating a FLOAT value (by adding 0) changes it to a DOUBLE value, which can produce a very different value from the original FLOAT: mysql> SELECT ft_text, ft, ft+0 FROM float_test; + + + + | ft_text | ft | ft+0 | + + + + | 1234567 | 1234570 | 1234567 | | 123456 | 123456 | 123456 | | 12345.6 | 12345.6 | 12345.599609375 | | 123.456 | 123.456 | 123.45600128173828 | | 1.23456 | 1.23456 | 1.2345600128173828 | | 0.00123456 | 0.00123456 | 0.00123456004075706 | | 1.23456e-3 | 0.00123456 | 0.00123456004075706 | | 123456000 | 123456000 | 123456000 | | 1.23456e8 | 123456000 | 123456000 | | 123456e3 | 123456000 | 123456000 | + + + + 10 rows in set (0.04 sec) DOUBLE size and range The DOUBLE data type is a double-precision floating-point number. Like a FLOAT,aDOUBLE is limited in how many significant digits it can store. See previous subsection ‘‘FLOAT Size and Range’’ for an explanation of floating-point numbers and significant digits. The data type is named DOUBLE because the limitation is approximately double the limitation of a single-precision FLOAT. As with FLOAT, this limitation depends on the hardware and operating system, but is usually a precision of 53 bits. This translates to 14 or 15 significant digits, and a storage cost of 8 bytes per DOUBLE. As with FLOAT, MySQL supports a syntax similar to the DECIMAL syntax. A DOUBLE field can be defined with no parameters as DOUBLE, or it can be defined as DOUBLE(g,f). The first argu- ment ( g) is the total number of digits, and the second argument (f) is the number of digits after the decimal point. The DOUBLE(g,f) syntax can be used to override the default number of significant digits. It should be noted that this is not an exact substitute for defining precision, because the number of digits after the decimal point is fixed. 176 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... ’’: mysql> SET SESSION sql_mode=’’; Query OK, 0 rows affected (0.00 sec) Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 201 5 Part II Developing with MySQL mysql> USE test; Database changed mysql> CREATE TABLE sql_mode_test (sm1 tinyint not null); Query OK, 0 rows affected (0.20 sec) mysql> INSERT INTO sql_mode_test (sm1) VALUES (126); Query OK, 1 row affected (0.13 sec) mysql> ... page) shows a summary of the MySQL numeric data types Boolean Types The ISO SQL:2003 standard defines a boolean data type of BOOLEAN MySQL supports the standard and adds a nonstandard abbreviation of BOOL However, MySQL implements BOOLEAN as an alias for TINYINT(1): mysql> CREATE TABLE boolean_test ( -> bt1 BOOLEAN, bt2 BOOL, bt3 TINYINT(1)); Query OK, 0 rows affected (0.19 sec) mysql> SHOW CREATE TABLE... attributes: ■ WITH TIME ZONE ■ WITHOUT TIME ZONE MySQL supports these datetime types, although it does not support the two attributes nor the TIME and TIMESTAMP precision arguments MySQL adds the following datetime data types: ■ YEAR ■ DATETIME The YEAR data type can be specified as YEAR(2) or YEAR(4) MySQL converts other values specified, including no value, as YEAR(4): mysql> create table year_test ( -> yt1... the empty string (’’) Otherwise, invalid ENUM and SET values cause mysqld to throw an error, and no values are stored The index of the empty string is 0: mysql> SET SESSION sql_mode=’’; Query OK, 0 rows affected (0.00 sec) mysql> CREATE TABLE enum_set_index ( -> et ENUM(’a’,’’,’1’), st SET(’a’,’1’)); Query OK, 0 rows affected (0.17 sec) mysql> INSERT INTO enum_set_index (et, st) VALUES -> (’a’,’a’), (’1’,’1’),... SHOW CREATE TABLE: mysql> USE INFORMATION_SCHEMA; Database changed mysql> SELECT AUTO_INCREMENT -> FROM TABLES -> WHERE TABLE_SCHEMA=’sakila’ AND TABLE_NAME=’category’\G *************************** 1 row *************************** AUTO_INCREMENT: 17 1 row in set (0.00 sec) Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 177 5 Part II Developing with MySQL mysql> USE sakila;... be desired; if it is not desired, set the appropriate sql_mode to throw an error: mysql> SET SESSION sql_mode=’TRADITIONAL’; Query OK, 0 rows affected (0.00 sec) mysql> TRUNCATE sql_mode_test; Query OK, 0 rows affected (0.42 sec) mysql> INSERT INTO sql_mode_test (sm1) VALUES (126); Query OK, 1 row affected (0.08 sec) mysql> INSERT INTO sql_mode_test (sm1) VALUES (127); Query OK, 1 row affected (0.09... NOT NULL AUTO_INCREMENT UNIQUE KEY 182 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark MySQL Data Types A value of 0 is false; non-zero values are true mysql> INSERT INTO boolean_test (bt1, bt2, bt3) -> VALUES (true, 0, 5); Query OK, 1 row affected (0.20 sec) mysql> SELECT bt1, bt2, bt3 FROM boolean_test; + + + + | bt1 | bt2 | bt3 | + + + + | 1 | 0 | 5 | + +... width of three digits to the left of the decimal point and four digits to the right of the decimal point: mysql> USE test; Database changed mysql> CREATE TABLE double_zerofill ( -> db DOUBLE(8,4) NOT NULL DEFAULT 0, -> dz DOUBLE(8,4) ZEROFILL NOT NULL DEFAULT 0); Query OK, 0 rows affected (0.11 sec) mysql> SHOW CREATE TABLE double_zerofill\G *************************** 1 row ***************************... watermark 187 5 Part II Developing with MySQL + -+ | 2008-11-20 07:45:55 | + -+ 1 row in set (0.00 sec) Query OK, 1 row affected (0.45 sec) mysql> SELECT ts1, ts_note FROM ts_test; + -+ + | ts1 | ts_note | + -+ + | 2008-11-20 07:45:15 | test | | 2008-11-20 07:45:55 | later test | + -+ + 2 rows in set (0.00 sec) mysql> UPDATE ts_test SET ts_note=’early... example shows the numerical representation of the zero value and a sample value: mysql> CREATE TABLE date_test (dt1 DATE, dt2 DATETIME, -> dt3 TIMESTAMP, dt4 TIME, dt5 YEAR(2), dt6 YEAR); Query OK, 0 rows affected (0.11 sec) 188 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark MySQL Data Types mysql> show create table date_test\G *************************** 1 row *************************** . is used. In MySQL, this character set is utf8: mysql& gt; CREATE TABLE nchar_test (nchar_fld NCHAR(10)); Query OK, 0 rows affected (0.55 sec) mysql& gt; SHOW. or bit strings: mysql& gt; USE test; Database changed mysql& gt; CREATE TABLE bit_test (bt BIT(10)); Query OK, 0 rows affected (0.64 sec) mysql& gt; INSERT

Ngày đăng: 21/01/2014, 13:20

TỪ KHÓA LIÊN QUAN