键盘上1左边,Esc下边那个键就是反引号键
在mysql中,desc是保留字,建表时不能用于字段名,表名:
mysql> create table desc (id varchar(255));
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 (id varchar(255))' at line 1
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 (id varchar(255))' at line 1
mysql> create table test3(desc varchar(255));
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(255))' at line 1
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(255))' at line 1
但将desc放到反引号中则正常
mysql> create table `desc` (id varchar(255));
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> create table test3 (`desc` varchar(255));
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> drop table desc;
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' at line 1
mysql> drop table `desc`;
Query OK, 0 rows affected (0.00 sec)
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' at line 1
mysql> drop table `desc`;
Query OK, 0 rows affected (0.00 sec)
有些管理员在建表时,为了省事,可能会选择从一个现有的表cp建表语句,修改创建.字段命名如果不规范的话,可能会用到mysql中的保留字,如上述的desc
例如从show create table xxx 或者mysqldump --no-data中取出建表语句,形式如下:
CREATE TABLE `test` (
`id` int(10) unsigned NOT NULL auto_increment,
`time` datetime NOT NULL default '0000-00-00 00:00:00',
`type` int(10) unsigned NOT NULL default '0',
`desc` varchar(255) default NULL,
primary key `i_id` (`id`)
) ENGINE=innodb DEFAULT CHARSET=latin1;
`id` int(10) unsigned NOT NULL auto_increment,
`time` datetime NOT NULL default '0000-00-00 00:00:00',
`type` int(10) unsigned NOT NULL default '0',
`desc` varchar(255) default NULL,
primary key `i_id` (`id`)
) ENGINE=innodb DEFAULT CHARSET=latin1;
创建成功后,在insert时如果不带反引号就会遇到问题
mysql> insert into test(`time`,`type`,`desc`) values(now(),5,'aa');
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
mysql> insert into test(time,type,desc) values(now(),5,'aa');
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) values(now(),5,'aa')' at line 1
mysql>
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) values(now(),5,'aa')' at line 1
mysql>
针对这个问题,有2个建议
1) 在建表时,不要用保留字作表名,字段名.可以加一个前缀标志加以区分,如在表名前加t_, 在列名前加F
2) 建表时,不要给表名/字段名/索引名加反引号,以免隐藏问题
反引号中带保留字建表时,mysql为什么不报错呢?
得先搞清楚反引号的本意,及show create table,mysqldump出来的数据带反引号的目的是什么.
有哪位知道,望不吝踢教~.~
相关资料:
mysql中的反引号与保留关键字desc问题来源网络,如有侵权请告知,即处理!
相关推荐
前言 MySQL和Oracle的关键字还是不尽相同的,在Oracle数据库中,我们的数据表中定义了大量的code...非保留关键字允许作为标识符,不需要加引号。 如果您要适用保留字作为标识符,就必须适用引号。 举个例子,BEGIN和E
C#在链接mysql数据库的时候 间歇性的会出现 打开链接的时候会报错. 调整mysql.data.dll 链接驱动版本. 将程序引用dll,替换为上传压缩包中的dll
MySQL关键字
可以用这批关键字去查询下自己数据库里是否存在这样的字段
mysql 参考手册(详解关键字的功能和涉及到的问题),帮助初学者解决困惑!
更改sq语句中的关键字,快速检索全局所包含关键字的内容及内容位置。
mysql 关键字 NO_WRITE_TO_BINLOG NULL NUMERICNO_WRITE_TO_BINLOG NULL NUMERICNO_WRITE_TO_BINLOG NULL NUMERIC
MY SQL 关键字 列出来所有的mysql关键字,本来是本人在建表的时候总是把字段的名称和mysql的关键字写成一样的了,所以整了个mysql关键字大全,来和大家分享下,希望搭建能够喜欢。
show status 与show variables 区别 1、show status 2、show variables 根据status状态对Mysql数据库进行优化: 1、连接数 1.1 show variables like ‘max_connections’; 1.2、 show global status like...
mysql中关键字的使用.txt
在动态拼接字符串时,我们常会用到字符拼接,我对拼接的引号不理解,如: 1、”’+ id +”’ 为什么是3个引号,为什么左边一个加号右边一个加号(能不能着重帮我解释下这个,详细点) SQL code sum(case Leave when...
详细介绍了mysql和sql server在语法、关键字等方面的一些区别
MySQL 是最流行的关系型数据库管理系统,在WEB应用方面 MySQL 是最好的RDBMS(Relational Database Management System:关系数据库管理...在本教程中,会让大家快速掌握 MySQL 的基本知识,并轻松使用 MySQL 数据库。
博文链接:https://argos.iteye.com/blog/67272
MySQL的保留字.pdf
数据库 关键字: 为了避免在数据库表结构设计过程中使用系统保留关键字我们必须知道数据库存在哪些关键字,接下来会列出mysql、oracle、sqlserver三个数据库各自的保留关键字。 Mysql ...
今天小编就为大家分享一篇解决python3插入mysql时内容带有引号的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