创建触发器
我只是想知道是否有人可以帮助我开始创建触发器
。
我需要创建一个触发器来检查 INSERT 语句中的某些字符,
例如:
INSERT INTO table
VALUES('ABC')
我想检查插入值是否有 A,然后是 B,然后是 C..
任何帮助都会很棒。 谢谢
Im just wondering if someone could help me get started with creating a trigger
.
I am needing to create a trigger that checks an INSERT
statement for certain chars
eg:
INSERT INTO table
VALUES('ABC')
I want to check that the insert value has the A, then B, then C..
Any help would be awesome.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行此操作的最佳方法 - 最好的方法是使用检查约束,因为它是性能最高、最简单且正确的方法。
对于您的原始示例,可能如下所示:
您在注释中给出的示例将不起作用,因为您将 TO_NUMBER 函数应用于非数字字符串,因为它包含冒号。它是否应该检查 DATE 列的时间元素?如果是这样,这可能会起作用:
确切的细节取决于您想要执行的规则。关键是完整性规则应该尽可能通过约束而不是触发器来强制执行,而且通常是可能的。
The best way of doing this - best because it is the most performant, the easiest, the proper way of doing this - would be to use a check constraint.
For your original example that might look like this:
The example you give in a comment won't work because you are applying a TO_NUMBER function to a string which is not numeric as it contains a colon. Is it supposed to be checking the time element of a DATE column? If so this could work:
The precise details depend on the rules you want to enforce. The key thing is that integrity rules should be enforced through constraints not triggers whenever possible, and it is usually possible.
这会在
插入
之前触发,检查值“ABC”。如果您希望它在插入
之后触发,请进行适当的更改。我们假设表table
有一个名为value
的字段。This triggers before the
insert
, checking for the value 'ABC'. If you want it to trigger after theinsert
, make the appropriate changes. We assume that the tabletable
has one field calledvalue
.检查创建触发器的文档,或者尝试此链接。要检查是否包含某些子字符串,请尝试 Instr 函数。
Check the Documentation for Creating Trigger, alternativly try this link. To check if certain substrings are included try the Instr function.