自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Oracle中用非默認(rèn)方式創(chuàng)建外鍵

數(shù)據(jù)庫 Oracle
oracle數(shù)據(jù)庫是一種大型數(shù)據(jù)庫系統(tǒng),一般應(yīng)用于商業(yè),政府部門,它的功能很強(qiáng)大,能夠處理大批量的數(shù)據(jù),在網(wǎng)絡(luò)方面也用的非常多。下文中將為大家?guī)砣绾卧贠racle中用非默認(rèn)方式創(chuàng)建外鍵的方法。

導(dǎo)讀:oracle數(shù)據(jù)庫是一種功能性很好的數(shù)據(jù)庫系統(tǒng),在數(shù)據(jù)庫=業(yè)界也是占據(jù)著重要地位的,相信很多人對oracle數(shù)據(jù)庫都是比較熟悉的,可是對與在oracle數(shù)據(jù)庫中用非默認(rèn)方式創(chuàng)建外鍵大家未必都會,下文就教給大家如何在oracle數(shù)據(jù)庫中用非默認(rèn)方式創(chuàng)建外鍵。

  創(chuàng)建外鍵約束時(shí)如果使用Oracle默認(rèn)的創(chuàng)建方式,在刪除被參照的數(shù)據(jù)時(shí),將無法被刪除,這一點(diǎn)在Oracle9i中給了我們更多靈活的選擇,我們可是使用on delete cascade和 on delete set null關(guān)鍵字來決定刪除被參照數(shù)據(jù)時(shí)是否要將參照這個(gè)數(shù)據(jù)的那些數(shù)據(jù)一并刪除,還是將那些參照這條數(shù)據(jù)的數(shù)據(jù)的對應(yīng)值賦空。

例如下面這兩個(gè)表中分別存的時(shí)員工的基本信息和公司的部門信息。我們?yōu)?/p>

  create table dept
  (deptno number(10) not null,
  deptname varchar2(30) not null,
  constraint pk_dept primary key(deptno));

  和

  create table emp
  ( empno number(10) not null,
  fname varchar2(20) ,
  lname varchar2(20) ,
  dept number(10) ,
  constraint pk_emp primary key(empno));

  然后我們現(xiàn)在分別使用這兩個(gè)關(guān)鍵字來增加外鍵試一下,首先我們來試一下on delete cascade

  alter table emp
  add constraint fk_emp_dept foreign key(dept) references dept(deptno) on delete cascade;

  先增加外鍵。然后插入數(shù)據(jù)。

  insert into dept values(1,’銷售部’);
  insert into dept values(2,’財(cái)務(wù)部’);
  insert into emp values (2,’Mary’,'Song’,1);
  insert into emp values (3,’Linda’,'Liu’,2);
  insert into emp values (4,’Linlin’,'Zhang’,1);

  然后現(xiàn)在我要?jiǎng)h除銷售部,會有什么后果呢?

  delete from dept where deptno = 1;

  我們發(fā)現(xiàn)除了dept中的一條數(shù)據(jù)被刪除了,emp中兩條數(shù)據(jù)也被刪除了,其中emp中的兩條數(shù)據(jù)是參照了銷售部的這條數(shù)據(jù)的,這就很容易理解on delete cascade了。

 

  創(chuàng)建外鍵約束時(shí)如果使用Oracle默認(rèn)的創(chuàng)建方式,在刪除被參照的數(shù)據(jù)時(shí),將無法被刪除,這一點(diǎn)在Oracle9i中給了我們更多靈活的選擇,我們可是使用on delete cascade和 on delete set null關(guān)鍵字來決定刪除被參照數(shù)據(jù)時(shí)是否要將參照這個(gè)數(shù)據(jù)的那些數(shù)據(jù)一并刪除,還是將那些參照這條數(shù)據(jù)的數(shù)據(jù)的對應(yīng)值賦空。

按照上文中介紹的就能夠?qū)崿F(xiàn)在oracle數(shù)據(jù)庫中用非默認(rèn)方式創(chuàng)建外鍵的目的,希望對大家能夠有所幫助。

【編輯推薦】

  1. Oracle一則詭異的程序
  2. Oracle學(xué)習(xí)入門心得
  3. 教你怎樣在Oracle數(shù)據(jù)庫中高速導(dǎo)出/導(dǎo)入
  4. Oracle數(shù)據(jù)庫開發(fā)技術(shù)經(jīng)驗(yàn)淺談
責(zé)任編輯:迎迎 來源: IT專家網(wǎng)論壇
相關(guān)推薦

2009-03-26 09:24:36

Oracle外鍵數(shù)據(jù)庫

2011-04-14 13:10:23

Oracle

2010-08-19 09:44:04

DB2創(chuàng)建外鍵

2011-05-12 14:33:37

MySQL外鍵

2010-04-26 16:39:35

Oracle外鍵

2010-08-09 10:08:15

DB2 創(chuàng)建外鍵

2017-01-05 15:15:59

UFWUbuntu配置防火墻

2018-09-14 08:00:00

LinuxWondershape網(wǎng)絡(luò)帶寬

2019-12-13 09:45:38

Windows 10PowerToysWindows

2010-10-19 17:28:08

SQL SERVER外

2010-04-23 15:44:29

Oracle 外鍵

2011-03-22 12:59:18

SQLOracle外鍵約束

2021-01-28 05:13:17

Oracle索引外鍵

2010-06-17 17:50:31

SQL Server外

2020-08-12 07:00:00

存儲數(shù)據(jù)工具

2016-10-21 10:51:53

Windows快捷程序

2010-04-20 15:47:25

Oracle實(shí)例

2016-07-29 11:21:16

Ubuntulinux程序

2020-02-18 19:02:03

UbuntuLinux終端

2010-11-22 10:08:03

Mysql外鍵用法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號