注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

伯纳乌の夢

可以挽回么?我们按“ Ctrl+Z”撤销掉吧。对不起啦~~

 
 
 

日志

 
 
 
 

Oracle Foreign key  

2009-08-19 21:56:40|  分类: Oracle/SQLServer |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
A foreign key is a column in a table that does not uniquely identify rows in that table, but is used as a link to matching columns in other tables to indicate a relationship.

For example, the Hr_Emp.deptNo column is a foreign key pointing the the Hr_Dept table's primary key - Hr_Dept.deptNo.

Examples

Inline and out of line definitions

1. Define a table with primary key to reference:

CREATE TABLE t1 (c1 NUMBER PRIMARY KEY);

2. Inline foreign key (part of column definition):

CREATE TABLE t2 (
        c1 NUMBER PRIMARY KEY,
        c2 NUMBER REFERENCES t1(c1) );

3. Out-of-line foreign key (after column definitions):

CREATE TABLE t3 (
        c1 NUMBER,
        c2 NUMBER,
        CONSTRAINT t1_fk FOREIGN KEY (c1) REFERENCES t1);

ON DELETE CASCADE

Foreign key with ON DELETE CASCADE:

CREATE TABLE parent (id NUMBER PRIMARY KEY);
CREATE TABLE child  (id NUMBER PRIMARY KEY,
                    pid REFERENCES parent(id) ON DELETE CASCADE);

If rows are deleted from the parent table, referenced rows will automatically be removed from the child table.

ON DELETE SET NULL

Foreign key with ON DELETE SET NULL:

CREATE TABLE parent (id NUMBER PRIMARY KEY);
CREATE TABLE child  (id NUMBER PRIMARY KEY,
                    pid REFERENCES parent(id) ON DELETE SET NULL);

If rows are deleted from the parent table, referenced rows field will automatically be set to null in the child table.
  评论这张
 
阅读(1616)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017