Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a table inside a schema?

Tags:

postgresql

I'm using PostgreSQL 9.x, I want to rename a table. This SQL code:

CREATE TABLE new (id int); ALTER TABLE new RENAME TO old; DROP TABLE old; 

renames the table correctly. But this SQL code:

CREATE SCHEMA domain; CREATE TABLE domain.old (id int); ALTER TABLE domain.old RENAME TO domain.new; 

fails, with error:

ERROR: syntax error at or near "."

The "." underlined is the one between 'domain' and 'new'

like image 344
comte Avatar asked Jan 05 '15 20:01

comte


People also ask

How do I rename a table with schema?

To change the schema and table name both, first we have to rename the table using SP_RENAME and then transfer the schema using ALTER SCHEMA command.

How do I rename a table schema in SQL Server?

To change the schema of a table by using SQL Server Management Studio, in Object Explorer, right-click on the table and then click Design. Press F4 to open the Properties window. In the Schema box, select a new schema. ALTER SCHEMA uses a schema level lock.

Can we rename a table in PostgreSQL?

PostgreSQL has a RENAME clause that is used with the ALTER TABLE statement to rename the name of an existing table. Syntax: ALTER TABLE table_name RENAME TO new_table_name; In the above syntax: First, specify the name of the table which you want to rename after the ALTER TABLE clause.


1 Answers

One way to do this:

ALTER TABLE domain.old RENAME TO new 

Other way:

SET search_path TO domain; ALTER TABLE old RENAME TO new; 

Documentation for search_path.

like image 100
Bohuslav Burghardt Avatar answered Oct 15 '22 03:10

Bohuslav Burghardt