Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Types of Data2:45 with Andrew Chalkley
There are good reasons why your data should be categorized in to different types. In this video we'll explore some of them.
Types of Data
Here's a slightly larger list of data types than in the video.
- Text Type Examples
- Numeric Type Examples
- Date Type Examples
Here's documentation sites for some other databases where you can see the similarities and differences in data types.
Table columns are defined in the schema to have certain data types.
A data type describes the value to be stored.
There are several data types you'll come across on a regular basis.
There are text types, numeric types and data types.
Text types are good for storing names or descriptions of things.
Numeric types are good for storing prices, ages, and quantities of things.
Dates are good for anything time related.
While this isn't a comprehensive list of data types, text, numeric, and
date types are a good starting point.
The reason we use types will become more important as we write SQL later on.
To help you envisage how its schema enforces types and
columns, you can think of the schema as a coin sorter.
A coin sorter is used to sort different types of coins into columns.
The coin sorter ensures that only the correct coin types go
into the correct column.
Like the coin sorter,
the database schema prescribes what data should go into each column of the table.
You may be asking, why even do this?
Let's look at some examples to see why it's important to have data types.
Imagine everything in this product table was just text.
The name, price, stock count, are all text.
If I wanted to sort by the current stock count, what result do you think I'd get?
Well, it'd be sorted like this.
This is because the stock count is sorted alphabetically, and not numerically.
Let's change the price and the stock count to a numeric type and
sort by stock count again.
That looks better.
Having correct data types in your database not only helps with sorting but
it also comes in handy for
many other applications such as generating the sum of all sales figures.
Generating a sum and
other mathematical operations can only be done by using with a numeric type.
Trying to do math with text is impossible.
In the case of an events table for an event planner where the new data is added
all the time, the data is retrieved in the order it was entered by default.
If the date of the event column was just text and
you attempted to sort it, it would look like this.
However if the date of the event was correctly at date type,
it would sort like this.
Understanding that column date types can affect how data is retrieved
helps people design the schema for the database.
While we're not designing the schema ourselves in this course,
understanding data types will also help us understand
how we compose the SQL queries throughout this course.
You need to sign up for Treehouse in order to download course files.Sign up