Using LIMIT 1 in MySQL

后端 未结 5 2070
猫巷女王i
猫巷女王i 2021-02-01 02:54

When is it appropriate to add LIMIT 1 at the end of the query in MySQL. I normally add it in DELETE but I\'ve seen it being used with INSERT a and even UPDATE. Is it an overkill

5条回答
  •  自闭症患者
    2021-02-01 03:40

    Assume you are writing Forgot Password code (in PHP):

    UPDATE
        `users`
    SET
        `pass` = PASSWORD('{$new_salted_pass}')
    WHERE
        `account_id` = '{$account_id}'
    LIMIT 1
    

    If your software is a Software as a Service (SaaS),

    and multiple organizations use your software,

    and account_id-organization_id fields together are a unique id,

    and the account_id is auto-generated in the code like so:

    JONES0001
    JONES0002
    JONES0003
    ...

    and the software engineer forgets to add AND organization_id = '{$organization_id}' to the query's WHERE condition,

    and the code passes QA when testing the Forgot Password functionality in QA environment,

    and the code is then pushed to production,

    then the LIMIT 1 can limit the damage done when two organizations' user's share the same account_id.

提交回复
热议问题