how to move tables from public to other schema in Postgres
Postgres 9.1数据库包含公共模式中的表yksus1 .. ykssu9。 pgAdmin显示这些定义,如下面的代码所示。
如何将这些表移动到firma1架构?
firma1模式中的其他表具有对这些表primay键的外键引用。 对这些表的外键引用仅来自firma1模式中的表。
其中一些表包含数据。
如果将表移动到firma1模式,则外键引用shouuld也会更新为firma1.yksusn表。
表结构不能改变。
看起来主键序列已经在firma1架构中,因此不应移动它们。
x86_64-unknown-linux-gnu上的版本字符串PostgreSQL 9.1.2,由gcc-4.4 .real编译(Debian 4.4.5-8)4.4.5,64位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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(); |
其他表格类似:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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(); |
1 2 | ALTER TABLE yksus1 SET SCHEMA firma1; |
手册中的更多细节:http://www.postgresql.org/docs/current/static/sql-altertable.html
Associated indexes, constraints, and sequences owned by table columns are moved as well.
虽然不确定触发器功能,但也有一个等效的