Course Content
Intermediate SQL
Intermediate SQL
CREATE and Constraints
Previously, we worked for different companies and executed SELECT
queries for their needs. However, we need to learn how to create and modify tables.
Tables are created using the CREATE
statement, which has a similar structure to the SELECT
statement, except instead of selecting data, it creates data:
CREATE TABLE example ( id INT PRIMARY KEY, some_info VARCHAR(50) );
Note
When you run these examples, you won't get any output because these examples only create a new table. If you run the code again, you will get an error saying that the table already exists. These code snippets are examples, and later in the task, data will be inserted into these newly created tables and displayed on the screen so you can see that everything is working.
Let's break down the SQL query above.
This query creates an empty table with two columns: id
and some_info
.
Notice the data types used for each column:
INT
is for integer data;VARCHAR(50)
is for text, allowing up to 50 characters.
We won't cover all data types now, as there are many. We'll focus on the main ones in this section and explore others as we continue learning.
For example, let's create another table with different data types:
CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(50), birthdate DATE, salary DECIMAL(10, 2), is_active BOOLEAN );
With this query, we create an empty table that should contain information about users, including:
- An
ID
with an integer data type; - Information about the
name
, with aVARCHAR(50)
data type; - Information about the birth date, with a
DATE
data type; - Information about the salary, with a floating-point number data type;
- Whether the user is active, with a data type that only accepts
true
orfalse
values.
Constraints
In SQL, constraints are rules applied to columns to ensure data integrity. For example, when we use PRIMARY KEY
next to ID
, it means this column must have unique values and cannot be empty. Each table can have only one PRIMARY KEY
.
Here are some common constraints:
NOT NULL
: This ensures the column cannot have empty values.UNIQUE
: This ensures all values in the column are different.DEFAULT
: This sets a default value for the column if no value is provided.
These are just a few examples. Let's see how we can modify a table to include these constraints:
CREATE TABLE users_2 ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, birthdate DATE, salary DECIMAL(10, 2) DEFAULT 50000, is_active BOOLEAN );
Now, the name
column must always have a value, as it cannot be empty or null. Also, if no salary is specified, it will default to 50000
.
Using constraints like these helps ensure that the data in your table is accurate and follows the rules you set.
Swipe to show code editor
Your task is to create a table named library
.
This table should have 4 columns:
id
- integer primary key;title
- varchar, not null;author
- varchar;pages
- int.
At the end of the query, be sure to put a semicolon (;
).
Please use these column names exactly as specified.
Note
On the right, you will see a large amount of code; do not modify it. It is written to ensure that your solution is correctly checked. We will learn everything written there later in this section.
Brief Instructions
- Use a CREATE query to create a new table named
library
. - The table should have four columns:
id
,title
,author
, andpages
. - For the first column, specify
INT PRIMARY KEY
. - For the second column, specify
VARCHAR(50) NOT NULL
. - For the third column, specify
VARCHAR(50)
. - For the fourth column, specify
INT
.
Solution
Thanks for your feedback!
CREATE and Constraints
Previously, we worked for different companies and executed SELECT
queries for their needs. However, we need to learn how to create and modify tables.
Tables are created using the CREATE
statement, which has a similar structure to the SELECT
statement, except instead of selecting data, it creates data:
CREATE TABLE example ( id INT PRIMARY KEY, some_info VARCHAR(50) );
Note
When you run these examples, you won't get any output because these examples only create a new table. If you run the code again, you will get an error saying that the table already exists. These code snippets are examples, and later in the task, data will be inserted into these newly created tables and displayed on the screen so you can see that everything is working.
Let's break down the SQL query above.
This query creates an empty table with two columns: id
and some_info
.
Notice the data types used for each column:
INT
is for integer data;VARCHAR(50)
is for text, allowing up to 50 characters.
We won't cover all data types now, as there are many. We'll focus on the main ones in this section and explore others as we continue learning.
For example, let's create another table with different data types:
CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(50), birthdate DATE, salary DECIMAL(10, 2), is_active BOOLEAN );
With this query, we create an empty table that should contain information about users, including:
- An
ID
with an integer data type; - Information about the
name
, with aVARCHAR(50)
data type; - Information about the birth date, with a
DATE
data type; - Information about the salary, with a floating-point number data type;
- Whether the user is active, with a data type that only accepts
true
orfalse
values.
Constraints
In SQL, constraints are rules applied to columns to ensure data integrity. For example, when we use PRIMARY KEY
next to ID
, it means this column must have unique values and cannot be empty. Each table can have only one PRIMARY KEY
.
Here are some common constraints:
NOT NULL
: This ensures the column cannot have empty values.UNIQUE
: This ensures all values in the column are different.DEFAULT
: This sets a default value for the column if no value is provided.
These are just a few examples. Let's see how we can modify a table to include these constraints:
CREATE TABLE users_2 ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, birthdate DATE, salary DECIMAL(10, 2) DEFAULT 50000, is_active BOOLEAN );
Now, the name
column must always have a value, as it cannot be empty or null. Also, if no salary is specified, it will default to 50000
.
Using constraints like these helps ensure that the data in your table is accurate and follows the rules you set.
Swipe to show code editor
Your task is to create a table named library
.
This table should have 4 columns:
id
- integer primary key;title
- varchar, not null;author
- varchar;pages
- int.
At the end of the query, be sure to put a semicolon (;
).
Please use these column names exactly as specified.
Note
On the right, you will see a large amount of code; do not modify it. It is written to ensure that your solution is correctly checked. We will learn everything written there later in this section.
Brief Instructions
- Use a CREATE query to create a new table named
library
. - The table should have four columns:
id
,title
,author
, andpages
. - For the first column, specify
INT PRIMARY KEY
. - For the second column, specify
VARCHAR(50) NOT NULL
. - For the third column, specify
VARCHAR(50)
. - For the fourth column, specify
INT
.
Solution
Thanks for your feedback!