Heim >Datenbank >MySQL-Tutorial >Oracle查询数据表结构(字段,类型,大小,备注)

Oracle查询数据表结构(字段,类型,大小,备注)

WBOY
WBOYOriginal
2016-06-07 15:02:501332Durchsuche

创建表的SQL语句如下 -- 创建数据表 create table TABLE_TEST ( NAME varchar2(40) not null, SEX varchar2(1) default ''''Y'''' not null, BIRTHDAY date not null, HEIGHT number(3,2), WEIGHT number(3,2), MEMO blob ); -- 给列添加备注 comment on col

创建表的SQL语句如下
-- 创建数据表
create table TABLE_TEST
(
    NAME varchar2(40) not null,
    SEX varchar2(1) default ''''Y'''' not null,
    BIRTHDAY date not null,
    HEIGHT number(3,2),
    WEIGHT number(3,2),
    MEMO blob
);
-- 给列添加备注
  comment on column TABLE_TEST.NAME is ''''姓名
'''';
  comment on column TABLE_TEST.SEX is ''''
性别
'''';
  comment on column TABLE_TEST.BIRTHDAY is ''''
生日
'''';
  comment on column TABLE_TEST.HEIGHT is ''''
身高
'''';
  comment on column TABLE_TEST.WEIGHT is ''''
体重
'''';
  comment on column TABLE_TEST.MEMO is ''''
备注'''';
-- 创建约束关系 主键 外键 其他
  alter table TABLE_TEST add constraint TB_TEST_P_NAME primary key (NAME);

数据表创建完毕,执行下列SQL语句:
select
    A.column_name
字段名,A.data_type 数据类型,A.data_length 长度,A.data_precision 整数位
,
    A.Data_Scale
小数位,A.nullable 允许空值,A.Data_default 缺省值,B.comments
备注
from
    user_tab_columns A,user_col_comments B
where
    A.Table_Name = B.Table_Name
    and A.Column_Name = B.Column_Name
    and A.Table_Name = ''''TABLE_TEST''''

我们可以得出一下结果:

<strong><span><span>字段名</span></span></strong>
<strong><span><span>数据类型</span></span></strong>
<strong><span><span>长度</span></span></strong>
<strong><span><span>整数位</span></span></strong>
<strong><span><span>小数位</span></span></strong>
<strong><span><span>允许空值</span></span></strong>
<strong><span><span>缺省值</span></span></strong>
<strong><span><span>备注</span></span></strong>
<span><span>NAME</span></span>
<span><span>VARCHAR2</span></span>
<span><span>40</span></span>
<span></span>
<span></span>
<span><span>N</span></span>
<span><span><long></long></span></span>
<span><span>姓名</span></span>
<span><span>SEX</span></span>
<span><span>VARCHAR2</span></span>
<span><span>1</span></span>
<span></span>
<span></span>
<span><span>N</span></span>
<span><span><long></long></span></span>
<span><span>性别</span></span>
<span><span>BIRTHDAY</span></span>
<span><span>DATE</span></span>
<span><span>7</span></span>
<span></span>
<span></span>
<span><span>N</span></span>
<span><span><long></long></span></span>
<span><span>生日</span></span>
<span><span>HEIGHT</span></span>
<span><span>NUMBER</span></span>
<span><span>22</span></span>
<span><span>3</span></span>
<span><span>2</span></span>
<span><span>Y</span></span>
<span><span><long></long></span></span>
<span><span>身高</span></span>
<span><span>WEIGHT</span></span>
<span><span>NUMBER</span></span>
<span><span>22</span></span>
<span><span>3</span></span>
<span><span>2</span></span>
<span><span>Y</span></span>
<span><span><long></long></span></span>
<span><span>体重</span></span>
<span><span>MEMO</span></span>
<span><span>BLOB</span></span>
<span><span>4000</span></span>
<span></span>
<span></span>
<span><span>Y</span></span>
<span><span><long></long></span></span>
<span><span>备注</span></span>

这样,我们在进行程序设计时,通过一条简单的SQL语句,然好通过Ole调用Word,便可为最终用户导出完整的数据库表字典文档。
再执行下列SQL语句:
select
    INDEX_NAME
索引名,INDEX_TYPE 索引类型,UNIQUENESS
索引类别
from
    user_indexes
where
    TABLE_NAME = ''''TABLE_TEST''''

得到结果如下(注:SYS_IL0000031226C00006$$索引为系统在创建数据库表时自动创建的,用于数据库表内容的维护):

<strong><span><span> </span></span></strong>
<strong><span><span>索引名</span></span></strong>
<strong><span><span>索引类型</span></span></strong>
<strong><span><span>索引类别</span></span></strong>
<span><span>1</span></span>
<span><span>SYS_IL0000031226C00006$$</span></span>
<span><span>LOB</span></span>
<span><span>UNIQUE</span></span>
<span><span>2</span></span>
<span><span>TB_TEST_P_NAME</span></span>
<span><span>NORMAL</span></span>
<span><span>UNIQUE</span></span>

 

执行下列SQL语句,我们将得到更多的关于数据库表结构的信息:
select
    A.column_name
字段名,A.data_type 数据类型,A.data_length 长度,A.data_precision 整数位
,
    A.Data_Scale
小数位,A.nullable 允许空值,A.Data_default 缺省值,B.comments 备注
,
    C.IndexCount
索引次数
from
    user_tab_columns A,
    user_col_comments B,
    (select count(*) IndexCount,Column_Name from User_Ind_Columns where Table_Name = ''''TABLE_TEST'''' group by Column_Name) C
where
    A.Table_Name = B.Table_Name
    and A.Column_Name = B.Column_Name
    and A.Column_Name = C.Column_Name(+)
    and A.Table_Name = ''''TABLE_TEST''''

得到结果如下:

<strong><span><span>字段名</span></span></strong>
<strong><span><span>数据类型</span></span></strong>
<strong><span><span>长度</span></span></strong>
<strong><span><span>整数位</span></span></strong>
<strong><span><span>小数位</span></span></strong>
<strong><span><span>允许空值</span></span></strong>
<strong><span><span>缺省值</span></span></strong>
<strong><span><span>备注</span></span></strong>
<strong><span><span>索引次数</span></span></strong>
<span><span>BIRTHDAY</span></span>
<span><span>DATE</span></span>
<span><span>7</span></span>
<span></span>
<span></span>
<span><span>N</span></span>
<span><span><long></long></span></span>
<span><span>生日</span></span>
<span></span>
<span><span>HEIGHT</span></span>
<span><span>NUMBER</span></span>
<span><span>22</span></span>
<span><span>3</span></span>
<span><span>2</span></span>
<span><span>Y</span></span>
<span><span><long></long></span></span>
<span><span>身高</span></span>
<span></span>
<span><span>MEMO</span></span>
<span><span>BLOB</span></span>
<span><span>4000</span></span>
<span></span>
<span></span>
<span><span>Y</span></span>
<span><span><long></long></span></span>
<span><span>备注</span></span>
<span></span>
<span><span>NAME</span></span>
<span><span>VARCHAR2</span></span>
<span><span>40</span></span>
<span></span>
<span></span>
<span><span>N</span></span>
<span><span><long></long></span></span>
<span><span>姓名</span></span>
<span><span>1</span></span>
<span><span>SEX</span></span>
<span><span>VARCHAR2</span></span>
<span><span>1</span></span>
<span></span>
<span></span>
<span><span>N</span></span>
<span><span><long></long></span></span>
<span><span>性别</span></span>
<span></span>
<span><span>WEIGHT</span></span>
<span><span>NUMBER</span></span>
<span><span>22</span></span>
<span><span>3</span></span>
<span><span>2</span></span>
<span><span>Y</span></span>
<span><span><long></long></span></span>
<span><span>体重</span></span>
<span></span>

 

    当然Oracle数据字典的应用远不止这些,通过Oracle数据库字典的支持,我们可以得到Oracle数据库结构的所有信息,著名的数据库开发工具PL/SQL Developer完全就是基于Oracle的数据库字典实现的。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn