6 min read
Overview While creating a table, each column in that table is required to have a name and a data type. The data type of a

By DIBYAJYOTI PANDA

Overview

While creating a table, each column in that table is required to have a name and a data type. The data type of a column defines what type of values the entire column should hold; integer, character, date, time, binary, and so on. E.g in a student table if you have declared the student_id as integer type, then every student’s id should be only integer type. If you insert a string value then it will report error. Or the database may go into an invalid state.

Note: Data types might have different names in different database. And even if the name is same, the size and other details may be different! Always check the documentation. I have also discussed those points in another section of my blog. Here we have discussed only some common data types that is used in most DBMS and you will get an overall knowledge to deal with SQL.

String data type

Data typeDescription
CHAR(size)A FIXED length string (can contain letters, numbers, and special characters). The size parameter specifies the exact length in characters – can be from 0 to 255. Default is 1
VARCHAR(size)Mostly same as CHAR type but not having fixed length. The size parameter specifies the maximum length in characters – can be from 0 to 65535
BINARY(size)Equal to CHAR(), but stores binary byte strings. The size parameter specifies the exact length in bytes. Default is 1
VARBINARY(size)Equal to VARCHAR(), but stores binary byte strings. The size parameter specifies the maximum length in bytes.
TINYBLOBFor BLOBs (Binary Large OBjects) which is a collection of binary data stored as a single entity. E.g images, audio, other multimedia objects or executable files. Max length: 255 bytes
TINYTEXTHolds a string with a maximum length of 255 characters
TEXT(size)Holds a string with a maximum length of 65,535 bytes
BLOB(size)For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data
MEDIUMTEXTHolds a string with a maximum length of 16,777,215 characters
MEDIUMBLOBFor BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data
LONGTEXTHolds a string with a maximum length of 4,294,967,295 characters
LONGBLOBFor BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of data
ENUM(val1, val2, val3, …)A string object that can have only one value, chosen from 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. The values are sorted in the order you enter them. E.g <span class="pln">CREATE TABLE </span><span class="typ">Foo</span> <span class="pun">(</span><span class="pln"> color ENUM</span><span class="pun">(</span><span class="str">'red'</span><span class="pun">,</span> <span class="str">'green'</span><span class="pun">,</span> <span class="str">'blue'</span><span class="pun">,</span> <span class="str">'yellow'</span><span class="pun">)</span> <span class="pun">);  This column allows you to store one of the values in the enum list, but no other value. It actually act as a constraint on values entered into the column. An error occurs for values that do not satisfy these conditions. This is analogical to radio button.
SET(val1, val2, val3, …)This is mostly same as ENUM. Only difference is it can take 0ne or more values, chosen from the list of predefined values. You can list up to 64 values in a SET list. This is analogical to Check box.

Numeric data types

Data typeDescription
BIT(size)A bit-value type. The number of bits per value is specified in size. The size parameter can hold a value from 1 to 64. The default value for size is 1.
TINYINT(size)A very small integer. Signed range is from -128 to 127. Unsigned range is from 0 to 255. The size parameter specifies the maximum display width (which is 255)
BOOLZero is considered as false, nonzero values are considered as true.
BOOLEANEqual to BOOL
SMALLINT(size)A small integer. Signed range is from -32768 to 32767. Unsigned range is from 0 to 65535. The size parameter specifies the maximum display width (which is 255)
MEDIUMINT(size)A medium integer. Signed range is from -8388608 to 8388607. Unsigned range is from 0 to 16777215. The size parameter specifies the maximum display width (which is 255)
INT(size)A medium integer. Signed range is from -2147483648 to 2147483647. Unsigned range is from 0 to 4294967295. The size parameter specifies the maximum display width (which is 255)
INTEGER(size)Equal to INT(size)
BIGINT(size)A large integer. Signed range is from -9223372036854775808 to 9223372036854775807. Unsigned range is from 0 to 18446744073709551615. Thesize parameter specifies the maximum display width (which is 255)
FLOAT(sized)A floating point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter. This syntax is deprecated in MySQL 8.0.17, and it will be removed in future MySQL versions
FLOAT(p)A floating point number. MySQL uses the p value to determine whether to use FLOAT or DOUBLE for the resulting data type. If p is from 0 to 24, the data type becomes FLOAT(). If p is from 25 to 53, the data type becomes DOUBLE()
DOUBLE(sized)A normal-size floating point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter
DOUBLE PRECISION(sized)
DECIMAL(sized)An exact fixed-point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter. The maximum number for size is 65. The maximum number for d is 30. The default value for size is 10. The default value for d is 0.
DEC(sized)Equal to DECIMAL(size,d)

Note: All the numeric data types may have an extra option: UNSIGNED or ZEROFILL. If you add the UNSIGNED option, MySQL disallows negative values for the column. If you add the ZEROFILL option, MySQL  pads the displayed value of the field with zeros.

Date and Time data types

Data typeDescription
DATEA date. Format: YYYY-MM-DD. The supported range is from ‘1000-01-01’ to ‘9999-12-31’
DATETIME(fsp)A date and time combination. Format: YYYY-MM-DD hh:mm:ss. The supported range is from ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’. Adding DEFAULT and ON UPDATE in the column definition to get automatic initialization and updating to the current date and time. fsp is the fractional seconds precision that ranges from 0 to 6. fsp value helps in number of digits after decimal point and rounding up the fractional part. See an example here.
TIMESTAMP(fsp)A timestamp. TIMESTAMP values are stored as the number of seconds since the Unix epoch (‘1970-01-01 00:00:00’ UTC). Format: YYYY-MM-DD hh:mm:ss. The supported range is from ‘1970-01-01 00:00:01’ UTC to ‘2038-01-09 03:14:07’ UTC. Automatic initialization and updating to the current date and time can be specified using DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP in the column definition
TIME(fsp)A time. Format: hh:mm:ss. The supported range is from ‘-838:59:59’ to ‘838:59:59’
YEARA year in four-digit format. Values allowed in four-digit format: 1901 to 2155, and 0000.
MySQL 8.0 does not support year in two-digit format.
Rate this post
3.5/5

Spread the words

Share on facebook
Share on google
Share on twitter
Share on linkedin