mysql custom global defined variable

前端 未结 4 1586
逝去的感伤
逝去的感伤 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 00:55

    Since MySQL 5.5 it's not possible to set a global user-defined variable.

    A work-around might be to create a stored procedure that would return what you need.

    DROP PROCEDURE IF EXISTS HOUSE_SMALL_TYPE;
    DELIMITER //
    CREATE PROCEDURE HOUSE_SMALL_TYPE ()
    BEGIN 
    SELECT 0;
    END//
    DELIMITER ;
    

    and then call it.

    CALL HOUSE_SMALL_TYPE();
    

    The DROP statement is required in order to be able to modify it.

    0 讨论(0)
  • 2021-01-21 01:03

    IMHO, MySQL has a huge gap in this area, apparently in the latter versions. One alternative might have been to resort to setting OS environment variables, but how such values can be retrieved from within MySQL, I've been unable to see.

    There's a whole page here: https://dev.mysql.com/doc/refman/5.0/en/setting-environment-variables.html teaching us how to "set" OS environment variables in the shell, but not a word on actually calling such variables in MySQL.

    As another workaround, using a FUNCTION might be considered more lightweight than a STORED PROCEDURE, like so:

    CREATE DEFINER=`root`@`localhost` FUNCTION `DEFAULT_COUNTRY_CODE`() RETURNS CHAR(4)
    DETERMINISTIC
    RETURN '+234';
    

    Elsewhere in your query, you can then do:

    SELECT CONCAT(DEFAULT_COUNTRY_CODE(), "-", telephone) FROM contacts WHERE CountryCode = "NGA"
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-21 01:15

    Your approach is fine, if you want to see the values in MySQL instead of 1, 2, 3 etc. then consider this:

    define('HOUSE_SMALL_TYPE', 'HOUSE_SMALL_TYPE');
    define('HOUSE_MEDIUM_TYPE', 'HOUSE_MEDIUM_TYPE');
    

    Then in MySQL you can use:

    SELECT * FROM house  WHERE type = 'HOUSE_SMALL_TYPE';
    

    You just need to remember that you cannot just jam any value you like into house.type without having support for it in PHP.

    Even better consider this:

    class HouseType {
      const SMALL = 'SMALL';
      const MEDIUM = 'MEDIUM';
    }
    

    or

    class House {
      const TYPE_SMALL = 'SMALL';
      const TYPE_MEDIUM = 'MEDIUM';
    }
    

    because then you can use HouseType::SMALL or House::TYPE_SMALL in your PHP code rather than using a global define. By doing this you may benefit from code completion in some IDE's.

    0 讨论(0)
提交回复
热议问题