diff --git a/src/data/courses/sql/chapters/data-definition-language/data-definition-language.md b/src/data/courses/sql/chapters/data-definition-language/data-definition-language.md new file mode 100644 index 000000000..618a6ab49 --- /dev/null +++ b/src/data/courses/sql/chapters/data-definition-language/data-definition-language.md @@ -0,0 +1,5 @@ +--- +title: Data Definition Language +description: Learn the basics of SQL, the language for querying databases. +order: 3 +--- diff --git a/src/data/courses/sql/chapters/data-definition-language/lessons/creating-tables.md b/src/data/courses/sql/chapters/data-definition-language/lessons/creating-tables.md new file mode 100644 index 000000000..227d0599a --- /dev/null +++ b/src/data/courses/sql/chapters/data-definition-language/lessons/creating-tables.md @@ -0,0 +1,113 @@ +--- +title: Creating Tables +description: Learn how to create tables in a database. +order: 100 +type: lesson-challenge +--- + +So far in this course, we have been querying data from tables that were already created for us. In reality, we need to create tables before we can store data in them and query them. + +In this lesson, we will learn how to create tables in a database. + +## What is a Table? + +We already covered what tables are in the previous lessons. But to recap, tables in relational databases are used to store data in a structured format. They are made up of rows and columns. Each row represents a record and each column represents a field. + +For example, let's say we want to store information about books in a database. We might create a table with the following columns: + +| id | title | author | price | +| --- | --------------------- | ------------------- | ----- | +| 1 | The Great Gatsby | F. Scott Fitzgerald | 10.99 | +| 2 | 1984 | George Orwell | 12.99 | +| 3 | To Kill a Mockingbird | Harper Lee | 8.99 | + +Another important thing to note is that each column/field has a data type. What is a data type? Data types identify the type of data that can be stored in a column. For example, a column with a data type of `INTEGER` can only store numeric values without decimals, a column with a data type of `VARCHAR` can store text values, and a column with a data type of `FLOAT` can store decimal values. We will learn more about data types in the next lesson. + +> Data types are a crucial part of SQL because they ensure that the data stored in a database is consistent and accurate. They also help the database engine optimize storage and performance. + +Alright, enough with the theory. Let's go ahead and create a table. + +## Creating a Table + +To create a table, we use the `CREATE TABLE` statement. The basic syntax for the `CREATE TABLE` statement is as follows: + +```sql +-- Syntax for creating a table +CREATE TABLE table_name ( + column1_name data_type, + column2_name data_type, + ... +); +``` + +Let's say we want to create a table to store information about books. We might create a table with the following columns: + +| id | title | author | price | year | +| --- | --------------------- | ------------------- | ----- | ---- | +| 1 | The Great Gatsby | F. Scott Fitzgerald | 10.99 | 1925 | +| 2 | 1984 | George Orwell | 12.99 | 1949 | +| 3 | To Kill a Mockingbird | Harper Lee | 8.99 | 1960 | + +Here is the SQL code to create this table: + +```sql +CREATE TABLE books ( + id INT, + title VARCHAR(255), + author VARCHAR(255), + price FLOAT, + year INT +); +``` + +In this example, we are creating a table called `books` with the following columns: + +| Column | Data Type | Data it can store | +| -------- | --------- | ----------------------------------------- | +| `id` | INT | Non-decimal numbers e.g. 1, 2, 3, etc. | +| `title` | VARCHAR | Max 255 characters long string of text | +| `author` | VARCHAR | Max 255 characters long string of text | +| `price` | FLOAT | Decimal numbers e.g. 10.99, 12.99, etc. | +| `year` | INT | Non-decimal numbers e.g. 1925, 1949, etc. | + +Now, let's run the above SQL query in the editor on the right and have a look at the "Schema" tab to see the table we created. + +--- + +## Error Handling + +In the previous example, we created a table called `books`. But what if we run the same query again? It will throw the error + +``` +ERROR: table "books" already exists +``` + +To avoid this error, we can use the `IF NOT EXISTS` clause. This clause checks if a table with the same name already exists before creating it. If it does, it will not create the table again. + +```sql +CREATE TABLE IF NOT EXISTS books ( + id INT, + title VARCHAR(255), + author VARCHAR(255), + price FLOAT, + year INT +); +``` + +You can run the above query as many times as you want and there will be no error. + +--- + +## Challenge + +Create a table called `books` with the following columns: + +| Column | Data it can store | +| -------- | ----------------------------------------- | +| `id` | Non-decimal numbers e.g. 1, 2, 3, etc. | +| `title` | Max 250 characters long string of text | +| `author` | Max 250 characters long string of text | +| `price` | Decimal numbers e.g. 10.99, 12.99, etc. | +| `year` | Non-decimal numbers e.g. 1925, 1949, etc. | + +You are required to identify the correct data types for the columns based on the description above. diff --git a/src/data/courses/sql/chapters/introduction/lessons/types-of-queries.md b/src/data/courses/sql/chapters/introduction/lessons/types-of-queries.md index 1242dc132..242843ef3 100644 --- a/src/data/courses/sql/chapters/introduction/lessons/types-of-queries.md +++ b/src/data/courses/sql/chapters/introduction/lessons/types-of-queries.md @@ -68,6 +68,8 @@ CREATE TABLE users ( ); ``` +Again, don't worry about the syntax for now. We will cover it in detail in the coming chapters. + ## Data Control Language (DCL) DCL is used to manage access permissions and security within a database. It allows administrators to grant or revoke rights to specific users. The most common DCL statements are `GRANT` and `REVOKE`. diff --git a/src/styles/global.css b/src/styles/global.css index 079d52c73..beb3199fd 100644 --- a/src/styles/global.css +++ b/src/styles/global.css @@ -93,6 +93,10 @@ a > code:before { .course-content p > code:before, .course-content.prose ul li > code:after, .course-content p > code:after, +.course-content h2 > code:after, +.course-content h2 > code:before, +.course-content table code:before, +.course-content table code:after, .course-content a > code:after, .course-content a > code:before { content: '' !important;