Create a trigger in SQL that doesn't allow (or deletes) certain special characters like: $, @, ?, #, & and etc

huangapple go评论57阅读模式
英文:

Create a trigger in SQL that doesn't allow (or deletes) certain special characters like: $, @, ?, #, & and etc

问题

我一直在尝试创建一个触发器,当接收到特殊字符时,要么不允许插入,要么删除该字符,这适用于字符(!,@,$,%,&,*,?,<,>,-)。

我真的想不出适用于这种情况的任何触发器函数,也许将每个特殊字符替换为''...但我认为这可能不是最佳解决方案。

有没有任何在这种情况下使用的代码的想法?

在此先感谢您的帮助。

英文:

I've been trying to create a trigger that when receives an special character either doesn't allow the insert or delete the character, this being aplied to characters like (!, @, $, %, &, *, ?, <, >, -).

I really could not think in any trigger function that would work for this situation, maybe replace every special character by ''...but I think would not be the best solution.

Anyone have any idea of code to use in this situation?

thanks in advance for the help.

答案1

得分: 1

一种选项是创建检查约束(而不是触发器)。

SQL> CREATE TABLE test
  2  (
  3     name   VARCHAR2 (30)
  4              CONSTRAINT ch_name CHECK
  5                 (NOT REGEXP_LIKE (name, '!|@|$|\%|&amp;|\*|\?|<|>|-' ))
  6  );

Table created.

测试:

SQL> INSERT INTO test (name) VALUES ('L!t');
INSERT INTO test (name) VALUES ('L!t')
*
ERROR at line 1:
ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated


SQL> INSERT INTO test (name) VALUES ('L@t');
INSERT INTO test (name) VALUES ('L@t')
*
ERROR at line 1:
ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated


SQL> INSERT INTO test (name) VALUES ('L?t');
INSERT INTO test (name) VALUES ('L?t')
*
ERROR at line 1:
ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated


SQL> INSERT INTO test (name) VALUES ('Lit');

1 row created.

SQL>

如果要删除这些字符,那么使用触发器

SQL> CREATE TABLE test
  2  (
  3     name   VARCHAR2 (30)
  4  );

Table created.

SQL> CREATE OR REPLACE TRIGGER trg_biu_test
  2     BEFORE INSERT OR UPDATE
  3     ON test
  4     FOR EACH ROW
  5  BEGIN
  6     :new.name := REGEXP_REPLACE (:new.name, '!|@|$|\%|&amp;|\*|\?|<|>|-' , '');
  7  END;
  8  /

Trigger created.

SQL> INSERT INTO test (name) VALUES ('L@$%*?t');

1 row created.

SQL> SELECT * FROM test;

NAME
----
Lt

SQL>
英文:

One option is to create check constraint (not trigger).

SQL&gt; CREATE TABLE test
  2  (
  3     name   VARCHAR2 (30)
  4              CONSTRAINT ch_name CHECK
  5                 (NOT REGEXP_LIKE (name, &#39;!|@|$|\%|&amp;|\*|\?|&lt;|&gt;|-&#39;))
  6  );

Table created.

Testing:

SQL&gt; INSERT INTO test (name) VALUES (&#39;L!t&#39;);
INSERT INTO test (name) VALUES (&#39;L!t&#39;)
*
ERROR at line 1:
ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated


SQL&gt; INSERT INTO test (name) VALUES (&#39;L@t&#39;);
INSERT INTO test (name) VALUES (&#39;L@t&#39;)
*
ERROR at line 1:
ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated


SQL&gt; INSERT INTO test (name) VALUES (&#39;L?t&#39;);
INSERT INTO test (name) VALUES (&#39;L?t&#39;)
*
ERROR at line 1:
ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated


SQL&gt; INSERT INTO test (name) VALUES (&#39;Lit&#39;);

1 row created.

SQL&gt;

If you want to remove those characters, then use a trigger:

SQL&gt; CREATE TABLE test
  2  (
  3     name   VARCHAR2 (30)
  4  );

Table created.

SQL&gt; CREATE OR REPLACE TRIGGER trg_biu_test
  2     BEFORE INSERT OR UPDATE
  3     ON test
  4     FOR EACH ROW
  5  BEGIN
  6     :new.name := REGEXP_REPLACE (:new.name, &#39;!|@|$|\%|&amp;|\*|\?|&lt;|&gt;|-&#39;, &#39;&#39;);
  7  END;
  8  /

Trigger created.

SQL&gt; INSERT INTO test (name) VALUES (&#39;L@$%*?t&#39;);

1 row created.

SQL&gt; SELECT * FROM test;

NAME
----
Lt

SQL&gt;

huangapple
  • 本文由 发表于 2023年3月15日 21:36:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定