GIT – supports a number of column types, which may be grouped into three categories: numeric types, date and time types, and string (character) types. This section first gives an overview of the types available. Please refer to the manuals for more details.

Type
Use for
Size
TINYINT A very small integer The signed range is –128 to 127. The unsigned range is 0 to 255.
SMALLINT A small integer The signed range is –32768 to 32767. The unsigned range is 0 to 65535
MEDIUMINT A medium-size integer The signed range is –8388608 to 8388607. The unsigned range is 0 to 16777215
INT or INTEGER A normal-size integer The signed range is –2147483648 to 2147483647. The unsigned range is 0 to 4294967295
BIGINT A large integer The signed range is –9223372036854775808 to 9223372036854775807. The unsigned range is 0 to 18446744073709551615
FLOAT A small (single-precision) floating-point number. Cannot be unsigned Ranges are –3.402823466E+38 to –1.175494351E-38, 0 and 1.175494351E-38 to 3.402823466E+38. If the number of Decimals is not set or <= 24 it is a single-precision floating point number
DOUBLE,
DOUBLE PRECISION,
REAL
A normal-size (double-precision) floating-point number. Cannot be unsigned Ranges are -1.7976931348623157E+308 to -2.2250738585072014E-308, 0 and 2.2250738585072014E-308 to 1.7976931348623157E+308. If the number of Decimals is not set or 25 <= Decimals <= 53 stands for a double-precision floating point number
DECIMAL,
NUMERIC
An unpacked floating-point number. Cannot be unsigned Behaves like a CHAR column: “unpacked” means the number is stored as a string, using one character for each digit of the value. The decimal point, and, for negative numbers, the ‘-‘ sign is not counted in Length. If Decimals is 0, values will have no decimal point or fractional part. The maximum range of DECIMAL values is the same as for DOUBLE, but the actual range for a given DECIMAL column may be constrained by the choice of Length and Decimals. If Decimals is left out it’s set to 0. If Length is left out it’s set to 10. Note that in MySQL 3.22 the Length includes the sign and the decimal point
DATE A date The supported range is ‘1000-01-01’ to ‘9999-12-31’. MySQL displays DATE values in ‘YYYY-MM-DD’ format
DATETIME A date and time combination The supported range is ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’. MySQL displays DATETIME values in ‘YYYY-MM-DD HH:MM:SS’ format
TIMESTAMP A timestamp The range is ‘1970-01-01 00:00:00’ to sometime in the year 2037. MySQL displays TIMESTAMP values in YYYYMMDDHHMMSS, YYMMDDHHMMSS, YYYYMMDD or YYMMDD format, depending on whether M is 14 (or missing), 12, 8 or 6, but allows you to assign values to TIMESTAMP columns using either strings or numbers. A TIMESTAMP column is useful for recording the date and time of an INSERT or UPDATE operation because it is automatically set to the date and time of the most recent operation if you don’t give it a value yourself
TIME A time The range is ‘-838:59:59’ to ‘838:59:59’. MySQL displays TIME values in ‘HH:MM:SS’ format, but allows you to assign values to TIME columns using either strings or numbers
YEAR A year in 2- or 4- digit formats (default is 4-digit) The allowable values are 1901 to 2155, and 0000 in the 4 year format and 1970-2069 if you use the 2 digit format (70-69). MySQL displays YEAR values in YYYY format, but allows you to assign values to YEAR columns using either strings or numbers. (The YEAR type is new in MySQL 3.22.)
CHAR A fixed-length string that is always right-padded with spaces to the specified length when stored The range of Length is 1 to 255 characters. Trailing spaces are removed when the value is retrieved. CHAR values are sorted and compared in case-insensitive fashion according to the default character set unless the BINARY keyword is given
VARCHAR A variable-length string. Note: Trailing spaces are removed when the value is stored (this differs from the ANSI specification) The range of Length is 1 to 255 characters. VARCHAR values are sorted and compared in case-insensitive fashion unless the BINARY keyword is given
TINYBLOB,
TINYTEXT
  A BLOB or TEXT column with a maximum length of 255 (2^8 – 1) characters
BLOB,
TEXT
  A BLOB or TEXT column with a maximum length of 65535 (2^16 – 1) characters
MEDIUMBLOB,
MEDIUMTEXT
  A BLOB or TEXT column with a maximum length of 16777215 (2^24 – 1) characters
LONGBLOB,
LONGTEXT
  A BLOB or TEXT column with a maximum length of 4294967295 (2^32 – 1) characters
ENUM An enumeration A string object that can have only one value, chosen from the list of values ‘value1’, ‘value2’, …, or NULL. An ENUM can have a maximum of 65535 distinct values.
SET A set A string object that can have zero or more values, each of which must be chosen from the list of values ‘value1’, ‘value2’, … A SET can have a maximum of 64 members

String Types

Name Max Size Space Taken
char(x) 255 bytes x bytes
varchar(x) 255 bytes x+1 bytes
tinytext 255 bytes x+1 bytes
tinyblob 255 bytes x+2 bytes
text 64k x+2 bytes
blob 64k x+2 bytes
mediumtext 1.6mb x+3 bytes
mediumblob 1.6mb x+3 bytes
longtext 4.2gb x+4 bytes
longblob 4.2gb x+4 bytes

Examples

Here is an example of a database commands using these field types. ach SQL command ends in a semi-colon. The following commands are issued below:

  1. Create a temp database
  2. Make the temp database the current database
  3. Create a table using some of the variables listed in the table above.

create database temp;

use database temp;

create table testTable
(testName varchar(30),testDate text,
testGiver char(30));

Integer Types

Type Range Space Taken
tinyint -128 to 127 1 byte
smallint -32768 to 32767 2 bytes
medium int -8388608 to 8388607 3 bytes
int -2147483648 to 2147483647 4 bytes
bigint -9223372036854775808 to 9223372036854775807 8 bytes
float(m,d) varies 4 bytes
double(m,d) varies 8 bytes
decimal varies M + 2 bytes

Date Types

Type Format Zero Value
datetime yyyy-nn-dd hh:mm:ss 0000-00-00 00:00:00
date yyyy-mm-dd 0000-00-00
time hh:mm:ss 00:00:00
year yyyy 0000
timestamp varies 0000000000

The various timestamp formats are:

Type Format
timestamp(14) yyyymmddhhmmss
timestamp(12) yymmddhhmmss
timestamp(10) yymmddhhmm
timestamp(8) yyyymmdd
timestamp(6) yymmdd
timestamp(4) yymm
timestamp(2) yy

Other types

In addition to the types mentioned there are two other types worth mentioning. The enum type defines a set of values, of which, only one may be chosen.

fieldname enum(‘a’,’b’,’c’)

In addition to enum, the set field type also lets you define a set of values. However, with set, more than one value in the list may be selected.

Column Modifiers

Name Applicable Types
auto_increment All int types
binary char, varchar
default all except blob and text
not null all
null all
primary key all
unique all
unsigned Numeric types
zerofill Numeric types

Auto_increment makes the field an automatic counter.

Binary makes the strings stored case sensitive. By default the values stored in the char, varchar types are not case sensitive when searched.

Default allows you specify a default value for a field

Below is a create statement that uses a number of modifiers:

create table testTable
(testID int not null primary key auto_increment,
testName varchar(30),
testDate datetime,
testGiver varchar(30));

Print Friendly

Comments

comments

Bài viết liên quan