有時候需要導出當前Oracle賬戶下所有的表結構信息,在其他Oracle測試庫重建,或者遷移到MySQL數據庫中
雖然可以用工具,但是本人還是習慣自己動手
本腳本會將分區表處理爲非分區表,如需添加分區信息,自己改腳本
本腳本只支持number,char,varchar2,date,timestamp數據類型
如需支持更多數據類型,自己修改腳本
如需將Oracle數據類型轉換爲MySQL數據類型,請看後面
set pages 10000
set feedback off
col table_ddl_in_current_user format a150
with tab_column as
(select table_name,
column_name,
column_id,
data_type,
data_length,
data_precision,
data_scale,
nullable
from user_tab_columns),
tab as
(select table_name from user_tables)
select case
when column_id = 0 then
'create table ' || lower(table_name)
end || case
when column_id = 1 and max_colid = 1 then
'(' || trim(column_name)
when column_id = 1 and max_colid <> 1 then
'(' || trim(column_name) || ','
when column_id > 1 and column_id < max_colid then
column_name || ','
when column_id = max_colid then
column_name
end || case
when column_id = 999 then
');'
end as table_ddl_in_current_user
from (select table_name,
column_id,
max(column_id) over(partition by table_name) max_colid,
case
when data_type in ('DATE', 'TIMESTAMP(6)') then
' ' || lower(column_name) || ' ' || lower(data_type)
when data_type in ('CHAR', 'VARCHAR2') then
' ' || lower(column_name) || ' ' || lower(data_type) || '(' ||
data_length || ')'
when data_type = 'NUMBER' and data_precision is not null and
data_scale = 0 then
' ' || lower(column_name) || ' ' || lower(data_type) || '(' ||
data_precision || ')'
when data_type = 'NUMBER' and data_precision is not null and
data_scale > 0 then
' ' || lower(column_name) || ' ' || lower(data_type) || '(' ||
data_precision || ',' || data_scale || ')'
when data_type = 'NUMBER' and data_precision is null and
data_scale is null then
' ' || lower(column_name) || ' ' || lower(data_type)
end || case
when nullable = 'N' then
' not null'
end as column_name
from tab_column
where table_name in (select table_name from tab)
union all
select table_name,
-1 as column_id,
null as max_colid,
null as column_name
from tab
union all
select table_name,
0 as column_id,
null as max_colid,
null as column_name
from tab
union all
select table_name,
999 as column_id,
null as max_colid,
null as column_name
from tab
order by table_name, column_id);
抓出當前賬戶下主鍵,外鍵,索引,視圖,check約束,觸發器,函數,存儲過程等我就不貼了
想精通SQL優化?精通SQL? 精通系統優化?精通表設計?精通分庫分表? 加我微信 692162374 報個名學習吧
將Oracle建表語句轉爲MySQL建表語句
with tab_column
as
(select table_name,
column_name,
column_id,
data_type,
data_length,
data_precision,
data_scale,
nullable
from user_tab_columns ),
tab as
(select table_name from user_tables)
select case
when column_id = 0 then
'create table ' || lower(table_name)
end || case
when column_id = 1 and max_colid = 1 then
'(' || trim(column_name)
when column_id = 1 and max_colid <> 1 then
'(' || trim(column_name) || ','
when column_id > 1 and column_id < max_colid then
column_name || ','
when column_id = max_colid then
column_name
end || case
when column_id = 999 then
');'
end as table_ddl_in_current_user
from (select table_name,
column_id,
max(column_id) over(partition by table_name) max_colid,
case
when data_type in ('DATE', 'TIMESTAMP(6)') then
' ' || lower(column_name) || ' ' || 'datetime'
when data_type in ('VARCHAR2') then
' ' || lower(column_name) || ' ' || 'varchar' || '(' ||
data_length || ')'
when data_type in ('CHAR') then
' ' || lower(column_name) || ' ' || 'varchar' || '(' ||
data_length || ')'
when data_type = 'NUMBER' and data_precision is not null and
data_scale = 0 then
' ' || lower(column_name) || ' ' || 'int' || '(' ||
data_precision || ')'
when data_type = 'NUMBER' and data_precision is not null and
data_scale > 0 then
' ' || lower(column_name) || ' ' || 'decimal' || '(' ||
data_precision || ',' || data_scale || ')'
when data_type = 'NUMBER' and data_precision is null and
data_scale is null then
' ' || lower(column_name) || ' ' || 'int'
end || case
when nullable = 'N' then
' not null'
end as column_name
from tab_column
where table_name in (select table_name from tab)
union all
select table_name,
-1 as column_id,
null as max_colid,
null as column_name
from tab
union all
select table_name,
0 as column_id,
null as max_colid,
null as column_name
from tab
union all
select table_name,
999 as column_id,
null as max_colid,
null as column_name
from tab
order by table_name, column_id);