- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add column to an existing table in PostgreSQL?
The syntax to add a new column to an existing table is quite straightforward.
ALTER TABLE table_name ADD COLUMN column_name column_type column_constraint;
Say you have existing table marks. An example is given below −
serial_no | name | roll_no | marks_obtained | perc_marks | max_marks | date_of_entry |
---|---|---|---|---|---|---|
1 | Yash | 26 | 42 | 42 | 100 | 2021-01-30 |
2 | Isha | 56 | 175 | 87.5 | 200 | 2021-01-30 |
Now, suppose you want to add a column named subject. You can do that using −
ALTER TABLE marks ADD COLUMN subject VARCHAR;
Now if you query the table again using,
SELECT * from marks
You will see the following output −
serial_no | name | roll_no | marks_obtained | perc_ marks | max_ marks | date_ of_ entry | subject |
---|---|---|---|---|---|---|---|
1 | Yash | 26 | 42 | 42 | 100 | 2021-01-30 | [null] |
2 | Isha | 56 | 175 | 87.5 | 200 | 2021-01-30 | [null] |
Note that the values in the subject column are null because we have just created the column, not populated it. We can populate it using the UPDATE statements. The statement syntax is as follows −
UPDATE table_name SET column_name = column_value WHERE condition
For instance, if, in the above example, Yash has scored 42 marks in Maths, the UPDATE statement will look like this −
UPDATE marks SET subject 'Maths' WHERE roll_no = 26
You could have also added the name=’Yash’ condition instead of roll_no=26. Now, if you query the table, you will see the following output −
serial_no | name | roll_no | marks_obtained | perc_ marks | max_ marks | date_ of_ entry | subject |
---|---|---|---|---|---|---|---|
1 | Yash | 26 | 42 | 42 | 100 | 2021-01-30 | Maths |
2 | Isha | 56 | 175 | 87.5 | 200 | 2021-01-30 | [null] |
If you don’t add any condition in the UPDATE statement, the value of every row will be changed for that column. For instance, if I run the following query −
UPDATE marks SET subject = 'Science'
And then query the table, I’ll see the following output −
serial_no | name | roll_no | marks_obtained | perc_ marks | max_ marks | date_ of_ entry | subject |
---|---|---|---|---|---|---|---|
1 | Yash | 26 | 42 | 42 | 100 | 2021-01-30 | Science |
2 | Isha | 56 | 175 | 87.5 | 200 | 2021-01-30 | Science |
- Related Articles
- How to add a new column to an existing table using JDBC API?
- How to add a new column to an existing table of Laravel in a migration?
- Create Primary Key on an existing table in PostgreSQL?
- How to add columns to an existing MySQL table?
- How to add current date to an existing MySQL table?
- How to rename a column in an existing MySQL table?
- Adding new enum column to an existing MySQL table?
- Add a new column and index to an existing table with ALTER in a single MySQL query?
- How to add not null constraint to existing column in MySQL?
- How to set auto-increment to an existing column in a table using JDBC API?
- How can we add columns with default values to an existing MySQL table?
- How to create a table in PostgreSQL?
- How can we add multiple columns, with single command, to an existing MySQL table?
- How to change the datatype of a column in an existing table using JDBC API?
- How to delete a column from an existing table in a database using JDBC API?
