Home  >  Q&A  >  body text

如何防止 MySQL 中纯数字数据的 Insert?

比如我不希望 username 是纯数字的,如果程序 insert 的数据,username 是纯数字的,就返回 mysql 数据格式错误,有这种功能吗?

大家讲道理大家讲道理2742 days ago674

reply all(5)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 14:49:14

    Achieved through stored procedures or triggers, but this logic is best controlled by the code layer

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:49:14

    Let’s talk about the conclusion first: I agree with LS’s point of view. Generally, verification is written in the program, and the database only does basic constraints such as unique

    If you really want to do it in the database, you will usually write trigger

    Then there seems to be add constraint check, but even if mysql supports this, it will not check before storage (all engines)

    http://stackoverflow.com/questions/16005283/is-it-possible-to-enforce-data-checking-in-mysql-using-regular-expression

    reply
    0
  • PHPz

    PHPz2017-04-17 14:49:14

    Program logic problems should not be left to the database to handle, or it should be used as the last line of verification defense.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 14:49:14

    Check before or during submission.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 14:49:14

    This kind of basic verification is also done in the database. Doesn’t this just put more pressure on the database? Everyone is reducing the burden on it

    reply
    0
  • Cancelreply