oracle建表脚本导出,oracle中导出表结构,产生建表得脚本跟存储参数

该博客提供两种方法在Oracle中自动生成创建表的SQL脚本,包括表结构和存储参数。脚本利用USER_TAB_COLUMNS视图,通过SQL查询组合成建表语句,考虑了数据类型、是否允许为空以及长度、精度和规模等细节。适用于数据库管理和维护。
摘要由CSDN通过智能技术生成

oracle中导出表结构,产生建表得脚本跟存储参数 ----------------------------

/************************************************************/

/* 功能:自动产生创建表的脚本 */

/* 文件名:gen_cre_tab1.sql */

/************************************************************/

set linesize 500

set pagesize 1000

set arraysize 8

set feedback off

set heading off

select decode(t1.column_id,1,'CREATE TABLE '||t1.table_name|| ' (', ' ') a,

t1.column_name b,

t1.data_type||decode(t1.data_type,'DATE',

decode(t1.NULLABLE,'N',' not null'),

'VARCHAR2','('||to_char(t1.data_length)||')'||

decode(t1.NULLABLE,'N',' not null'),

'NUMBER',decode(t1.data_precision,null,‘ ‘ ,

'('||to_char(t1.data_precision)|| ','||to_char(t1.data_scale)||')' )

||decode(t1.NULLABLE,'N',' not null'), 'CHAR','('||to_char(t1.data_length)||')'||

decode(t1.NULLABLE,'N',' not null'))|| decode(t1.column_id,max(t2.column_id),');',',') c FROM user_tab_columns t1,user_tab_columns t2 WHERE t1.table_name = t2.table_name and

t1.table_name in (select distinct object_name from user_objects

where object_type='TABLE')

group by t1.column_id,t1.table_name,t1.data_type,t1.nullable,

t1.data_length,t1.data_scale,t1.column_name,

t1.data_precision

order by t1.table_name,t1.column_id;

例2.从USER_TABLES和USER_TAB_COLUMNS来产生创建表的脚本:

/************************************************************/

/* 功能:自动产生创建表的脚本和相应的存储参数 */

/* 文件名:gen_cre_tab2.sql */

/************************************************************/

set linesize 500

set pagesize 1000

set arraysize 8

set feedback off

set heading off

select decode(t1.column_id,1,'CREATE TABLE '||t1.table_name|| ' (', ' ') a, t1.column_name b, t1.data_type||decode(t1.data_type,'DATE',

decode(t1.NULLABLE,'N',' not null'),

'VARCHAR2','('||to_char(t1.data_length)||')'||

decode(t1.NULLABLE,'N',' not null'), 'NUMBER',decode(t1.data_precision,null,‘ ‘ ,

'('||to_char(t1.data_precision)|| ','||to_char(t1.data_scale)||')' )

||decode(t1.NULLABLE,'N',' not null'), 'CHAR','('||to_char(t1.data_length)||')'||

decode(t1.NULLABLE,'N',' not null'))|| decode(t1.column_id,max(t2.column_id),

')'||chr(10)|| 'TABLESPACE

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值