oracle 查询表名称,字段名,是否主键,字段描述,字段类型,字段长度,是否为空,是否自增序列,缺省值,约束,sql
时间: 2023-11-25 09:07:12 浏览: 131
您可以使用以下的 SQL 语句来查询 Oracle 数据库中表的结构信息:
```sql
SELECT
cols.table_name,
cols.column_name,
cols.nullable,
cols.data_type,
cols.data_length,
cols.data_precision,
cols.data_scale,
cols.data_default,
cols.column_id,
cols.data_default,
cons.constraint_type,
cons.constraint_name,
cons.r_constraint_name,
com.comments,
(
SELECT
CASE
WHEN COUNT(*)
OVER (PARTITION BY cols.table_name, cols.column_name) > 1
THEN 1
ELSE 0
END
FROM
all_tab_columns c
WHERE
c.owner = cols.owner
AND c.table_name = cols.table_name
AND c.column_name = cols.column_name
) AS is_duplicated
FROM
all_tab_columns cols
LEFT JOIN all_col_comments com ON cols.owner = com.owner AND cols.table_name = com.table_name AND cols.column_name = com.column_name
LEFT JOIN (
SELECT
cons.owner,
cons.table_name,
cols.column_name,
cons.constraint_type,
cons.constraint_name,
cons.r_constraint_name
FROM
all_constraints cons
JOIN all_cons_columns cols ON cols.owner = cons.owner AND cols.constraint_name = cons.constraint_name
WHERE
cons.constraint_type IN ('P', 'U')
) cons ON cols.owner = cons.owner AND cols.table_name = cons.table_name AND cols.column_name = cons.column_name
WHERE
cols.owner = 'YOUR_SCHEMA_NAME'
AND cols.table_name = 'YOUR_TABLE_NAME'
ORDER BY
cols.table_name,
cols.column_id;
```
其中,将 `YOUR_SCHEMA_NAME` 和 `YOUR_TABLE_NAME` 替换为您所需查询的表所在的 schema 名称和表名称。执行后,您将可以得到如下的表结构信息:
- `table_name`:表名称
- `column_name`:字段名称
- `nullable`:该字段是否可为空,值为 `Y` 或 `N`
- `data_type`:字段类型
- `data_length`:字段长度
- `data_precision`:字段精度
- `data_scale`:字段小数位数
- `data_default`:字段缺省值
- `column_id`:字段在表中的顺序
- `constraint_type`:约束类型,如 Primary Key、Unique、Foreign Key 等
- `constraint_name`:约束名称
- `r_constraint_name`:外键关联的主键名称
- `comments`:字段描述
- `is_duplicated`:是否为重复字段,值为 `1` 或 `0`
阅读全文