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

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

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

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

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

测试:

  1. SQL> INSERT INTO test (name) VALUES ('L!t');
  2. INSERT INTO test (name) VALUES ('L!t')
  3. *
  4. ERROR at line 1:
  5. ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated
  6. SQL> INSERT INTO test (name) VALUES ('L@t');
  7. INSERT INTO test (name) VALUES ('L@t')
  8. *
  9. ERROR at line 1:
  10. ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated
  11. SQL> INSERT INTO test (name) VALUES ('L?t');
  12. INSERT INTO test (name) VALUES ('L?t')
  13. *
  14. ERROR at line 1:
  15. ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated
  16. SQL> INSERT INTO test (name) VALUES ('Lit');
  17. 1 row created.
  18. SQL>

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

  1. SQL> CREATE TABLE test
  2. 2 (
  3. 3 name VARCHAR2 (30)
  4. 4 );
  5. Table created.
  6. SQL> CREATE OR REPLACE TRIGGER trg_biu_test
  7. 2 BEFORE INSERT OR UPDATE
  8. 3 ON test
  9. 4 FOR EACH ROW
  10. 5 BEGIN
  11. 6 :new.name := REGEXP_REPLACE (:new.name, '!|@|$|\%|&amp;|\*|\?|<|>|-' , '');
  12. 7 END;
  13. 8 /
  14. Trigger created.
  15. SQL> INSERT INTO test (name) VALUES ('L@$%*?t');
  16. 1 row created.
  17. SQL> SELECT * FROM test;
  18. NAME
  19. ----
  20. Lt
  21. SQL>
英文:

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

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

Testing:

  1. SQL&gt; INSERT INTO test (name) VALUES (&#39;L!t&#39;);
  2. INSERT INTO test (name) VALUES (&#39;L!t&#39;)
  3. *
  4. ERROR at line 1:
  5. ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated
  6. SQL&gt; INSERT INTO test (name) VALUES (&#39;L@t&#39;);
  7. INSERT INTO test (name) VALUES (&#39;L@t&#39;)
  8. *
  9. ERROR at line 1:
  10. ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated
  11. SQL&gt; INSERT INTO test (name) VALUES (&#39;L?t&#39;);
  12. INSERT INTO test (name) VALUES (&#39;L?t&#39;)
  13. *
  14. ERROR at line 1:
  15. ORA-02290: check constraint (ZAHTJEV.CH_NAME) violated
  16. SQL&gt; INSERT INTO test (name) VALUES (&#39;Lit&#39;);
  17. 1 row created.
  18. SQL&gt;

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

  1. SQL&gt; CREATE TABLE test
  2. 2 (
  3. 3 name VARCHAR2 (30)
  4. 4 );
  5. Table created.
  6. SQL&gt; CREATE OR REPLACE TRIGGER trg_biu_test
  7. 2 BEFORE INSERT OR UPDATE
  8. 3 ON test
  9. 4 FOR EACH ROW
  10. 5 BEGIN
  11. 6 :new.name := REGEXP_REPLACE (:new.name, &#39;!|@|$|\%|&amp;|\*|\?|&lt;|&gt;|-&#39;, &#39;&#39;);
  12. 7 END;
  13. 8 /
  14. Trigger created.
  15. SQL&gt; INSERT INTO test (name) VALUES (&#39;L@$%*?t&#39;);
  16. 1 row created.
  17. SQL&gt; SELECT * FROM test;
  18. NAME
  19. ----
  20. Lt
  21. 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:

确定