mysql data types

mysql supports number of data types, which can be classified the following four broad categories.

  • numeric types
  • string types
  • date and time types
  • complex types

numeric types

mysql numeric data types can broadly be divided into two categories, one for integers and the other for floating-point or decimal numbers.

note :- *the integer types have an extra option called unsigned.normally, the integer goes from an negative to positive value.adding the unsigned attribute will move that range up so it starts at zero instead of a negative number.

data typedescription
tinyint(size)-128 to 127 normal.0 to 255 unsigned*.the maximum number of digits may be specified in parenthesis.
smallint(size)-32768 to 32767 normal.0 to 65535 unsigned*.the maximum number of digits may be specified in parenthesis.
mediumint(size)-8388608 to 8388607 normal.0 to 16777215 unsigned*.the maximum number of digits may be specified in parenthesis.
int(size)-2147483648 to 2147483647 normal.0 to 4294967295 unsigned*.the maximum number of digits may be specified in parenthesis
bigint(size)-9223372036854775808 to 9223372036854775807 normal. 0 to 18446744073709551615 unsigned*.the maximum number of digits may be specified in parenthesis
float(size,d)a small number with a floating decimal point.the maximum number of digits may be specified in the size parameter.the maximum number of digits to the right of the decimal point is specified in the d parameter
double(size,d)a large number with a floating decimal point.the maximum number of digits may be specified in the size parameter.the maximum number of digits to the right of the decimal point is specified in the d parameter
decimal(size,d)a double stored as a string, allowing for a fixed decimal point.the maximum number of digits may be specified in the size parameter.the maximum number of digits to the right of the decimal point is specified in the d parameter.

String Data Types

text types:

data typedescription
char(size)holds a fixed length string (can contain letters, numbers, and special character).the fixed size is specified in parenthesis.can store up to 255 characters
varchar(size)holds a variable length string (can contain letters, numbers, and special characters).the maximum size is specified in parenthesis.can store up to 255 characters.
note:if you put a qreater value than 255 it will be converted to a text type
tinytextholds a string with a maximum length of 255 characters
textholds a string with a maximum length of 65,535 characters
blobblobs(binary large objects).holds up to 65,535 bytes of data
mediumtextholds a string with a maximum length of 16,777,215 characters
mediumblobblobs(binary large objects).holds up to 16,777,215 bytes of data
longtextholds a string with a maximum length of 4,294,967,295 bytes of data
longblobblobs(binary large objects).holds up to 4,294,967,295 bytes of data
enum(x,y,z etc.)let you enter a list of possible values. you can list up to 65535 values in an enum list.if a value is inserted that is not in the list, a blank value will be inserted.

you enter the possible values in this format:enum('x','y','z').

note: the values are sorted in the order you enter them.
setset may contain up to 64 list items and can store more than one choice.

Date Time Data Types

note :-*even if datetime and timestamp return the same format, they work very differently.in an insert or update query, the timestamp automatically set itself to the current date and time.timestamp also accepts various formats, like yyyymmddhhmmss,yymmddhhmmss,yyyymmdd,or yymmdd.

data typedescriptionformat
date()a date value.
note: the supported range is from '1000-01-01' to '9999-12-31'
yyyy-mm-dd
time()a time value.
note: the supported range is from '-838:59:59' to '838:59:59'
hh:mm:ss
year()a year in two-digit or four-digit format.
note: values allowed in two-digit format: 70 to 69
          values allowed in four-digit format:1901 to 2155.
yyyy
datetime()*a date and time combination value.
note: the supported range is from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
yyyy-mm-dd hh:mm:ss
timestamp()*a timestamp.timestamp values are stored as the number of seconds since the unix epoch('1970-01-01 00:00:00' utc).
note: the supported range is from '1970-01-01 00:00:01'utc to '2038-01-19 03:14:07'utc
yyyy-mm-dd hh:mm:ss

complex types :the mysql support two complex types enum and set.

enum type permits selection of only a single value from a collection of allowed values.

set type permits selection of any arbitrary number of values from the available collection.