將oracle中的欄位和表名全部修改為小寫
在建立表和表結構的時候,如果想要小寫需要在名稱上面新增雙引號,如果不新增oracle資料庫會預設識別為大寫
1.將表名和欄位名改為大寫
批量將表名變為大寫
begin
for c in (select table_name tn from user_tables where table_name <> upper(table_name)) loop
begin
execute immediate 'alter table "'||c.tn||'" rename to '||c.tn;
exception
when others then
dbms_output.put_line(c.tn||'已存在');
end;
end loop;
end;
批量將空間內所有表的所有欄位名變成大寫 此方法可能導致溢位
begin
for t in (select table_name tn from user_tables) loop
begin
for c in (select column_name cn from user_tab_columns where table_name=t.tn) loop
begin
execute immediate 'alter table "'||t.tn||'" rename column "'||c.cn||'" to '||c.cn;
exception
when others then
dbms_output.put_line(t.tn||'.'||c.cn||'已經存在');
end;
end loop;
end;
end loop;
end;
將特點表PROPERTY_INFO的所有列名小寫變大寫
begin
for c in (select COLUMN_NAME cn from all_tab_columns where table_name='PROPERTY_INFO') loop
begin
execute immediate 'alter table PROPERTY_INFO rename column "'||c.cn||'" to '||c.cn;
exception
when others then
dbms_output.put_line('PROPERTY_INFO'||'.'||c.cn||'已經存在');
end;
end loop;
end;
2.將表名和欄位名改為小寫
①改表名為小寫
begin for c in (select table_name tn from user_tables where table_name <> lower(table_name)) loop begin execute immediate 'alter table '||c.tn||' rename to "'||lower(c.tn)||'"'; exception when others then dbms_output.put_line(c.tn||'已存在'); end; end loop; end;
②改欄位名為小寫
begin for t in (select table_name tn from user_tables) loop begin for c in (select column_name cn from user_tab_columns where table_name=t.tn) loop begin execute immediate 'alter table '||t.tn||' rename column '||c.cn||' to "'||lower(c.cn)||'"'; exception when others then dbms_output.put_line(t.tn||'.'||c.cn||'已經存在'); end; end loop; end; end loop; end;
注:
1.如果欄位和表名都要改為小寫,先改欄位,再改表。
2.如果表名獲取欄位名改為小寫以後,要在查詢語句中將表名和欄位名都要加上雙引號
例.
表名小寫 :select * from "department";
欄位名小寫:select "id" from "department";