mySQLMySQL

MySQL is an open source relational database management system (RDBMS) with a client-server model. It is the go-to database to store web-based content and includes web applications like Facebook, Twitter, YouTube, Google, and WordPress.

Database

A database is a collection of data that is stored and organized for easy retrieval to an application. Data are stored in tables and each table relates to other tables in the database in some manner (hence the term relational database).

SQL

Structured Query Language is the primary programming language to interact with the database.

Data Types

Data types define what kind and range of data can be stored in a given field or column. Properly defining the field in a table is important in the performance of the database. MySQL uses a variety of data types which fall into three categories. Much of this data type information was taken from tutorialspoint.com:

  • Numeric
    • Integers: numbers without fractions. Can be signed (positive or negative) or unsigned (positive). Commonly used for ID field and counts.
    • Decimal: numbers with fractions requiring exact precision. Can be signed (positive or negative) or unsigned (positive). Commonly used for monetary fields. You must specify the number of significant digits allowed overall (x) and after the decimal point (y) in parentheses.
    • Float / Doubles: numbers with fractions not requiring exact precision. Can be signed (positive or negative) or unsigned (positive). Commonly used for monetary fields. Commonly used for all real numbers except monetary fields.
  • Date and Time
  • String Types

When creating a table to store data you must define the type of data to be stored. The character type TEXT is useful for storing long-form text strings that can take from 1 byte to 4 GB. We often find the TEXT data type for storing article body in news sites, product description in e-commerce sites.

Numeric Data Types
  • INT − A normal-sized integer that can be signed or unsigned. If signed, the allowable range is from -2147483648 to 2147483647. If unsigned, the allowable range is from 0 to 4294967295. You can specify a width of up to 11 digits.
  • TINYINT − A very small integer that can be signed or unsigned. If signed, the allowable range is from -128 to 127. If unsigned, the allowable range is from 0 to 255. You can specify a width of up to 4 digits.
  • SMALLINT − A small integer that can be signed or unsigned. If signed, the allowable range is from -32768 to 32767. If unsigned, the allowable range is from 0 to 65535. You can specify a width of up to 5 digits.
  • MEDIUMINT − A medium-sized integer that can be signed or unsigned. If signed, the allowable range is from -8388608 to 8388607. If unsigned, the allowable range is from 0 to 16777215. You can specify a width of up to 9 digits.
  • BIGINT − A large integer that can be signed or unsigned. If signed, the allowable range is from -9223372036854775808 to 9223372036854775807. If unsigned, the allowable range is from 0 to 18446744073709551615. You can specify a width of up to 20 digits.
  • FLOAT(M,D) − A floating-point number that cannot be unsigned. You can define the display length (M) and the number of decimals (D). This is not required and will default to 10,2, where 2 is the number of decimals and 10 is the total number of digits (including decimals). Decimal precision can go to 24 places for a FLOAT.
  • DOUBLE(M,D) − A double precision floating-point number that cannot be unsigned. You can define the display length (M) and the number of decimals (D). This is not required and will default to 16,4, where 4 is the number of decimals. Decimal precision can go to 53 places for a DOUBLE. REAL is a synonym for DOUBLE.
  • DECIMAL(M,D) − Decimal is the optimal data type for storing monetary values. It is an unpacked floating-point number that cannot be unsigned. In the unpacked decimals, each decimal corresponds to one byte. Defining the display length (M) and the number of decimals (D) is required. For example DECIMAL(5,2) means that the amount the column can store 5 digits with 2 decimal places; therefore, the range of the amount column is from 999.99 to -999.99. NUMERIC is a synonym for DECIMAL.
