查询oracle数据库的表结构,数据库字段名、表字段注释、 表字段属性、是否主键等

SELECT
    -- 获取字段名
    t1.Table_Name  AS "数据库字段名",
    -- 获取表注释(这里假设表注释存放在USER_TAB_COMMENTS视图中,你可能需要根据实际情况调整所属用户等条件)
    t3.comments AS "表注释",
    CASE WHEN pk.column_name IS NOT NULL THEN '是' ELSE '否' END AS "是否主键",
    t1.Table_Name || chr(13) || t3.comments       AS "表名称及说明",
    --t3.comments                                 AS "表说明",
    t1.Column_Name                                AS "字段名称",
    t1.DATA_TYPE || '(' || t1.DATA_LENGTH || ')'  AS "数据类型",

    -- 判断是否允许为空
    CASE WHEN t1.NullAble   = 'Y' THEN '是' ELSE '否' END AS "是否为空",
    t2.Comments                                   AS "字段说明",
    t1.Data_Default "默认值"
--       t4.created                                  AS "建表时间"
FROM cols t1
         LEFT JOIN user_col_comments t2
                   ON t1.Table_name = t2.Table_name
                       AND t1.Column_Name = t2.Column_Name
         LEFT JOIN user_tab_comments t3
                   ON t1.Table_name = t3.Table_name
         LEFT JOIN user_objects t4
                   ON t1.table_name = t4.OBJECT_NAME

    -- 关联主键相关视图判断是否主键
         LEFT JOIN (
    SELECT
        cc.column_name,
        cc.table_name
    FROM
        user_constraints c
            JOIN user_cons_columns cc ON c.constraint_name = cc.constraint_name
    WHERE
            c.constraint_type = 'P'
) pk ON t1.column_name = pk.column_name AND t1.table_name = pk.table_name
WHERE NOT EXISTS (SELECT t4.Object_Name
                  FROM User_objects t4
                  WHERE t4.Object_Type = 'TABLE'
                    AND t4.Temporary = 'Y'
                    AND t4.Object_Name = t1.Table_Name
)
  and t1.TABLE_NAME='你要查询的表' --这里注意哦换成自己要查的那个表的表名,如果去掉这句话会查出该数据库所有的表结构
ORDER BY t1.Table_Name, t1.Column_ID;
--以上所查的列如果不符合自己的要求 可以通过自己更改sql语句来获得想要查询的列,想要的属性上面几张系统表里都有,自己写也很简单

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值