查询user_cons_columns表结构如下:
desc user_cons_columns;
Name Type Nullable Default Comments
--------------- -------------- -------- ------- ------------------------------------------------------------------------------------------------
OWNER VARCHAR2(30) Owner of the constraint definition
CONSTRAINT_NAME VARCHAR2(30) Name associated with the constraint definition
TABLE_NAME VARCHAR2(30) Name associated with table with constraint definition
COLUMN_NAME VARCHAR2(4000) Y Name associated with column or attribute of object column specified in the constraint definition
POSITION NUMBER Y Original position of column or attribute in definition
Owner 约束的所有者
Constraint_ Name 约束名
Table_Name 与约束关联的表名
Column_Name 与约束关联的列名
Position 约束定义中列的顺序
-- 这个 POSITION 是记录用户在创建约束(比如主键)时, 各个列的顺序
-- 观察一下这两个值
SQL>
SQL> col column_name format a30 ;
SQL> create table test(id int, name varchar2(10)) ;
Table created
SQL> alter table test add constraint PK_test primary key (id,name) ;
Table altered
SQL> select table_name, column_name,position
2 from user_cons_columns where constraint_name = 'PK_TEST' ;
TABLE_NAME COLUMN_NAME POSITION
------------------------------ ------------------------------ ----------
TEST ID 1
TEST NAME 2
SQL> alter table test drop constraint PK_test ;
Table altered
SQL> alter table test add constraint PK_test primary key (name,id) ;
Table altered
SQL> select table_name, column_name,position
2 from user_cons_columns where constraint_name = 'PK_TEST' ;
TABLE_NAME COLUMN_NAME POSITION
------------------------------ ------------------------------ ----------
TEST ID 2
TEST NAME 1
SQL> drop table test purge ;
Table dropped
也就是说,position就是指primary key (name,id) 中,name和id的位置,name是第一个,position就是1,id是第二个,position就是2。如果是primary key (id,name),那id对应的position就是1,name对应的position就是2。
注意,部分约束的position为空,例如not null,check。