- 作者: 三十而立
- 时间:2009年11月05日 5:09:04
- 请尊重原创作品。转载请保持文章完整性,并以超链接形式注明原始作者“inthirties(三十而立) ”和出处”http://blog.csdn.net/inthirties/archive/2009/11/05/4771059.aspx ”,深入讨论可以联系inthirties@gmail.com 。
上篇文章“一个外键引发的错误---ORA-2449有关drop表空间错误解决实例 ”解决一个由外键引发的问题。 这个外键就是references了,有关的数据库的数据字典表是constraints系列表(dba_constraints, user_constraints, dba_cons_columns等相关表), 不过这个dba_constraints表里并去全是references,references也是一个constraints的类型,在表里constraint_type为R的便是我们这里给你讲到的references了。
这个reference自己以前没有好好研究过,处理上个问题的时候,仅仅是用到了这几个数据字典表而已。 其实这里要知道的不仅仅是数据字典的内容,我在写上个文章的时候,把自己的故障排除过程记下来的过程中,就深思到references里的几个问题。 也被其整蛊了一番,不过在文档的帮助下,还是解决了,看来oracle的学习时时刻刻都在进行呀。
下面我们就看看这个references, 先看数据字典,主要的数据字典表是
dba_constraints这个表,
里面的主要字段
Name Type Nullable Default Comments
----------------- ------------ -------- ------- ---------------------------------------------------------------------------
OWNER VARCHAR2(30) Owner of the table
CONSTRAINT_NAME VARCHAR2(30) Name associated with constraint definition
CONSTRAINT_TYPE VARCHAR2(1) Y Type of constraint definition
TABLE_NAME VARCHAR2(30) Name associated with table with constraint definition
R_OWNER VARCHAR2(30) Y Owner of table used in referential constraint
R_CONSTRAINT_NAME VARCHAR2(30) Y Name of unique constraint definition for referenced table
LAST_CHANGE DATE Y The date when this column was last enabled or disabled
INDEX_OWNER VARCHAR2(30) Y The owner of the index used by this constraint
INDEX_NAME VARCHAR2(30) Y The index used by this constraint
TYPE为R的就是我们的References
实验为先
SQL> conn test1/test1
SQL> create table test_1_to_test1 (id int);
SQL> alter table test_1_to_test1 add constraint PK_test1_id primary key (id);
SQL>select constraint_name, constraint_type from dba_constraints a where a.constraint_name = 'PK_TEST1_ID';
CONSTRAINT_NAME CONSTRAINT_TYPE
------------------------------ ---------------
PK_TEST1_ID P
这里是P表示是Primary key
SQL> create table test_1_to_test2 (id int primary key, pid int, constraint FK_te
st_1_id foreign key (pid) references test_1_to_test1(id));
表已创建。
SQL> select constraint_name, constraint_type from dba_constraints a where a.owner = 'TEST1' and a.table_name = 'TEST_1_TO_TEST2';
CONSTRAINT_NAME CONSTRAINT_TYPE
------------------------------ ---------------
SYS_C0010957 P
FK_TEST_1_ID R
这里下面的就是我们添加的references,上面是我们oracle指定的PK的constraint name,我们在建pk的时候美誉给名字,由Oracle代劳了。
能不 能跨用户的使用references叻,这个是我再处理上面的问题里,面对的一个选择,答案是肯定的
我在上个文章里写sql后,回来仔细看了看sql发现我当时考虑掉了,跨用户的reference的关系,我们来看看如何跨用户的用reference,当然也许这样的数据库设计不合理,不过我们讨论的仅是片面的reference的知识点。
我在test_1用户里建了一个表 test1 其中 id是主键, 那么我现在在tetst1这个用户下做一个表引用到这个字符, 命令和普通的一样
SQL> alter table test_1_to_test1 add (pid int);
表已更改。
SQL> alter table test_1_to_test1 add constraint FK_test1_id2 foreign key (pid) r
eferences test_1.test_1(id);
alter table test_1_to_test1 add constraint FK_test1_id2 foreign key (pid) refere
nces test_1.test_1(id)
*
第 1 行出现错误:
ORA-01031: 权限不足
不行,但是由于时间有限,所以马上用sys登录上去
SQL> alter table test1.test_1_to_test1 add constraint FK_test1_id2 foreign key (pid) references test_1.test_1(id);
alter table test1.test_1_to_test1 add constraint FK_test1_id2 foreign key (pid) references test_1.test_1(id)
ORA-01031: 权限不足
依然权限不足
根据以往的经验,直接赋对象权限试试。
SQL> conn test_1/test_1
SQL>grant references on test_1 to test1;
然后可以了。
试试references any table提示错误选项。 看到这个只能是对象权限,没有系统权限。
为什么sys也不能操作了,以前在plsql里create table里碰到过,只能是对User有效,而不能作用在Role上的, 这里这个refer也是如此。
时间有限,所以在现场也就只是完成目标而已,没有详细的研究, 这里到此也仅仅是研究了其中grant和数据字典部分,
特别在这里给大家看到了不同schema里对象的reference的方式和需要注意的,
由于时间有限,这里仅仅提到这里,不过更多的需要大家自己去探索探索,用上篇文章提到的,如果把学习转换成探索的成分,你是不是跟喜欢上了Oracle呀。
至此, 如果朋友探讨这个过程中有遇到疑问,都可以发信给我。 愿意和大家一起分享Oracle的奥秘。
- 如果没有那句“三十而立”,三十岁的男人正可以轻轻松松
- 专业论坛 http://www.inthirties.com
- 技术博客 http://blog.csdn.net/inthirties
- 个人站点 http://blog.inthirties.com
- Oracle Mysql技术论坛| 打造实用的Oracle Mysql技术交流园地