mysql custom global defined variable

前端 未结 4 1585
逝去的感伤
逝去的感伤 2021-01-21 00:10

In my database design, I tend to store some variable that is meant to be acting as a ROLE or TYPE as SMALLINT. For example :



        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 01:07

    I suggest using MySQL variables:

    SET HOUSE_SMALL_TYPE = 0;
    SET HOUSE_MEDIUM_TYPE = 1;
    

    Then, in your queries you may use these variables:

    SELECT * FROM house  WHERE type = @HOUSE_SMALL_TYPE;
    

    This method defines session variables:

    If you change a session system variable, the value remains in effect until your session ends or until you change the variable to a different value. The change is not visible to other clients.

    If you want to define global MySQL variables (available to all sessions):

    SET GLOBAL HOUSE_SMALL_TYPE = 0;
    SET GLOBAL HOUSE_MEDIUM_TYPE = 1;
    

    To indicate explicitly that a variable is a global variable, precede its name by GLOBAL or @@global.. The SUPER privilege is required to set global variables.

    Documentation:
    SET statement
    Using system variables
    User-defined variables

提交回复
热议问题