Introduction to Data Types: Numeric vs. String

What is a Data Type?

Defining the Rules

In a database, a Data Type is a constraint that tells the system what kind of information can live in a column. It ensures that your data remains clean, organized, and efficient.

Welcome to the foundation of database design. Before we can store data, we must define the rules for it. A data type is like a container shape; it specifies exactly what fits inside. This ensures we don't waste storage space, keeps our data accurate by blocking invalid inputs, and allows the database to process numbers and text at lightning speeds.

Numeric vs. String Families

The Two Major Families

Most SQL data falls into two categories: Numeric and String.

Numeric Types

String Types

Almost all data you'll work with belongs to one of two families. On the left, we have Numeric types. Use these for anything you might need to add, subtract, or average—like prices or counts. On the right are String types. These are for descriptive text like names, or codes that might contain letters and symbols.

Deep Dive: Numeric Constraints

Optimizing Numbers

When working with numbers, we can use modifiers like UNSIGNED to maximize our range.

For example, SMALLINT UNSIGNED fits values from 0 up to 65,535.

When defining numeric columns, we want to be precise. Normally, an integer splits its range between negative and positive values. However, if we know a value will never be negative—like a Movie ID—we use the UNSIGNED modifier. This shifts the entire range to the positive side, allowing a SMALLINT UNSIGNED to store up to 65,535 unique values perfectly.

Deep Dive: CHAR vs. VARCHAR

Handling Text

Strings come in two main flavors depending on how they use space.

Not all text is stored the same way. VARCHAR is like an elastic band; if you define it as 50 characters but only use 10, it only takes up 10 units of space. CHAR, however, is like a rigid box. A CHAR(4) always takes up 4 units, which is perfect for standardized codes like movie ratings.

The Sorting Pitfall

Why Types Matter for Sorting

If you store numbers as a String, the database sorts them alphabetically, not numerically.

Try sorting the lists below to see the difference.

A common mistake is storing numbers as strings. Try clicking the 'Sort' button on both columns. Notice how the Numeric column sorts by value, but the String column puts '100' before '25' because it only looks at the first digit. This is why prices and counts must always be Numeric!

Building the Movie Table

Practical Application

Let's apply what we've learned to our Movies table design:

Let's design our Cinema database. For MovieID, we expect 60,000 movies, so SMALLINT UNSIGNED is the perfect fit. Titles vary greatly, so VARCHAR(50) saves space for short names while allowing long ones. Finally, for Ratings like 'G' or 'PG13', we use CHAR(4) to keep our codes consistent.

Data Type Consultant

You are a database consultant. A client needs to store US Zip Codes (e.g., '02134'). Which family should they use and why?

Let's test your judgment. A client wants to store Zip Codes. Think about the rules we discussed: do we do math on them? Do leading zeros matter? Type your recommendation and explain your reasoning.