在MySQL中转义关键字时是否应该使用反引号

在MySQL中转义关键字时是否应该使用反引号

本文介绍了在MySQL中转义关键字时是否应该使用反引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否应将MySQL中的所有表名都放在反引号(`)中,以防止与保留关键字发生冲突?我问的原因是因为它们的使用使SQL的可移植性降低,因为并非所有数据库都允许反引号.

Should all table names in MySQL be enclosed in backticks (`) to prevent collisions with reserved keywords? The reason I ask is because their use makes the SQL less portable as not all databases allow backticks.

那么避免表和列名包含关键字是更好的做法吗?如果是这样,可以采取什么措施减轻MySQL在下一版本中添加一个可能与您的架构冲突的新关键字的风险.

So would avoiding table and column names containing keywords be a better course of action? If so what can be done to mitigate the risk of MySQL adding a new keyword in the next version that might collide with your schema.

是否有关于此的最佳实践?

Is there a best practice regarding this?

推荐答案

(在系统之间)最可移植的方法是使用双引号,但是,这将需要启用ANSI_QUOTES,在大多数安装中该默认情况下处于禁用状态.

The most portable way (between the systems) is to use double quotes, however, it would require enabling ANSI_QUOTES which is off by default on most installations.

因此,尽管在保持不同引擎之间有用的兼容性(并且不兼容性不仅将自身限制于反引号,还限制了MySQL与其他系统之间不计其数的其他事物),您正在杀死MySQL的不同设置之间的兼容性,这是到目前为止更为重要.

So while keeping arguably useful compatibility between different engines (and incompatibility does not limit itself to backticks only but to zillion other things different between MySQL and other systems) you are killing the compatibility between different setups of MySQL which is by far more important.

避免使用保留关键字始终是最佳解决方案.

Avoiding the reserved keywords is always the best solution.

这篇关于在MySQL中转义关键字时是否应该使用反引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:42