天天看點

MySql 使用關鍵字做字段名

  • 在寫sql語句的時候,在特殊情況下有使用命名字段的時候,發現使用的字段是sql語言的關鍵字,但是我們又需要用到它作為字段名使用:
    mysql> create table test (id int auto_increment primary key, desc varchar(10));
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc varchar(10))' at line 1
               
  • 上面創表語句中的

    desc

    字段就是sql關鍵字,如果我們需要用到它作為字段創表成功,隻需要在字段前後加上這兩點即可 `desc` :
    mysql> create table test (id int auto_increment primary key, `desc` varchar(10));
    Query OK, 0 rows affected (0.02 sec)
               
  • 也不是隻有關鍵字才可以加這兩點,所有的字段名都可以加這兩個點,包括表名都可以:
    mysql> create table `test` (`id` int auto_increment primary key, `desc` varchar(10));
    Query OK, 0 rows affected (0.01 sec)