Is name a reserved word in MySQL?

后端 未结 6 1526
傲寒
傲寒 2021-01-18 12:38

Can I name a column name in my mysql tables?

相关标签:
6条回答
  • 2021-01-18 12:58

    Just a word of caution here. As field name name is fine.

    However in stored procedures name as variable does something else.

    Consider the following procedure. When I called this procedure it always would alter the first record in my config database. Once I renamed the variable named name to something else, things worked fine.

    PROCEDURE `updateConfig`(IN name VARCHAR(255), IN val VARCHAR(255))
    BEGIN
    UPDATE LOW_PRIORITY `config`
      SET `value`=val
      WHERE `name` = name
      LIMIT 1;
    END
    
    0 讨论(0)
  • 2021-01-18 13:07

    Regardless of whether or not you can, you might want to consider why you want to do it in the first place. Name of what? What kind of name? After a few years, if you look at the database schema, will you still remember these details?

    I suggest it would be in your best interest to treat the column name the same as you would treat any variable names, and give it a descriptive name. Years down the road, you (or anyone who maintains your code) will be thanking you.

    0 讨论(0)
  • 2021-01-18 13:12
    CREATE TABLE mytest (name INT);
    
    SELECT  *
    FROM    mytest;
    
    name
    --------
    

    In dubious cases you may enclose names into grave accents:

    CREATE TABLE `CREATE TABLE` (name INT);
    
    SELECT  *
    FROM    `CREATE TABLE`;
    
    name
    --------
    
    0 讨论(0)
  • 2021-01-18 13:13

    You should be fine calling a column 'name'. Check out http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html for more details on how to deal with reserved words.

    Wrapping column names in the (`) character ensures that even reserved words won't be misinterpreted.

    0 讨论(0)
  • 2021-01-18 13:15

    Why not just try it?

    mysql> select name from name;
    +------+
    | name |
    +------+
    |    1 |
    +------+
    

    Also you can quote any names with ``.

    mysql> select `from` from `from`;
    +------+
    | from |
    +------+
    |    1 |
    +------+
    
    0 讨论(0)
  • 2021-01-18 13:20

    Yes even though if its a person name I would recommend FirstName or FullName to be the column name to be more specific MYSQL Reserved Words

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