oracle 查询唯一键,ORACLE: 查询(看)表的主键、外键、唯一性约束 和 索引
时间: 2024-03-09 07:51:16 浏览: 57
你可以使用如下语句查询表的主键、外键、唯一性约束和索引:
查询主键:
```
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = '表名'
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;
```
查询外键:
```
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = '表名'
AND cons.constraint_type = 'R'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;
```
查询唯一性约束:
```
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = '表名'
AND cons.constraint_type = 'U'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;
```
查询索引:
```
SELECT index_name, uniqueness, table_name, column_name, column_position
FROM all_ind_columns
WHERE table_name = '表名'
ORDER BY index_name, column_position;
```
其中,'表名'为你要查询的表的名称。
阅读全文