Change Table Name — SQL Technical Interview Questions & Answers

Change Table Name

Change Table Name

  • RENAME TABLE students TO learners; or ALTER TABLE students RENAME TO learners; Both change the table name. Apps and FOREIGN KEYs that still say students will break until you update them.
  • Trap: renaming in production during peak hours without updating the app.
  • On the example next to this theory: Change Table Name: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Technical Interview Tip: RENAME TABLE students TO learners;

Code Examples for Change Table Name

Change Table Name — sample query

-- Change Table Name
CREATE TABLE demo (
  id INTEGER PRIMARY KEY,
  label TEXT NOT NULL,
  qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
  (1, 'alpha', 2),
  (2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;

Change Table Name: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

How to Explain Change Table Name to an Interviewer

How do you rename a table?

RENAME TABLE old TO new; or ALTER TABLE … RENAME TO