Date and Time Types
  • DATE − A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. For example, December 30th, 1973 would be stored as 1973-12-30.
  • DATETIME − A date and time combination in YYYY-MM-DD HH:MM:SS format, between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. For example, 3:30 in the afternoon on December 30th, 1973 would be stored as 1973-12-30 15:30:00. Although this format requires 8 bytes of storage (twice that of TIMESTAMP) the DATETIME are human-readable straight out of the database so that if browsing your data, using phpMyAdmin for example, you can easily view the dates. One other reason that you may want to choose DATETIME over TIMESTAMP is that MySQL has  many built-in functions to add and subtract dates, and calculate the difference between dates.
  • TIMESTAMP − A timestamp between midnight, January 1st, 1970 and 19th January 2038. Visually it appears like the previous DATETIME format, only without the hyphens between numbers; 3:30 in the afternoon on December 30th, 1973 would be stored as 19731230153000 ( YYYYMMDDHHMMSS ). Timestamps are particularly useful if you’re using PHP, as the date() function will display them in your chosen format and you can calculate dates by adding or subtracting a number of seconds (86400 is equivalent to 24 hours etc.). One drawback of using TIMESTAMP is that they’re not human readable straight from the database. Would you know what date 1262986313 refers to? Another is that due to the 10-digit length, Unix timestamps may have problems at 3:14:07AM on 19th January 2038 when the value 9999999999 is reached!
  • TIME − Stores the time in a HH:MM:SS format.
  • YEAR(M) − Stores a year in a 2-digit or a 4-digit format. If the length is specified as 2 (for example YEAR(2)), YEAR can be between 1970 to 2069 (70 to 69). If the length is specified as 4, then YEAR can be 1901 to 2155. The default length is 4
String Types
  • CHAR(M) − A fixed-length string between 1 and 255 characters in length (for example CHAR(5)), right-padded with spaces to the specified length when stored. Defining a length is not required, but the default is 1.
  • VARCHAR(M) − A variable-length string between 1 and 255 characters in length. For example, VARCHAR(25). You must define a length when creating a VARCHAR field. In contrast to CHAR, VARCHAR values are stored as a one-byte length prefix plus data. For example, if you define a column as VARCHAR(255) and only insert 5 characters the storage requirement will only be 6 bytes. Therefore if your strings will be of variable length, the VARCHAR data type will require less storage
  • BLOB or TEXT − A field with a maximum length of 65535 characters used to store larger amounts of text. BLOBs are “Binary Large Objects” and are used to store large amounts of binary data, such as images or other types of files. Fields defined as TEXT also hold large amounts of data. The difference between the two is that the sorts and comparisons on the stored data are case sensitive on BLOBs and are not case sensitive in TEXT fields. You do not specify a length with BLOB or TEXT.
  • TINYBLOB or TINYTEXT − A BLOB or TEXT column with a maximum length of 255 characters. You do not specify a length with TINYBLOB or TINYTEXT.
  • MEDIUMBLOB or MEDIUMTEXT − A BLOB or TEXT column with a maximum length of 16777215 characters. You do not specify a length with MEDIUMBLOB or MEDIUMTEXT.
  • LONGBLOB or LONGTEXT − A BLOB or TEXT column with a maximum length of 4294967295 characters. You do not specify a length with LONGBLOB or LONGTEXT.
  • ENUM − An enumeration, which is a fancy term for list. When defining an ENUM, you are creating a list of items from which the value must be selected (or it can be NULL). For example, if you wanted your field to contain “A” or “B” or “C”, you would define your ENUM as ENUM (‘A’, ‘B’, ‘C’) and only those values (or NULL) could ever populate that field.
How to Select the Right Data Types

The best strategy for selecting the data type is to choose the smallest data type that matches the kind of data you have and allows for all the feasible values of your data.

For example, customer_id, in an example sales table is a whole number starting with 0. There may be 15,000 customers in the table. We may be tempted to select the SMALLINT datatype since it will allow for values between 0 and 65535. However, if we expect the number of customers to grow over 65535, we may want to choose MEDIUMINT unsigned which will allow for growth to 16,777,215 customers.

It is possible to allow MySQL to automatically select the data type for you based on your sample data. But it may choose the wrong data type, be too constrained, or too conservative. So it is important that you spend some time to choose the best data type for your data and growth of the database.