Difference between revisions of "PostgreSQL-Database/C2/Alter-Table-Statement/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of the Script: Alter Table Statement'''. '''Author: EduPyramids''' '''Keywords: '''PostgreSQL, pgAdmin4, Alter Table, add marks, numeric type, EduPyramids, video t...")
(No difference)

Revision as of 22:12, 8 May 2026

Title of the Script: Alter Table Statement.

Author: EduPyramids


Keywords: PostgreSQL, pgAdmin4, Alter Table, add marks, numeric type, EduPyramids, video tutorial.


Visual Cue Narration
Slide 1

Title Slide

Welcome to this Spoken Tutorial on Alter Table Statement in PostgreSQL.
Slide 2

Learning Objectives

In this tutorial, we will learn about:
  • Alter table statement
Slide 3

System Requirements

  • Ubuntu 24.04 LTS
  • PostgreSQL version 18.1, and
  • PgAdmin4 version 9.11.


To record this tutorial, I am using the following setup.
Slide 4

Pre-requisites

https://EduPyramids.org

To follow this tutorial,
  • Learners should have PostgreSQL and pgAdmin4
  • For the prerequisite PostgreSQL tutorials, please visit this website.
Slide 5

Code Files

The following code file is required to practice this tutorial:

  • al-commands.txt

This file is provided in the Code Files link on this tutorial page.

Please download and extract the file.

The following code file is required to practice this tutorial.

This file is provided in the Code Files link of this tutorial page.

Please download and extract the file.

Let us learn about the Alter table.
Slide 5

Alter Table

ALTER TABLE is used to modify the structure of an existing table.

It can add, delete, or modify columns.

It is used to add or remove constraints.

Slide 6

Syntax of Alter Table statement

ALTER TABLE table_name

ADD column_name datatype;

This is the syntax of the Alter table statement to add a column.
Let us understand with an example.
Show pgAdmin 4 interface I have opened the pgAdmin 4 interface and connected to the localhost server.
Type:

SELECT * FROM students;


Show the student table

Highlight: student ID, name, city, gender, date of birth, and CGPA.

This is the students table with its current columns.

Now, we will add new columns to store subject marks.

To add a new column, we will use the ALTER TABLE statement.
Type:


ALTER TABLE students

ADD Maths NUMERIC,

ADD English NUMERIC;

Click on the Execute query button.

Let's type the following query.

In this query we will add two columns Maths and English with Numeric data type.

Click on the Execute query icon.

The table now has two new columns Maths and English.

Type:

SELECT * FROM students;

Click on the Execute query button.

To see the added columns type this query.

Let us execute the query..


We can see the maths and english columns added successfully.

Copy and paste from code file:

UPDATE students AS s

SET

maths = v.maths,

english = v.english

FROM (VALUES

('S101',72,75), ('S102',88,91), ('S103',69,73),

('S104',85,87), ('S105',64,70), ('S106',92,95),

('S107',78,81), ('S108',86,89), ('S109',71,74),

('S110',83,85), ('S111',67,69), ('S112',90,93),

('S113',76,80), ('S114',82,84), ('S115',68,72),

('S116',74,77), ('S117',89,92)

) AS v(studentid, maths, english)

WHERE s.studentid = v.studentid;

Click on the Execute Query button on the tool bar.

Let us copy and paste this query from the code file into the query window.

Let us add marks to the Maths and English columns of the students table.

We will use the UPDATE statement to populate marks for the existing student records.

Let us execute the query.

Type:

ALTER TABLE students RENAME COLUMN dob TO date_of_birth;

Highlight output.

Now let us see how to modify a column name.

Type this query.This query changes the column name from dob to date of birth.

Let us execute the query.

The column name is modified successfully.

Type:

SELECT * FROM students;

Click on the Execute query button.

To see the inserted marks and change in column name, type this query.

Let us execute it.


The table is modified successfully.

Slide 7

Summary

In this tutorial, we have learnt about,

  • Alter table statement
With this we come to the end of this tutorial.

Let us summarise.

Slide 8

Assignment

As an assignment,

  • Add a new column email with datatype VARCHAR(50) to the students table.


We encourage you to try this assignment.
Slide 9

This Spoken Tutorial is brought to you by EduPyramids Educational Services Private limited SINE IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Madhurig