Find order of columns in foreign key
I am trying to extract some meta data from Oracle about foreign keys. I am using the “all_constraints” to find all referential constraints information and “all_cons_columns” to find the actual columns. However, I can’t get the order of my columns in the foreign key.
Below are 4 example tables all with 3 columns. 3 of the tables reference the table “tab_d”, but the order of the columns in the foreign key differs. This order is not reflected in the “all_cons_columns” view, so is there another place where this information is available?
create table tab_d (
col_a int,
col_b int,
col_c int,
constraint tab_d_pk primary key (col_a, col_b, col_c)
);
create table tab_e (
ref_col_a int,
ref_col_b int,
ref_col_c int,
constraint tab_e_fk foreign key (ref_col_b, ref_col_c, ref_col_a)
references tab_d(col_b, col_c, col_a)
);
create table tab_f (
ref_col_a int,
ref_col_b int,
ref_col_c int,
constraint tab_f_fk foreign key (ref_col_b, ref_col_c, ref_col_a)
references tab_d(col_c, col_a, col_b)
);
create table tab_g (
ref_col_a int,
ref_col_b int,
ref_col_c int,
constraint tab_g_fk foreign key (ref_col_c, ref_col_b, ref_col_a)
references tab_d(col_c, col_b, col_a)
);
The information i get from "all_cons_columns" is shown below and e.g. 开发者_JAVA技巧I my table “tab_g” where I thought position show the column order ref_col_c = 1, ref_col_b = 2 and ref_col_a = 3 it shows what order the column is in the primary key.
CONSTRAINT_NAME TABLE_NAME COLUMN_NAME POSITION
TAB_F_FK TAB_F REF_COL_A 2
TAB_F_FK TAB_F REF_COL_B 3
TAB_F_FK TAB_F REF_COL_C 1
TAB_E_FK TAB_E REF_COL_A 1
TAB_E_FK TAB_E REF_COL_B 2
TAB_E_FK TAB_E REF_COL_C 3
TAB_G_FK TAB_G REF_COL_A 1
TAB_G_FK TAB_G REF_COL_B 2
TAB_G_FK TAB_G REF_COL_C 3
The order of the columns is reflected in the ALL_CONS_COLUMNS view - by the column called POSITION.
精彩评论