2016-12-27 42 views
4

我使用PostgreSQL 9.5,我有discribes列的集合A型:如何在不改變PostgreSQL類型的情況下將列添加到類型表中?

CREATE TYPE datastore.record AS 
    (recordid bigint, 
    ... 
    tags text[]); 

我創造了許多表reliying這種類型:

CREATE TABLE datastore.events 
OF datastore.record; 

現在我想補充一個列添加到依賴此TYPE而不更新TYPE的表中。我認爲這是不可能的,因此我想知道是否有辦法從這個TYPE中取出我的表而不丟失任何數據或將表複製到臨時表中?

回答

5

爲此,有一個特殊選項not of。每the documentation

不 - 這種形式從類型分離類型表。

所以:

alter table my_table not of; 
alter table my_table add new_column integer; 
+0

謝謝你,它的工作。我不知道NOT OF關鍵字。我所有的關係都保存下來, – jlandercy

+0

太棒了。我不知道' –

1

如果你不想打破關係:

--drop table if exists t2; 
--drop table if exists t1; 
--drop type if exists tp_foo; 

create type tp_foo as (i int, x int); 

create table t1 of tp_foo; 
create table t2 (y text) inherits(t1); 

alter type tp_foo add attribute z date cascade; 

select * from t2; 
+0

'不是'謝謝你分享這個方法。經過測試,它會緩解我的維護。 – jlandercy

相關問題