0

Let's say I have a table Student with just 2 columns - id bigint, name varchar(50).

I'm trying to rename it and add column in a same single query in PostgreSQL version 11.16 like below:

ALTER TABLE student
  RENAME COLUMN name TO fullname, 
  ADD COLUMN roll_no varchar(30);

But I got this error:

Syntax error at or near "ADD"

Is it possible? If not, why?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    No, not possible. You could execute two ALTER statements in a single transaction. And why it's not supported? Because nobody has build it yet. – Frank Heikens Oct 18 '22 at 11:06
  • 1
    im not sure but maybe that will help you: https://stackoverflow.com/questions/70176036/setting-multiple-alter-statements-on-a-single-column-in-one-command-sqlhttps://stackoverflow.com/questions/70176036/setting-multiple-alter-statements-on-a-single-column-in-one-command-sql – Marco Oct 18 '22 at 11:09
  • @frank-heikens, But I can add two columns in a single alter query. – Md. Nowshad Hasan Oct 18 '22 at 11:12
  • 2
    @Md.NowshadHasan: Yes, that's correct, you can add multiple columns in a single statement. But not rename and add in a single statement. Just check the manual – Frank Heikens Oct 18 '22 at 11:13

1 Answers1

0

Actually, that is not possible. I got this from the StackOverflow answer. One RENAME is possible for only one statement. Even multiple RENAME commands are also not possible. Here is the postgres manual.