Chapter 3. SQL Data Types

Previously, we defined "database" as an organized collection of information. Not only does that mean that data have to be organized according to a company's business rules, but also the database organization should reflect the nature of the information. Databases can store dollar amounts, quantities, names, date and time values, binary files, and more. These can be further classified by type, which reflects the "nature" of the data: numbers, characters, dates, and so on.

Note

"Data type" is a characteristic of a database table column that determines what kind of data it can hold. SQL:2003 defines "data type" as a set of representable values, where each value belongs to at least one data type.

But why do we need data types at all? Wouldn't it be easier to have one uniform data type and store everything, let's say, in the form of character strings?

There are many reasons why things aren't done this way. Some of them are historical. When relational databases were born in the late twentieth century, hard disk space and memory storage were at a premium, so the idea was to store everything as efficiently as possible. But existing programming languages had some built-in rules for how to store different types of data. For example, any English character (plus special characters and digits) could be represented using its ASCII equivalent, and the necessary storage for it was 1 byte (more about ASCII later in this chapter). Numbers are traditionally stored in the form ...

Get SQL Bible, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.