Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...