Learn MySQL data types like Numeric Types, Date and Time Types and String Types for building dynamic web sites.
Learn the three main basic data types used in MySQL and how to utilize them to build a more effective dynamic web site. Lots of web developers use MySQL but not all of them harness the power of these features.
The three main types of data types used in MySQL are
1) Numeric (Integer/Numbers/Money)
2) String (Text)
3) Date (Dates and Time)
It’s very important to pick the right data to achieve speed, effective storage and data retrieval. Here is a introduction to data types:
Numeric Data Types
In addition to int (Integer data type), MySQL also has provision for floating-point and double precision numbers. Each integer type can take also be UNSIGNED and/or AUTO_INCREMENT.
- TINYINT: very small numbers; suitable for ages. Actually, we should have used this data type for employee ages and number of children. Can store numbers between 0 to 255 if UNSIGNED clause is applied, else the range is between -128 to 127.
- SMALLINT: Suitable for numbers between 0 to 65535 (UNSIGNED) or -32768 to 32767.
- MEDIUMINT: 0 to 16777215 with UNSIGNED clause or -8388608 to 8388607.
- INT: UNSIGNED integers fall between 0 to 4294967295 or -2147683648 to 2147683647.
- BIGINT: Huge numbers. (-9223372036854775808 to 9223372036854775807)
- FLOAT: Floating point numbers (single precision)
- DOUBLE: Floating point numbers (double precision)
- DECIMAL:Floating point numbers represented as strings.
Date and Time Data types
Text Data Type
Text can be fixed length (char) or variable length strings. Also, text comparisions can be case sensitive or insensitive depending on the type you choose.
- CHAR(x): where x can range from 1 to 255.
- VARCHAR(x): x ranges from 1 – 255
- TINYTEXT: small text, case insensitive
- TEXT: slightly longer text, case insensitive
- MEDIUMTEXT: medium size text, case insensitive
- LONGTEXT: really long text, case insensitive
- TINYBLOB: Blob means a Binary Large OBject. You should use blobs for case sensitive searches.
- BLOB: slightly larger blob, case sensitive.
- MEDIUMBLOB: medium sized blobs, case sensitive.
- LONGBLOB: really huge blobs, case sensitive.
- ENUM: Enumeration data type have fixed values and the column can take only one value from the given set. The values are placed in parenthesis following ENUM declaration. An example, is the marital status column we encountered in employee_per table.
m_status ENUM("Y", "N")
Thus, m_status column will take only Y or N as values. If you specify any other value with the INSERT statement, MYSQL will not return an error, it just inserts a NULL value in the column.
- SET: An extension of ENUM. Values are fixed and placed after the SET declaration; however, SET columns can take multiple values from the values provided. Consider a column with the SET data type as
foods SET ("Apples", "Cookies", "Pies", "Chicken")
You can have 0 or all the four values in the column.
INSERT tablename (foods) values ("Apple", "Cookies");