在mysql中避免保留字的影响

如果在mysql中的sql语句中使用了mysql的保留字,就会报错,例如下面:


mysql> CREATE TABLE group (

    -> groupID tinyint(2) unsigned NOT NULL auto_increment,

    -> groupName varchar(60) default NULL,

    -> groupPriority tinyint(1) unsigned default '0',

    -> PRIMARY KEY  (groupID)

    -> ) TYPE=InnoDB;

ERROR 1064: 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 'group (

groupID tinyint(2) unsigned NOT NULL auto_increment,

gr

mysql>

上面就因为使用了保留字“group”出现错误。

只要把字段和表名用"`"引起来,就可以避免保留字的影响。这也是写规范的mysql语句的要求之一;

但是,为了避免不必要的麻烦,还是尽量不要在其中使用mysql的保留字。