Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn ALTER and INSERT Operations | DDL and DML in SQL
Intermediate SQL
Section 4. Chapter 2
single

single

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.

Note
Definition

ALTER: Used to change the structure of existing database objects, such as adding or removing columns from a table.

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
Note

Using the ALTER statement, 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.

Note
Definition

INSERT: Used to add new rows to a table.

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:

  1. Start with INSERT INTO, followed by the table name;
  2. Specify the column names in parentheses;
  3. Use VALUES to list the data in the same order as the columns;
  4. Ensure data types match the columns;
  5. 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!

Task

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)

Your task is to modify the table structure and populate it with data:

  1. Add a new column country VARCHAR(50) to the existing table using ALTER TABLE.
  2. Insert 2 rows into the table using INSERT INTO:
employee_idfirst_namelast_namedepartmentsalarycountry
1EmilyTorresOperations80000United Kingdom
2DavidBobrEngineering95000Poland

Do not delete or modify the pre-written code on the right — it is used to check your solution.

Instructions

  • Use ALTER TABLE ... ADD COLUMN to add country VARCHAR(50) to the employees table.
  • Use INSERT INTO employees (col1, col2, ...) — list all columns including the new country column.
  • Insert both rows with the exact values shown in the table above.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt