2016-12-27 52 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

相关问题