Oracle 触发器来保存值

发布于 2024-08-28 11:34:29 字数 205 浏览 2 评论 0原文

我有一个专栏,我需要能够保证永远不会被设置为“N”以外的任何内容 - 我认为触发器将是这个问题的完美解决方案,但我似乎不知道如何做到这一点每当列设置为“N”以外的其他值时,我将其重置回“N”

有任何指针吗?

编辑:我不想做一个约束,因为可能将其更改为 Y 的应用程序超出了我的控制范围,并且我不希望它在将其设置为 Y 时出现错误,我只想被动地设置它不张扬地回到N。

I have a column that I need to be able to guarantee never gets set to anything other than "N" - I thought a trigger would be the perfect solution for this, but I can't seem to figure out how to make it so that anytime the column gets set to something other than "N" I reset it back to "N"

Any pointers?

EDIT: I wouldn't want to do a constraint because the application that will potentially change it to Y is outside of my control and I don't want it to be getting errors when it sets it to Y, I just want to passively set it back to N without fanfare.

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

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

发布评论

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

评论(3

み零 2024-09-04 11:34:29

我建议使用列约束而不是触发器。更容易追踪和调试。但我假设你的意思是它永远不能设置为除 N 或 null 之外的任何值,对吧?如果不是,如果值始终为“N”,为什么还要存储它呢?

I would recommend a column constraint instead of a trigger. Easier to track down and debug. But I assume that you mean it could never be set to any value other than N or null, right? If not, why bother storing it if the value is always 'N'?

苦行僧 2024-09-04 11:34:29

也许是这样的。我知道这是一个坏主意,它会回来困扰你。另外,我已经有几年没有做过 Oracle 了,但这应该可以帮助你入门:

create or replace trigger trg_special_col before update on AnnoyingTable
for each row
begin
  :new.special_col := 'N';
end;

Something like this perhaps. I know it is a bad idea, and it will come back to haunt you. Also, I have not done Oracle in a few years, but this should get you started:

create or replace trigger trg_special_col before update on AnnoyingTable
for each row
begin
  :new.special_col := 'N';
end;
猥琐帝 2024-09-04 11:34:29

最简单的方法(如果您可以选择创建视图)是在表上提供一个视图,并将相关列的值设置为视图中的静态值“N” - 即将该值保留在的观点。

The easiest way (if the option is open to you to create views) is to provide a view on your table, and set the value of the column in question to a static value of "N" within the view - i.e. persist the value in the view.

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