Okay, let's build on our understanding of tables, columns, and rows. When we define a column in a table, we don't just give it a name; we also specify what kind of data it's allowed to hold. This specification is known as the column's data type.
Think of data types as rules for your columns. Just like you wouldn't store soup in a sieve, you wouldn't store text like "Hello World" in a column meant only for numbers. Defining data types is fundamental because it ensures:
While the exact names and features can vary slightly between different database systems (like PostgreSQL, MySQL, SQL Server, SQLite), the core concepts and common types are largely consistent. Here are the main categories you'll encounter frequently:
These types handle numerical data.
101
, 102
), quantities (5
, 100
), or counts. They can be positive or negative. Variations like SMALLINT
, TINYINT
, BIGINT
exist to store smaller or larger ranges of integers, optimizing storage space.DECIMAL(10, 2)
can store a number up to 10 digits long, with 2 digits reserved for the fractional part (e.g., 12345678.99
).These types store text data.
n
. For example, VARCHAR(255)
can store any text string from 0 up to 255 characters long. The database only uses the storage needed for the actual characters stored, plus a small overhead. Use this when the length of the text varies significantly, like for names, addresses, or descriptions.n
. If the string you insert is shorter than n
, the database typically pads it with spaces to reach the exact length n
. If the string is longer, it might be truncated or cause an error, depending on the database system. CHAR
can be slightly faster for retrieval in some cases if the data length is always the same (e.g., storing two-letter country codes like US
, CA
, GB
using CHAR(2)
).VARCHAR
typically supports comfortably. The exact length limits for TEXT
vary significantly between database systems but are generally very large.These types are specifically designed to store temporal information.
2023-10-27
.14:35:10
.2023-10-27 14:35:10.123
. These types are often used for logging events, like recording when an order was placed or a record was last updated. Some systems store timestamp information relative to UTC (Coordinated Universal Time), which helps manage time zones.TRUE
or FALSE
values. Some database systems don't have a specific BOOLEAN
type and might use a small integer type instead (like TINYINT(1)
, where 0
represents FALSE
and 1
represents TRUE
). Used for flags or status indicators, like is_active
, has_paid
, or email_verified
.Let's visualize how these types might be applied in a simple Customers
table.
A conceptual diagram showing columns in a
Customers
table and their corresponding SQL data types.customer_id
is typically anINTEGER
and serves as a unique identifier (Primary Key). Names and email useVARCHAR
for variable-length text.signup_date
usesDATE
.total_spent
usesDECIMAL
for precise currency values, andis_premium
usesBOOLEAN
for a true/false status.
Choosing the appropriate data type when designing a table is an important step. It ensures your data is stored correctly and efficiently, making it easier and more reliable to query and analyze later using SQL. As we move forward, you'll see how these types influence the way we write our queries, especially when filtering and performing calculations.
© 2025 ApX Machine Learning