In our previous lesson, we learned how to create a table and we looked at some basic data types.
In our first lesson of this chapter, we learned how to create a table and we looked at some basic data types.
To recap, when we create a table, we define the columns and the data type for each column. Data types are one of the key components of a table and they specify the type of data that can be stored in a column.
To recap, when we create a table, we define the columns and the data type for each column. Data types are one of the key components of a table and they enforce the type of data that can be stored in a column.
> This lesson will be a quick overview of the common data types without going too deep into the details. The next two lessons will be more detailed and will cover some of the important data types in more detail.
## Basic Data Types
Data types can be categorized into these basic categories:
| Numeric | Store numeric values e.g. amounts, counts, ratings, measurements, sizes, salaries, etc. |
| Character | Store textual data of varying or fixed lengths e.g. names, addresses, descriptions, etc. |
| Temporal | Stores date, time, and timestamp (has a date and time) values e.g. dates, times, timestamps, etc. |
| Boolean | Store true/false values e.g. `True` or `False`. Boolean values can be considered as a numeric type (1 and 0). |
Different databases have different data types, but all the database support some common data types such as integers, floating-point numbers, strings, dates, and booleans. The table below shows the list of common data types with their description and examples.
| `INTEGER` | Whole numbers without decimal points e.g. 1, 2, 3, etc. |
| `FLOAT` | Numbers with decimal points e.g. 1.99, 2.99, 3.99, etc. |
| `DOUBLE` | Like `FLOAT` but with more precision e.g. 1.79769313486231 |
| `VARCHAR` | Variable length string of text e.g. 'Hello, World!' |
| `CHAR` | Fixed length string of text e.g. 'Hello' |
| `DATE` | Date in the format YYYY-MM-DD e.g. 2024-01-01 |
| `BOOLEAN` | `True` or `False` |
It's important to carefully analyze the data you are storing and choose the appropriate data type. For example, if you are storing a person's age, you would use an integer data type. A person's weight should be stored in a floating-point number data type, while the amount of money would use a decimal data type.
> We will be revisiting floating-point and decimal data types in more detail in the next lesson. There are some important differences between them that you should be aware of.
## Let's look at an Example
> Feel free to run the code below in the editor on the right and look at the `Schema` tab to see the data types for each column.
@ -81,16 +85,20 @@ On the other hand, `CHAR (250)` will **always** allocate 250 bytes of space for
#### What happens if we store a string that is longer than the maximum length?
The behavior of the database in such cases depends on the database system. In MySQL, if we try to store a string that is longer than the maximum length, the database will truncate the string to the maximum length and store the truncated string while showing a warning.
The behavior of the database in such cases depends on the database system. In MySQL, if we try to store a string that is longer than the maximum length, the database will truncate the string to the maximum length and store the truncated string while showing a warning. However, in PostgreSQL, if we try to store a string that is longer than the maximum length, the database will throw an error.
```
ERROR: value too long for type character varying(..)
```
### `FLOAT` vs `DOUBLE` for the `weight` column
The difference between `FLOAT` and `DOUBLE` data types in MySQL is that `FLOAT` occupies 32 bits of memory (4 bytes) and generally provides precision up to about 7 decimal digits, while `DOUBLE` occupies 64 bits of memory (8 bytes) and can achieve precision of about 15-16 decimal digits.
The difference between `FLOAT` and `DOUBLE`(called `DOUBLE PRECISION` in PostgreSQL) data types is that `FLOAT` occupies 32 bits of memory (4 bytes) and generally provides precision up to about 7 decimal digits, while `DOUBLE` occupies 64 bits of memory (8 bytes) and can achieve precision of about 15-16 decimal digits.
In our example, the weight values such as `73.99`, `65`, `92.06`, etc., have only up to 2 decimal places. Therefore, choosing `FLOAT` is more memory-efficient than `DOUBLE` while still maintaining sufficient accuracy for this use case.
---
> We will be revisiting floating-point and decimal data types in more detail in the next lesson. There are some important differences between them that you should be aware of.
In this lesson, we learned about the common data types and saw some examples to understand how to choose the appropriate data type for a column. While this is should give you a good starting point, there is a lot more to learn about data types and you should always refer to the documentation of the database you are using.
In the next lesson, we will learn more about the data types, look at MySQL specific data types and see how to use them in a table.
In the next lesson, we will learn more about numeric data types, how to use them in a table, and go through some common pitfalls.
Before we dive into data types, I would like to take a moment to explain what SQLite is and how it differs from traditional databases like MySQL, PostgreSQL, etc. More specifically, we will look at the differences in data types between SQLite and other databases.
> ### Optional Lesson
>
> You really don't need to know anything about SQLite. I just wanted to take a moment to talk about it because it's a very popular database, and you might come across it in your career.
>
> Throughout this course and our coding environment, I will be using PostgreSQL, which will help you understand the concepts of SQL in a more traditional database.
## What is SQLite?
SQLite is a lightweight, self-contained database engine known for its simplicity and portability. Unlike MySQL and PostgreSQL, which use a client-server model, SQLite works directly with a single database file on disk. This design removes the need for server management, making it easy to set up and maintain.
@ -22,7 +28,7 @@ For example, the SQL statement below is perfectly valid in SQLite:
```sql
CREATE TABLE users (
id INTEGER,
name VARCHAR(255),
name TEXT,
age INTEGER
);
@ -44,16 +50,18 @@ The output will be:
| --- | -------- | ----------- |
| 1 | John Doe | Twenty Five |
You should also note that SQLite supports only a few data types: `INTEGER`, `REAL`, `TEXT`, `BLOB`, and `NULL`. Other data types such as `DECIMAL`, `FLOAT`, `VARCHAR`, etc., are not supported, although it will not complain if you try to assign them to a column.
For more details, check the [SQLite data types](https://www.sqlite.org/datatype3.html) and [why it uses dynamic typing](https://www.sqlite.org/flextypegood.html) in the official documentation.
### Static Typing in Other Databases
Databases like MySQL and PostgreSQL use static typing, which enforces data types when columns are created. This means a column defined as `INTEGER` can only store numeric values, not strings.
Databases like MySQL and PostgreSQL use static typing, which enforces data types assigned to columns. This means a column defined as `INTEGER` can only store numeric values, not strings.
If we run the above example in MySQL we will get an error:
If we run the above example in PostgreSQL we will get an error:
```
ERROR 1366 (HY000) at line 9: Incorrect integer value: 'Twenty Five' for column 'age' at row 1
ERROR: invalid input syntax for type integer: "Twenty Five"
```
Since SQLite has different typing rules compared to other databases, I would recommend using an online service like [SQLFiddle](https://sqlfiddle.com/) or [DB Fiddle](https://www.db-fiddle.com/) to test out the examples in the next two lessons about data types.
With that said, let's move on to the next lesson about data types.
@ -14,7 +14,7 @@ Now that we have got the theoretical part out of the way, let's dive into the pr
We don't expect you to have a database setup on your local machine. All our lessons will have the coding environment setup with the database ready for you to use and practice. We will be using a hypothetical pre-populated databases throughout this course to help you learn SQL. Additional things to note about the coding environment:
- Our database environment is not persistent. This means that if you close the browser or refresh the page, the database will be reset to its original state.
- We are using SQLite for our coding environment. The concepts we will be covering, however, are the same for any other database and you are not required to know anything about SQLite to follow this course.
- We are using PostgreSQL for our coding environment. The concepts we will be covering, however, are the same for any other database and you are not required to know anything about PostgreSQL to follow this course.