Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (6.1k points)

In PostgreSQL, if I have to rename and change the column data type, I should run two separate queries to do so.

To rename:

ALTER TABLE <tablename> RENAME <oldcolumn> TO <newcolumn> 

And to change the column type:

ALTER TABLE <tablename> ALTER COLUMN <columnname> <columntype>.

Although is there any way to do both of these works with a single query like the below MySQL query:

ALTER TABLE <tableName> CHANGE COLUMN <oldcolumnname> <newcolumnname> <newtype>

1 Answer

0 votes
by (12.7k points)

In the PostgreSQL, ALTER TABLE can take a set of operations. So:

ALTER TABLE <tablename> RENAME <oldcolumn> TO <newcolumn>;
ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE <newtype>;

Is the same as,

ALTER TABLE <tablename> 
  ALTER COLUMN <columnname> TYPE <newtype>
  RENAME <oldcolumn> TO <newcolumn>;

Looking for a SQL Tutorial? Join the Intellipaat's SQL Course to gain more knowledge on SQL.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...