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
postgresql : how to rename a table inside a schema
提问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;