Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
:sectnums:
:sectnumlevels: 5

= 约束列视图实现

== 将约束映射到列

`contrib/ivorysql_ora/src/sysview/sysview--1.0.sql` 中的定义通过约束的关系和列编号关联 `pg_class` 与 `pg_attribute`。列位置来自它在 `pg_constraint.conkey` 数组中的序号。`ALL_CONS_COLUMNS` 展开数组并保留序号,另外两个视图在数组中查找对应列的位置。

== 应用可见性规则

`USER_CONS_COLUMNS` 按表所有者过滤。`ALL_CONS_COLUMNS` 检查模式使用权限和表查询权限,并单独列出主键之外的列级 `NOT NULL` 属性。`DBA_CONS_COLUMNS` 列出普通表上的约束及单独查询得到的 `NOT NULL` 属性。各视图都对名称应用 Oracle 标识符大小写转换。

使用列序号的查询示例见 xref:master/oracle_compatibility/compat_xxx_cons_columns_view.adoc[约束列视图]。
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
:sectnums:
:sectnumlevels: 5

= 约束列视图

== 选择视图

Oracle 兼容视图 `SYS.USER_CONS_COLUMNS`、`SYS.ALL_CONS_COLUMNS` 和 `SYS.DBA_CONS_COLUMNS` 列出约束所用的列。每行包含表、约束、列及该列在约束中的位置。位置从 1 开始。

- 查询当前用户拥有的表时,使用 `USER_CONS_COLUMNS`。
- 查询当前用户拥有表 `SELECT` 权限且对其模式具有 `USAGE` 权限的表时,使用 `ALL_CONS_COLUMNS`。
- 查看整个数据库的普通表时,使用 `DBA_CONS_COLUMNS`。视图访问由其授权控制。

== 查看组合主键

在 Oracle 兼容会话中,选择可创建表的临时测试模式并执行以下语句。

[source,sql]
----
CREATE TABLE cons_columns_demo (
tenant_id integer,
invoice_id integer,
CONSTRAINT cons_columns_demo_pk PRIMARY KEY (invoice_id, tenant_id)
);

SELECT lower(column_name) AS column_name, position
FROM sys.user_cons_columns
WHERE lower(table_name) = 'cons_columns_demo'
AND lower(constraint_name) = 'cons_columns_demo_pk'
ORDER BY position;

DROP TABLE cons_columns_demo;
----

结果显示 `invoice_id` 位于第 1 位,`tenant_id` 位于第 2 位。按 `position` 排序可还原约束中的列顺序,它可能与表中的列顺序不同。

实现细节见 xref:master/compatibility_features_design/xxx_cons_columns_view_impl.adoc[约束列视图实现]。
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
:sectnums:
:sectnumlevels: 5

= Constraint Column View Implementation

== Map constraints to columns

The definitions in `contrib/ivorysql_ora/src/sysview/sysview--1.0.sql` join a constraint's relation and column numbers to `pg_class` and `pg_attribute`. The column's ordinal comes from its entry in `pg_constraint.conkey`. `ALL_CONS_COLUMNS` expands the array with ordinality. The other views look up the matching column's position in the array.

== Apply visibility rules

`USER_CONS_COLUMNS` filters by table owner. `ALL_CONS_COLUMNS` checks schema usage and table select privileges, and it includes a separate projection for column-level `NOT NULL` attributes outside primary keys. `DBA_CONS_COLUMNS` projects regular-table constraints and includes a separate projection for `NOT NULL` attributes. Each view applies the Oracle identifier case conversion to its names.

For a query that uses the ordinal, see xref:master/oracle_compatibility/compat_xxx_cons_columns_view.adoc[constraint column views].
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
:sectnums:
:sectnumlevels: 5

= Constraint Column Views

== Choose a view

The Oracle-compatible `SYS.USER_CONS_COLUMNS`, `SYS.ALL_CONS_COLUMNS` and `SYS.DBA_CONS_COLUMNS` views list the columns used by constraints. Each row identifies the table, constraint, column and its one-based position within the constraint.

- Use `USER_CONS_COLUMNS` for tables owned by the current user.
- Use `ALL_CONS_COLUMNS` for tables for which the current user has `SELECT` and schema `USAGE` privileges.
- Use `DBA_CONS_COLUMNS` for a database-wide inventory of regular tables. Access to the view is controlled by its grants.

== Inspect a composite key

Run the following in an Oracle-mode session in a scratch schema where you can create a table.

[source,sql]
----
CREATE TABLE cons_columns_demo (
tenant_id integer,
invoice_id integer,
CONSTRAINT cons_columns_demo_pk PRIMARY KEY (invoice_id, tenant_id)
);

SELECT lower(column_name) AS column_name, position
FROM sys.user_cons_columns
WHERE lower(table_name) = 'cons_columns_demo'
AND lower(constraint_name) = 'cons_columns_demo_pk'
ORDER BY position;

DROP TABLE cons_columns_demo;
----

The result lists `invoice_id` at position 1 and `tenant_id` at position 2. Order by `position` to recover the constraint order, which can differ from the table's column order.

See xref:master/compatibility_features_design/xxx_cons_columns_view_impl.adoc[constraint column view implementation].
Loading