Bird
0
0

In a migration, which SQL query correctly renames the column 'username' to 'user_name' in the 'accounts' table for a PostgreSQL database inside the up() method?

hard📝 Application Q8 of 15
NestJS - Database with TypeORM
In a migration, which SQL query correctly renames the column 'username' to 'user_name' in the 'accounts' table for a PostgreSQL database inside the up() method?
Aawait queryRunner.query(`ALTER TABLE accounts RENAME COLUMN username TO user_name`);
Bawait queryRunner.query(`ALTER TABLE accounts CHANGE username user_name VARCHAR(255)`);
Cawait queryRunner.query(`RENAME COLUMN username TO user_name IN accounts`);
Dawait queryRunner.query(`MODIFY COLUMN username TO user_name IN accounts`);
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct SQL syntax for renaming columns in PostgreSQL

    PostgreSQL uses ALTER TABLE table_name RENAME COLUMN old_name TO new_name.
  2. Step 2: Match with options

    await queryRunner.query(`ALTER TABLE accounts RENAME COLUMN username TO user_name`); matches the correct syntax exactly.
  3. Final Answer:

    await queryRunner.query(`ALTER TABLE accounts RENAME COLUMN username TO user_name`); -> Option A
  4. Quick Check:

    PostgreSQL uses RENAME COLUMN syntax [OK]
Quick Trick: PostgreSQL renames columns with ALTER TABLE RENAME COLUMN [OK]
Common Mistakes:
  • Using MySQL syntax (CHANGE) in PostgreSQL
  • Incorrect SQL keywords like MODIFY or REPLACE
  • Wrong order of clauses in the query

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes