ALTER and INSERT Operations
Let's imagine a situation where we need to add a column to an existing table. It wouldn't be right to delete the table (especially if it already contains some data) and then create a new table, filling it again with data.
Therefore, in this chapter, we will look at the ALTER operation.
Let's see how to use this operation:
CREATE TABLE library (
id INT PRIMARY KEY,
title VARCHAR(50) NOT NULL,
author VARCHAR(50),
pages INT
);
ALTER TABLE library ADD price DECIMAL DEFAULT 300;
ALTER TABLE library DROP COLUMN price;
As you can see, this is the script for creating a table from the previous chapter.
Next, there are two ALTER operations. The first operation adds a price column to the table, setting the default value to 300 for this column. The second operation removes this column:
ALTER TABLE table_name ADD/DROP column_name DATA_TYPE;
Note
Using the
ALTERstatement, you can perform various schema-level operations on a table, such as adding or removing constraints, renaming, changing data types, and adding or dropping indexes.
Let's move on to another operation, namely the insertion operation.
To use INSERT, we need to specify into which columns we want to add values.
Here's what the syntax of this statement looks like:
INSERT INTO library (id, title, author, pages) VALUES
(1, 'CAMINO GHOSTS', 'John Grisham', '213'),
(2, 'FUNNY STORY', 'Emily Henry', '341');
This snippet is from the previous chapter, showing how to insert data into the library table.
Here's a breakdown:
- Start with
INSERT INTO, followed by the table name; - Specify the column names in parentheses;
- Use
VALUESto list the data in the same order as the columns; - Ensure data types match the columns;
- Close parentheses and separate rows with commas.
The general syntax is:
INSERT INTO table_name (column1_name, column2_name) VALUES
(column1_value, column2_value),
(column1_value, column2_value),
...;
Don't forget about the semicolon in the end!
Swipe to start coding
There is an empty table called employees with the following columns:
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
It's the same table as in the previous sections, but now this table doesn't contain any data (rows) at all.
Your task is to:
- Add a column
countryto this table, which will contain information about the country where the employee resides. - Insert 2 rows of data into the table, which will look like this:
id=1, first_name=Emily, last_name=Torres, department=Operations, salary=80000, country=United Kingdom.id=2, first_name=David, last_name=Bobr, department=Engineering, salary=95000, country=Poland.
To accomplish this task, use ALTER TABLE for the first subtask and INSERT for the second subtask.
Note
On the right side of the code editor, some code will already be written. Please do not delete or edit this code, as it is necessary to check the correctness of your solution.
Brief Instructions
- Use an ALTER TABLE statement to add a
countrycolumn of typeVARCHAR(50)to theemployeestable. - Use INSERT INTO to add two employees to the
employeestable. - In parentheses, specify the columns in the correct order where youβll insert the data.
- Insert two employees with the data provided in the requirements.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
What other operations can I perform with the ALTER statement?
Can you explain how to remove multiple columns at once?
How do I insert data if I don't want to specify all columns?
Awesome!
Completion rate improved to 4
ALTER and INSERT Operations
Swipe to show menu
Let's imagine a situation where we need to add a column to an existing table. It wouldn't be right to delete the table (especially if it already contains some data) and then create a new table, filling it again with data.
Therefore, in this chapter, we will look at the ALTER operation.
Let's see how to use this operation:
CREATE TABLE library (
id INT PRIMARY KEY,
title VARCHAR(50) NOT NULL,
author VARCHAR(50),
pages INT
);
ALTER TABLE library ADD price DECIMAL DEFAULT 300;
ALTER TABLE library DROP COLUMN price;
As you can see, this is the script for creating a table from the previous chapter.
Next, there are two ALTER operations. The first operation adds a price column to the table, setting the default value to 300 for this column. The second operation removes this column:
ALTER TABLE table_name ADD/DROP column_name DATA_TYPE;
Note
Using the
ALTERstatement, you can perform various schema-level operations on a table, such as adding or removing constraints, renaming, changing data types, and adding or dropping indexes.
Let's move on to another operation, namely the insertion operation.
To use INSERT, we need to specify into which columns we want to add values.
Here's what the syntax of this statement looks like:
INSERT INTO library (id, title, author, pages) VALUES
(1, 'CAMINO GHOSTS', 'John Grisham', '213'),
(2, 'FUNNY STORY', 'Emily Henry', '341');
This snippet is from the previous chapter, showing how to insert data into the library table.
Here's a breakdown:
- Start with
INSERT INTO, followed by the table name; - Specify the column names in parentheses;
- Use
VALUESto list the data in the same order as the columns; - Ensure data types match the columns;
- Close parentheses and separate rows with commas.
The general syntax is:
INSERT INTO table_name (column1_name, column2_name) VALUES
(column1_value, column2_value),
(column1_value, column2_value),
...;
Don't forget about the semicolon in the end!
Swipe to start coding
There is an empty table called employees with the following columns:
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
It's the same table as in the previous sections, but now this table doesn't contain any data (rows) at all.
Your task is to:
- Add a column
countryto this table, which will contain information about the country where the employee resides. - Insert 2 rows of data into the table, which will look like this:
id=1, first_name=Emily, last_name=Torres, department=Operations, salary=80000, country=United Kingdom.id=2, first_name=David, last_name=Bobr, department=Engineering, salary=95000, country=Poland.
To accomplish this task, use ALTER TABLE for the first subtask and INSERT for the second subtask.
Note
On the right side of the code editor, some code will already be written. Please do not delete or edit this code, as it is necessary to check the correctness of your solution.
Brief Instructions
- Use an ALTER TABLE statement to add a
countrycolumn of typeVARCHAR(50)to theemployeestable. - Use INSERT INTO to add two employees to the
employeestable. - In parentheses, specify the columns in the correct order where youβll insert the data.
- Insert two employees with the data provided in the requirements.
Solution
Thanks for your feedback!
single