日期:2014-05-16  浏览次数:20711 次

MySql-identifiers使用合格的名称
测试都是在world数据库下进行.
1.
select * from country;
select * from world.country;
2.
select name from country;
select country.name from country;
select world.country.name from country;
3.使用存储过程
db_name.routine_name
4.使用触发器
table_name.trigger_name
5.
To use quoted identifiers in a qualified name, quote them separately. For example, quote world.Country as `world`.`Country`, not as `world.Country`.
为了在一个限定名称中使用被单引号引用的标识符,那么就必须分别用单引号引用它们.
例如:用单引号(')引用world.country正确的方法是:'world'.'country',而不是'world.country'

6.使用mysql的保留字作为标识符
create table t(order int not null unique,d date not null);
--order是order by从句的保留字,所以在创建该表时将失败
select 1 as INTEGER;
--INTEGER是MYSQL数据库的保留字,所以在查询时将失败
那么我们如何使用MYSQL数据库中的保留字?

要使用MYSQL数据库中的保留字作为数据库,表,列或索引的标识符,这里有一??种或两种允许引用的样式,这取决于MYSQL服务器上的SQL模式。默认情况下,反引号('`')内(即:被反引号包围的字符)的字符允许它被用作一个标识符.如下:
create table t(`order` int not null unique,d date not null);
如果数据库的ANSI_QUOTES的SQL模式被激活,它也允许使用双引号(")引用保留字,如下:
create table t2("order" int not null unique,d date not null);
要使用一个保留字作为别名,使用单引号,双引号,或反引号引用都可以.SQL模式没有什么区别;使用三种引用中的任意一种都是合法的。
select 1 as 'INTEGER';
select 1 as "INTEGER";
select 1 as `INTEGER`;

MYSQL数据库中的保留字(关键字)和函数名称(mysql系统函数和用户自定义函数)是不区分大小写的.他们可以是大写,小写,甚至混合的情况下,不需要写成同样的方式在整个查询过程中.