From 38860d35e50540cdc51e909fd34d95aa5ac12ec2 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Thu, 26 Dec 2024 16:51:55 +0000 Subject: [PATCH] Add data types in sqlite --- .../lessons/common-data-types.md | 2 +- .../lessons/data-types-in-sqlite.md | 61 +++++++++++++++++++ .../lessons/more-on-data-types.md | 26 ++++++++ .../introduction/lessons/next-steps.md | 4 +- 4 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 src/data/courses/sql/chapters/data-definition-language/lessons/data-types-in-sqlite.md create mode 100644 src/data/courses/sql/chapters/data-definition-language/lessons/more-on-data-types.md diff --git a/src/data/courses/sql/chapters/data-definition-language/lessons/common-data-types.md b/src/data/courses/sql/chapters/data-definition-language/lessons/common-data-types.md index 144014b34..5b0461552 100644 --- a/src/data/courses/sql/chapters/data-definition-language/lessons/common-data-types.md +++ b/src/data/courses/sql/chapters/data-definition-language/lessons/common-data-types.md @@ -1,7 +1,7 @@ --- title: Common Data Types description: Learn about common data types in SQL. -order: 110 +order: 120 type: lesson-challenge --- diff --git a/src/data/courses/sql/chapters/data-definition-language/lessons/data-types-in-sqlite.md b/src/data/courses/sql/chapters/data-definition-language/lessons/data-types-in-sqlite.md new file mode 100644 index 000000000..f8639e04b --- /dev/null +++ b/src/data/courses/sql/chapters/data-definition-language/lessons/data-types-in-sqlite.md @@ -0,0 +1,61 @@ +--- +title: Data Types in SQLite +description: Learn about the differences between SQLite and other databases. +order: 110 +type: lesson-challenge +--- + +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. + +## 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. + +While SQLite supports standard SQL, it differs from other databases in how it handles data types. + +### Dynamic Typing in SQLite + +SQLite uses dynamic typing, meaning it doesn’t strictly enforce data types on columns. This allows storing any value, like a string, in an `INT` column which is something not allowed in databases with static typing, such as MySQL and PostgreSQL. + +For example, the SQL statement below is perfectly valid in SQLite: + +```sql +CREATE TABLE users ( + id INT, + name VARCHAR(255), + age INT +); + +-- Although the `age` column is `INT`, it can +-- store a string because of dynamic typing. +INSERT INTO users (id, name, age) +VALUES (1, 'John Doe', 'Twenty Five'); +``` + +If you run the following SQL statement: + +```sql +SELECT * FROM users; +``` + +The output will be: + +| id | name | age | +| --- | -------- | ----------- | +| 1 | John Doe | Twenty Five | + +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 `INT` can only store numeric values, not strings. + +If we run the above example in MySQL we will get an error: + +``` +ERROR 1366 (HY000) at line 9: Incorrect integer value: 'Twenty Five' for column 'age' at row 1 +``` + +--- + +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. diff --git a/src/data/courses/sql/chapters/data-definition-language/lessons/more-on-data-types.md b/src/data/courses/sql/chapters/data-definition-language/lessons/more-on-data-types.md new file mode 100644 index 000000000..cc25da05a --- /dev/null +++ b/src/data/courses/sql/chapters/data-definition-language/lessons/more-on-data-types.md @@ -0,0 +1,26 @@ +--- +title: More on Data Types +description: Learn about common data types in SQL. +order: 130 +type: lesson-challenge +--- + +As we discussed in the previous lesson, there are following four basic categories of data types in SQL: + +| Category | Description | +| --------- | ------------------------------------- | +| Numeric | Used for storing numerical values | +| Temporal | Used for storing date and time values | +| Character | Used for storing text values | +| Boolean | Used for storing boolean values | + +We only covered some basic data types for each i.e. + +| Category | Data Types | +| --------- | ------------------------ | +| Numeric | `INT`, `FLOAT`, `DOUBLE` | +| Character | `CHAR`, `VARCHAR` | +| Temporal | `DATE` | +| Boolean | `BOOLEAN` | + +In this lesson we will dive a bit deeper into each of these data types and cover some more advanced data types supported by MySQL (all of which have their equivalents in other databases as well). diff --git a/src/data/courses/sql/chapters/introduction/lessons/next-steps.md b/src/data/courses/sql/chapters/introduction/lessons/next-steps.md index 8dd7bba60..32965fcfc 100644 --- a/src/data/courses/sql/chapters/introduction/lessons/next-steps.md +++ b/src/data/courses/sql/chapters/introduction/lessons/next-steps.md @@ -12,6 +12,6 @@ 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. You don't need to know anything about SQLite to follow this course as the concepts we will be covering are the same for any other database. +- 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. -See you in the next chapter! \ No newline at end of file +See you in the next chapter!