SQL - 在netezza中删除一列

时间:2016-11-27 15:26:49

标签: sql netezza

我有一张表table1,如下所示,我正试图删除一列。

表1

id name time    value
---------------------
1 john  11:00   324
2 NULL  12:00   645
3 NULL  13:00   324
4 jane  11:00   132
5 NULL  12:00   30

创建临时表,因为权限不能更改原始表。通过选择除id之外的所有内容,这种情况可能非常简单,但我真正需要做的是在有大量cols时删除一列。

create temp table table2 as(
select * from table1
) distribute on random;

alter table table2 drop column id;

这会产生错误 - Drop behaviour (RESTRICT | CASCADE) needs to be specified when dropping a column or constraint

alter table语句应该如何?

3 个答案:

答案 0 :(得分:1)

作为错误消息和documentation say,您需要指定RESTRICT或CASCADE。但请注意,您不能从真正的TEMPORARY表中删除列,因此这仅适用于普通表。

ALTER TABLE <table> <action> [ORGANIZE ON {(<columns>) | NONE}]

Where <action> can be one of:

ADD COLUMN <col> <type> [<col_constraint>][,…] |
ADD <table_constraint> |
ALTER [COLUMN] <col> { SET DEFAULT <value> | DROP DEFAULT } |
DROP [COLUMN] column_name[,column_name…] {CASCADE | RESTRICT } |
DROP CONSTRAINT <constraint_name> {CASCADE | RESTRICT} |
MODIFY COLUMN (<col> VARCHAR(<maxsize>)) |
OWNER TO <user_name> |
RENAME [COLUMN] <col> TO <new_col_name> |
RENAME TO <new_table> |
SET PRIVILEGES TO <table> 

像这样:

SYSTEM.ADMIN(ADMIN)=> create table t1 (col1 bigint, col2 varchar(5));
CREATE TABLE
SYSTEM.ADMIN(ADMIN)=> insert into t1 values (1,'One');
INSERT 0 1
SYSTEM.ADMIN(ADMIN)=> insert into t1 values (2,'Two');
INSERT 0 1
SYSTEM.ADMIN(ADMIN)=> insert into t1 values (3,'Three');
INSERT 0 1
SYSTEM.ADMIN(ADMIN)=> select * from t1;
 COL1 | COL2  
------+-------
    3 | Three
    1 | One
    2 | Two
(3 rows)

SYSTEM.ADMIN(ADMIN)=> alter table t1 drop column col2 restrict;
ALTER TABLE
SYSTEM.ADMIN(ADMIN)=> select * from t1;
 COL1 
------
    1
    2
    3
(3 rows)

与往常一样,如果您更改要删除或添加列的表,则应使用GROOM进行跟踪以清理版本化表:

SYSTEM.ADMIN(ADMIN)=> groom table t1 versions;
NOTICE:  Groom will not purge records deleted by transactions that started after 2016-11-07 17:00:11.
NOTICE:  If this process is interrupted please either repeat GROOM VERSIONS or issue 'GENERATE STATISTICS ON "T1"'
NOTICE:  Groom processed 2 pages; purged 0 records; scan size unchanged; table size unchanged.
GROOM VERSIONS
SYSTEM.ADMIN(ADMIN)=>

答案 1 :(得分:0)

这是在Netezza中删除列的语法

Alter table tablename drop columnname RESTRICT

答案 2 :(得分:-1)

据此:http://datawarehouse.ittoolbox.com/groups/technical-functional/netezza-l/netezza-issue-2467523 看来你不能通过ALTER TABLE来删除一个列,只有一个约束。