查询primary key和forgen key的关系表

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

说明:

SQL create user a identified by a; --创建测试用户a

SQL grant connect,resource to a; --给用户a授权

SQL conn a/a --以用户a连接

SQL create table a1(a11 number primary key);

SQL insert into a1 values(1);

SQL insert into a1 values(2);

SQL insert into a1 values(3);

SQL commit;

SQL create table b1

2 (

3 b11 char(1),

4 b12 number,

5 foreign key(b12) references a1(a11)

6 )

7 /

SQL insert into b1 values('a',4);

insert into b1 values('a',4)

*

ERROR 位于第 1 行:

ORA-02291: 违反完整约束条件 (A.SYS_C001241) - 未找到父项要害字

注:a1表中a11列上只有1、2、3这三个值,而b1表中的列b12定义时引用了a1表中的a11列,

那么,在向b1表中录入记录时,b12列只能录1、2、3,而不能输入4。

SQL create table a2(a21 number primary key);

SQL insert into a2 values(1);

SQL insert into a2 values(2);

SQL insert into a2 values(3);

SQL commit;

SQL create table b2

2 (

3 b21 char(1),

4 b22 number,

5 foreign key(b22) references a2(a21)

6 )

7 /

注:以下语句查询primary key和forgen key的关系表

SQL col 外键拥有者 format a10

SQL col 外键表 format a10

SQL col 主键拥有者 format a10

SQL col 主键表 format a10

SQL col 外键列 format a15

SQL col 主键列 format a15

select

a.owner 外键拥有者,

a.table_name 外键表,

c.column_name 外键列,

b.owner 主键拥有者,

b.table_name 主键表,

d.column_name 主键列

from

user_constraints a,

user_constraints b,

user_cons_columns c,

user_cons_columns d

where

a.r_constraint_name=b.constraint_name

and a.constraint_type='R'

and b.constraint_type='P'

and a.r_owner=b.owner

and a.constraint_name=c.constraint_name

and b.constraint_name=d.constraint_name

and a.owner=c.owner

and a.table_name=c.table_name

and b.owner=d.owner

and b.table_name=d.table_name

/

外键拥有者 外键表 外键列 主键拥有者 主键表 主键列

---------- ---------- --------------- ---------- ---------- ------

A B1 B12 A A1 A11

A B2 B22 A A2 A21

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航