postgresql:如何重命名架构内的表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27787741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:52:22  来源:igfitidea点击:

postgresql : how to rename a table inside a schema

postgresql

提问by comte

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

我正在使用 PostgreSQL 9.x,我想重命名一个表。此 SQL 代码:

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

renames the table correctly. But this SQL code:

正确重命名表。但是这个 SQL 代码:

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'

这 ”。” 下划线是“域”和“新”之间的那个

回答by Bohuslav Burghardt

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.

的文档search_path

回答by 7guyo

switch to your database

切换到你的数据库

machine$\c my_database

机器$\c my_database

rename the db

重命名数据库

my_databse=# alter table old_name rename to new_name;

my_databse=# 将表 old_name 重命名为 new_name;