Lesson 3 | Column data types |
Objective | Given certain specifications, create a table that holds the information needed for a customer record. |
Column Data Types
Given certain specifications, create a table that holds the information needed for a customer record.
Column Data Types
A key part of a column's definitions is the data type. The following list summarizes the most common SQL data types:
- BLOB: A Binary Large Object. This is any chunk of binary data such as a JPEG file, audio file, video file, or Word document. The database knows nothing about the internal structure of this
data so, for example, if the BLOB contains a Word document the database cannot search its contents.
- BOOLEAN: A true or false value.
- CHAR: A fixed-length string. Use this for strings that always have the same length such as two-letter state abbreviations or five-digit ZIP Codes.
- DATE: A month, date, and year such as February 29, 2012.
- DATETIME: A date and time such as 12:34pm February 29, 2012.
- DECIMAL(p, s): A fixed-point number where p (precision) gives the total number of digits and s (scale) gives the number of digits to the right of the decimal. For example, DECIMAL(6, 2) holds
numbers of the form 1234.56.
- INT: An integer value.
- NUMBER: A floating point number.
- TIME: A time without a date such as 3:14am.
- TIMESTAMP: A date and time.
- VARCHAR: A variable-length string. Use this for strings of unknown lengths such as names and street addresses.
Specific database products often provide extra data types and aliases for these types. They also sometimes use these names for different purposes.
For example, in different databases the INT data type might use
32 or 64 bits, and the database may provide other data types such as SMALLINT, TINYINT, BIGINT, and so forth to hold integers of different sizes.
MySQL Data Types
In general, all the popular database servers have the capacity to store the same types of data, such as strings, dates, and numbers. Where they typically differ is in the specialty data types, such as XML documents or very large text or binary documents.
Since this is an introductory book on SQL, and since 98% of the columns you encounter will be simple data types, this book covers only the character, date, and numeric data types.
Character Data
Character data can be stored as either fixed-length or variable-length strings; the difference is that fixed-length strings are right-padded with spaces and always consume the same number of bytes, and variable-length strings are not right-padded with spaces and don't always consume the same number of bytes.
When defining a character column, you must specify the maximum size of any string to be stored in the column. For example, if you want to store strings up to 20 characters in length, you could use either of the following definitions:
char(20) /* fixed-length */
varchar(20) /* variable-length */
The maximum length for char columns is currently 255 bytes, whereas varchar columns can be up to 65,535 bytes. If you need to store longer strings (such as emails, XML documents, etc.),
then you will want to use one of the text types (mediumtext and longtext), which I cover later in this section. In general, you should use the char type when all strings to be stored in the
column are of the same length, such as state abbreviations, and the varchar type when strings to be stored in the column are of varying lengths. Both char and varchar are used in a similar fashion in all the major database servers.
Create Table - Exercise
SQL Database Programming