在Oracle SQL中,如何使用一个规则创建一个值以默认为一个值,以默认为插入时同一行中另一个值的百分比?

发布于 2025-02-08 08:56:37 字数 249 浏览 2 评论 0原文

如何更改创建表格块,以使“佣金”默认为“价格”的百分比?

例如,在进行插入并输入佣金中时,是否有一种方法可以使“佣金”自动为“价格”的5%?

CREATE TABLE SALE (
    price NUMBER (5, 2),
    commission NUMBER (5, 2)
);

INSERT INTO SALE (price, commission)
    VALUES(1000, NULL)
;

How can I change the CREATE TABLE block so that 'commission' will default to a percentage of 'price'?

For example, is there a way to have 'commission' automatically be a 5% of 'price' when doing an INSERT and entering NULL for commission?

CREATE TABLE SALE (
    price NUMBER (5, 2),
    commission NUMBER (5, 2)
);

INSERT INTO SALE (price, commission)
    VALUES(1000, NULL)
;

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

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

发布评论

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

评论(1

还在原地等你 2025-02-15 08:56:37

您无法使用另一列的默认值创建表

对默认列值的限制默认列值受到以下限制:

默认表达式不能包含对PL/SQL函数的引用或其他列,PseudoColumns级别,先验和Rownum或尚未完全指定的日期常数。

表达式除标量子查询表达式外,可以是任何形式。

oracle

文档插入。但这是另一个问题。堆栈溢出的规则是在一篇文章中不问多个问题。

You can not create table with a default value of another column

Restrictions on Default Column Values Default column values are subject to the following restrictions:

A DEFAULT expression cannot contain references to PL/SQL functions or to other columns, the pseudocolumns LEVEL, PRIOR, and ROWNUM, or date constants that are not fully specified.

The expression can be of any form except a scalar subquery expression.

ORACLE documentation

You can create a trigger for the INSERT. But that is another question. And Stack Overflow has a rule of not asking multiple questions in one post.

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