Redshift - 如何删除NOT NULL约束?

时间:2022-08-11 23:05:06

Since Redshift does not support ALTER COLUMN, I would like to know if it's possible to remove the NOT NULL constraints from columns in Redshift.

由于Redshift不支持ALTER COLUMN,我想知道是否可以从Redshift中的列中删除NOT NULL约束。

2 个解决方案

#1


18  

You cannot alter the table.

你不能改变桌子。

There is an alternative approach. You can create a new column with NULL constraint. Copy the values from your old column to this new column and then drop the old column.

还有另一种方法。您可以使用NULL约束创建新列。将旧列中的值复制到此新列,然后删除旧列。

Something like this:

像这样的东西:

ALTER TABLE table1 ADD COLUMN somecolumn (definition as per your reqm);
UPDATE table1 SET somecolumn = oldcolumn;
ALTER TABLE table1 DROP COLUMN oldcolumn;
ALTER TABLE table1 RENAME COLUMN somecolumn TO oldcolumn;

#2


8  

There is no way to change column on Redshift.

没有办法在Redshift上更改列。

I can suggest you to create new column, copy values from old to new column and drop old column.

我建议您创建新列,将值从旧列复制到新列并删除旧列。

ALTER TABLE Table1 ADD COLUMN new_column (___correct_column_definition___);
UPDATE Table1 SET new_column = column;
ALTER TABLE Table1 DROP COLUMN column;
ALTER TABLE Table1 RENAME COLUMN new_column TO column;

#1


18  

You cannot alter the table.

你不能改变桌子。

There is an alternative approach. You can create a new column with NULL constraint. Copy the values from your old column to this new column and then drop the old column.

还有另一种方法。您可以使用NULL约束创建新列。将旧列中的值复制到此新列,然后删除旧列。

Something like this:

像这样的东西:

ALTER TABLE table1 ADD COLUMN somecolumn (definition as per your reqm);
UPDATE table1 SET somecolumn = oldcolumn;
ALTER TABLE table1 DROP COLUMN oldcolumn;
ALTER TABLE table1 RENAME COLUMN somecolumn TO oldcolumn;

#2


8  

There is no way to change column on Redshift.

没有办法在Redshift上更改列。

I can suggest you to create new column, copy values from old to new column and drop old column.

我建议您创建新列,将值从旧列复制到新列并删除旧列。

ALTER TABLE Table1 ADD COLUMN new_column (___correct_column_definition___);
UPDATE Table1 SET new_column = column;
ALTER TABLE Table1 DROP COLUMN column;
ALTER TABLE Table1 RENAME COLUMN new_column TO column;