oracle、mysql&mariadb、postgresql中表名、列名的默认大小写问题

  从https://zhuanlan.zhihu.com/p/378495347https://blog.csdn.net/qq_43454016/article/details/123231325,可知,mysql表名的大小写敏感取决于lower_case_table_names参数的设置,而字段名是不区分大小写。无论是否用``括起来都一样,相当于给查询中出现的文本都加了"col_NAME",如下:

select * from act_hi_actinst aha2;
id_|proc_def_id_|proc_inst_id_|execution_id_|act_id_|task_id_|call_proc_inst_id_|act_name_|act_type_|assignee_|start_time_|end_time_|duration_|tenant_id_|
---+------------+-------------+-------------+-------+--------+------------------+---------+---------+---------+-----------+---------+---------+----------+

select ID_ as "ID_",PROC_def_id_ as "PROC_def_id_" from act_hi_actinst aha;
ID_|PROC_def_id_|
---+------------+

这和oracle/postgresql使用""括起来就大小写敏感不同。

create table Tab1(id int);
create table TAB2(id int);
create table `tab3`(id int);
create table `TAB4`(`ID` int);
create table `tAB5`(`iD` int);
create table `tab6`(iD int);
create table `tAB7`(`iD` int,id int);
SQL 错误 [1060] [42S21]: Duplicate column name 'id'

show variables like '%lower_case_table_names%'
lower_case_table_names=1
alter table t1 add column "C_x" varchar(10);
select * from t1 limit 1;
select "C_x" || 1 C_x from t1;
select c_x from t1;
SQL 错误 [42703]: 错误: 字段 "c_x" 不存在
  Hint: 也许您想要引用列"t1.C_x"。
  Position: 8
  
select "C_x" || 1 C_x from t1 limit 1;
c_x|
---+
   |
   

lightdb_lower_case_column_names
0 lightdb模式,默认返回小写
1 mysql模式,返回SQL语句编写的大小写,为字段名增加as "字段名"别名实现,select *需要展开后处理,仅限于顶层select,其他的的insert select, ctas, 子查询中的全部不改写
2 oracle模式,默认返回大写,为不带""的字段名增加as "UPPER字段名"别名实现,select *需要展开后处理,仅限于顶层select 
对于oracle
/mysql模式,如果别名不带双引号,则需要按照上述规则进行大小写处理,然后给添加上双引号,确保不管是否包含别名结果都是正确的。

select "C_x" || 1 "C_x1" from t1 order by "C_x1"; -- 同样,order by也需要双引号括起来。

posted @ 2022-12-29 15:43  zhjh256  阅读(345)  评论(0编辑  收藏  举报