在 Postgres,如何将表从 public 转移到其他模式

Postgres 9.1数据库包含 public schema. pgAdmin 中的表 yksus1. . ykssu9,下面的代码显示了这些定义。 如何将这些表移动到 firma1模式?

Firma1模式中的其他表具有对这些表主键的外键引用。对这些表的外键引用仅来自 firma1模式中的表。

其中一些表包含数据。 如果表被移动到 firma1模式,外键引用也应该更新到 firma1.yksusn 表。 不能更改表结构。

看起来主键序列已经在 firma1模式中了,所以不应该移动它们。 X86 _ 64-known-linux-gnu 上的版本字符串 PostgreSQL 9.1.2,由 gcc-4.4. real (Debian 4.4.5-8)4.4.5,64位编译

CREATE TABLE yksus1
(
yksus character(10) NOT NULL DEFAULT ((nextval('firma1.yksus1_yksus_seq'::regclass))::text || '_'::text),
veebis ebool,
nimetus character(70),
"timestamp" character(14) DEFAULT to_char(now(), 'YYYYMMDDHH24MISS'::text),
username character(10) DEFAULT "current_user"(),
klient character(40),
superinden character(20),
telefon character(10),
aadress character(50),
tlnr character(15),
rus character(60),
CONSTRAINT yksus1_pkey PRIMARY KEY (yksus)
);
ALTER TABLE yksus1
OWNER TO mydb_owner;


CREATE TRIGGER yksus1_trig
BEFORE INSERT OR UPDATE OR DELETE
ON yksus1
FOR EACH STATEMENT
EXECUTE PROCEDURE setlastchange();

其他表格也类似:

CREATE TABLE yksus2
(
yksus character(10) NOT NULL DEFAULT ((nextval('firma1.yksus2_yksus_seq'::regclass))::text || '_'::text),
nimetus character(70),
"timestamp" character(14) DEFAULT to_char(now(), 'YYYYMMDDHH24MISS'::text),
osakond character(10),
username character(10) DEFAULT "current_user"(),
klient character(40),
superinden character(20),
telefon character(10),
aadress character(50),
tlnr character(15),
rus character(60),
CONSTRAINT yksus2_pkey PRIMARY KEY (yksus),
CONSTRAINT yksus2_osakond_fkey FOREIGN KEY (osakond)
REFERENCES yksus2 (yksus) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE NO ACTION DEFERRABLE INITIALLY IMMEDIATE
);
ALTER TABLE yksus2
OWNER TO mydb_owner;


CREATE TRIGGER yksus2_trig
BEFORE INSERT OR UPDATE OR DELETE
ON yksus2
FOR EACH STATEMENT
EXECUTE PROCEDURE setlastchange();
73667 次浏览
ALTER TABLE yksus1
SET SCHEMA firma1;

More details in the manual: http://www.postgresql.org/docs/current/static/sql-altertable.html

Associated indexes, constraints, and sequences owned by table columns are moved as well.

Not sure about the trigger function though, but there is an equivalent ALTER FUNCTION .. SET SCHEMA ... as well.