Alter列使其成为派生的列

发布于 2025-01-23 14:27:52 字数 346 浏览 0 评论 0原文

我正在学习PostgreSQL,我想知道可以在表中添加派生列,然后在另一行上更改它,我尝试了不同的方式,但是我找不到正确的语法。

出于某种原因,似乎这项演习希望在两行上想要它,但是我目前只能做到这一点。

-- 1) Add an attribute Taxes to table Payment
ALTER TABLE Payment
ADD Taxes real;

-- 2) Set the derived attribute taxes as 12% of PaySlip
ALTER TABLE Payment
ALTER COLUMN Taxes AS (0.12*Payment.PaySlip);

I'm learning PostgreSQL and I'm wondering is it possible to add a derived column to a table then ALTER it on a another line, I've tried different ways but I can't find the correct Syntax for it.

For some reason its seems as if the exercise wants it on two lines, but I can only do it on one at the moment.

-- 1) Add an attribute Taxes to table Payment
ALTER TABLE Payment
ADD Taxes real;

-- 2) Set the derived attribute taxes as 12% of PaySlip
ALTER TABLE Payment
ALTER COLUMN Taxes AS (0.12*Payment.PaySlip);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕嗳→ 2025-01-30 14:27:52

您可以尝试使用 a>

如果此列存在在您的表中,我们可能需要在创建它之前将其删除。

由于该文档可能不支持 a>带有生成的

alter [column] column_name添加生成{始终|默认情况下}作为Identity [(sequence_options)]

ALTER TABLE Payment DROP COLUMN Taxes;

ALTER TABLE Payment
ADD Taxes  [type of Taxes]  GENERATED ALWAYS AS (0.12 * PaySlip) stored;

You can try to use Generated Columns

If this column exists in your table, we might need to drop it before creating it.

Due to the document might not support ALTER COLUMN with GENERATED

ALTER [ COLUMN ] column_name ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]

ALTER TABLE Payment DROP COLUMN Taxes;

ALTER TABLE Payment
ADD Taxes  [type of Taxes]  GENERATED ALWAYS AS (0.12 * PaySlip) stored;

sqlfiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文