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'
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With