2011-10-12 176 views
3

在postgres sql中创建表为select在表上删除非空限制。创建表,因为select是在postgresql中删除非空限制

例如:

create table A (char a not null); 
create table B as select * from a; 
select * from B;-- no constraint is copied from A table 

请让我知道如何表数据,以及限制复制的Postgres。

回答

6

对此没有单一命令解决方案。

要创建基于现有的,包括所有的约束,使用表:

create table B (like a including constraints); 

一旦你这样做,你可以从旧的数据复制到新的一个:

insert into b 
select * from a; 

如果您在单个事务中执行此操作,它看起来像连接到数据库的所有其他会话的原子操作。

0

非常详细的,并很好地解释教程PostgreSQL中创建表命令9.1

http://www.postgresql.org/docs/current/static/sql-createtable.html

非空约束总是被复制(如果通过给父表的参考在创建表命令创建表),甚至用包括约束,只有检查约束将被复制。

+0

我将链接替换为发布版本的“官方”手册。它包含相同的信息。 –