MySQL_ Data types
MySQL_ Data types
Once you have identified all of the tables and columns that the database will need, you should determine each
field's MySQL data type. When creating the database, as you will do in the next chapter, MySQL requires that
you define what sort of information each field will contain. There are three primary categories, which is true for
almost every database software:
● Text
● Numbers
● Dates and times
Within each of these, there are a number of variants—some of which are MySQL-specific—you can use.
Choosing your column types correctly not only dictates what information can be stored and how, but also
affects the database's overall performance. Table 3.2 lists most of the available types for MySQL, how much
space they take up, and a brief description.
Table 3.2 Here are most of the available column types for use with MySQL databases.
MySQL Datatypes
Ty p e Size Description
Many of the types can take an optional Length attribute, limiting their size (the square brackets, [], indicate an
optional parameter to be put in parentheses, while parentheses themselves indicate required arguments).
Further, the number types can be UNSIGNED—limiting the column to positive numbers or zero—or be defined
as ZEROFILL, which means that any extra room will be padded with zeroes (ZEROFILLs are also automatically
UNSIGNED). The various date types have all sorts of unique behaviors, which are documented in the manual
at www.mysql.com/doc/D/A/DATETIME.html. You'll primarily use the DATE and TIME fields without
modification, so you need not worry too much about their intricacies. There are also two extensions of the
TEXT types that result in a different behavior—ENUM and SET—which allow you to define a series of
acceptable values when creating the table. An ENUM field can have only one of a possible several thousand
values, while SET allows for several of up to 64 possible values. There are two caveats with ENUM and SET:
These types are not supported by other databases, and their usage undermines normalization.
● There is some debate as to the merits of these two similar types. Both store strings
and can be set with a fixed maximum length. One primary difference is that anything
stored as a CHAR will always be stored as a string the length of the column (using
spaces to pad it). Conversely, VARCHAR strings will be only as long as the stored
string.
The two implications of this are
● VARCHAR columns tend to take up less disk space.
● Unless you are using the InnoDB table types (see Chapter 11,
"Advanced MySQL," for more information), CHAR columns are faster
to access than VARCHAR.
● Granted, the speed and disk space differences between the two types may be
imperceptible in most cases and is therefore not worth dallying over.
There is also a third, minor difference between these two: MySQL trims off extra
spaces from CHAR columns when data is retrieved and from VARCHAR when it's
inserted.
4. Set the maximum length for text and number columns as well as other attributes such as
UNSIGNED (Table 3.3).
Rather than going over how I defined all 21 columns and why, I've listed the properties I came up
with in Table 3.3. Different developers have different preferences, but the most important factor is
to tailor each setting to the information at hand rather than using generic (and inefficient) TEXT
and INT types at all times.
Table 3.3 An often overlooked aspect of database design is defining the optimal type for each
field.
Accounting Database
C o l um n N a m e Ta b l e Co lum n Ty pe
Tips
● Many of the data types have synonymous names: INT and INTEGER, DEC and DECIMAL, etc.
● The TIMESTAMP field is automatically set when an INSERT or UPDATE occurs, even if no value
is specified for the field. If a table has multiple TIMESTAMP columns, only the first one will be
updated when an INSERT or UPDATE is performed.
● There is also a BLOB type, which is a variant on TEXT, and allows for storing binary files in a
table. I'll demonstrate this in action in Chapter 9, "Database Programming Techniques."