修改 SET USING 和 DEFAULT 列
修改 SET USING 和 DEFAULT 表达式
ALTER TABLE...ALTER COLUMN 可以将现有列设置为 SET USING 或 DEFAULT,或者更改现有 SET USING 或 DEFAULT 列的查询表达式。在这两种情况下,现有值都保持不变。Vertica 在以下情况下会刷新列值:
-
DEFAULT 列:仅在加载新行或调用 UPDATE 将列值设置为
DEFAULT
时刷新。 -
SET USING 列:仅当您在表上调用 REFRESH_COLUMNS 时才刷新。
例如,可以将整个列设置为 NULL,如下所示:
=> ALTER TABLE orderFact ALTER COLUMN cust_name SET USING NULL;
ALTER TABLE
=> SELECT REFRESH_COLUMNS('orderFact', 'cust_name', 'REBUILD');
REFRESH_COLUMNS
---------------------------
refresh_columns completed
(1 row)
=> SELECT order_id, order_date::date, cid, cust_name, cust_gender, amount FROM orderFact ORDER BY cid;
order_id | order_date | cid | cust_name | cust_gender | amount
----------+------------+-----+-----------+-------------+---------
100 | 2018-12-31 | 1 | | F | 15.00
200 | 2018-12-31 | 1 | | F | 1000.00
300 | 2018-12-31 | 2 | | M | -50.00
500 | 2018-12-31 | 2 | | M | 200.00
400 | 2018-12-31 | 3 | | F | 100.00
(5 rows)
有关详细信息,请参阅 定义列值
移除 SET USING 和 DEFAULT 约束
使用 ALTER TABLE...ALTER COLUMN 移除列的 SET USING
或 DEFAULT
约束,如下所示:
ALTER TABLE table-name ALTER COLUMN column-name DROP { SET USING | DEFAULT };
Vertica 会从指定列中移除约束,但该列及其数据不受影响。例如:
=> ALTER TABLE orderFact ALTER COLUMN cust_name DROP SET USING;
ALTER TABLE
删除由 SET USING 或 DEFAULT 查询的列
Vertica 强制实施修整表与其查询的表之间的依赖关系。除非删除操作还包含 CASCADE
选项,否则尝试删除查询的列或其表会返回错误。Vertica 通过从修整表中移除 SET USING 或 DEFAULT 约束来实施 CASCADE
。表列及其数据不受影响。
例如,尝试删除表 custDim
中的列 name
会返回回退错误,因为该列由 SET USING 列 orderFact.cust_gender
引用:
=> ALTER TABLE custDim DROP COLUMN gender;
ROLLBACK 7301: Cannot drop column "gender" since it is referenced in the default expression of table "public.orderFact", column "cust_gender"
要删除此列,请使用 CASCADE
选项:
=> ALTER TABLE custDim DROP COLUMN gender CASCADE;
ALTER TABLE
在执行操作过程中,Vertica 从 orderFact.cust_gender
中移除 DEFAULT 约束。不过,cust_gender
会保留先前从删除的 custDim.gender
列中查询的数据:
=> SELECT EXPORT_TABLES('','orderFact');
EXPORT_TABLES
------------------------------------------------------------------------------------------------------------
CREATE TABLE public.orderFact
(
order_id int NOT NULL,
order_date timestamp NOT NULL DEFAULT (now())::timestamptz(6),
cid int,
cust_name varchar(20),
cust_gender varchar(1) SET USING NULL,
amount numeric(12,2),
CONSTRAINT C_PRIMARY PRIMARY KEY (order_id) DISABLED
)
PARTITION BY ((orderFact.order_date)::date) GROUP BY (CASE WHEN ("datediff"('year', (orderFact.order_date)::date, ((now())::timestamptz(6))::date) >= 2) THEN (date_trunc('year', (orderFact.order_date)::date))::date WHEN ("datediff"('month', (orderFact.order_date)::date, ((now())::timestamptz(6))::date) >= 2) THEN (date_trunc('month', (orderFact.order_date)::date))::date ELSE (orderFact.order_date)::date END);
ALTER TABLE public.orderFact ADD CONSTRAINT C_FOREIGN FOREIGN KEY (cid) references public.custDim (cid);
(1 row)
=> SELECT * FROM orderFact;
order_id | order_date | cid | cust_name | cust_gender | amount
----------+----------------------------+-----+-----------+-------------+---------
400 | 2021-01-05 13:27:56.026115 | 3 | | F | 100.00
300 | 2021-01-05 13:27:56.026115 | 2 | | F | -50.00
200 | 2021-01-05 13:27:56.026115 | 1 | | F | 1000.00
500 | 2021-01-05 13:30:18.138386 | 3 | | F | 750.00
100 | 2021-01-05 13:27:56.026115 | 1 | | F | 15.00
500 | 2021-01-05 13:27:56.026115 | 2 | | F | 200.00
(6 rows)