主頁 > 知識庫 > Oracle判斷表、列、主鍵是否存在的方法

Oracle判斷表、列、主鍵是否存在的方法

熱門標(biāo)簽:百度競價排名 Linux服務(wù)器 服務(wù)外包 網(wǎng)站排名優(yōu)化 呼叫中心市場需求 AI電銷 地方門戶網(wǎng)站 鐵路電話系統(tǒng)

在編寫程序時,數(shù)據(jù)庫結(jié)構(gòu)會經(jīng)常變化,所以經(jīng)常需要編寫一些數(shù)據(jù)庫腳本,編寫完成后需發(fā)往現(xiàn)場執(zhí)行,如果已經(jīng)存在或者重復(fù)執(zhí)行,有些腳本會報錯,所以需要判斷其是否存在,現(xiàn)在我就把經(jīng)常用到的一些判斷方法和大家分享下:

一。判斷Oracle表是否存在的方法

declare tableExistedCount number;  --聲明變量存儲要查詢的表是否存在
begin
   select count(1) into tableExistedCount from user_tables t where t.table_name = upper('Test'); --從系統(tǒng)表中查詢當(dāng)表是否存在
   if tableExistedCount = 0 then --如果不存在,使用快速執(zhí)行語句創(chuàng)建新表
     execute immediate
     'create table Test --創(chuàng)建測試表
     (ID number not null,Name = varchar2(20) not null)';
   end if;
end;

二。判斷Oracle表中的列是否存在的方法

declare columnExistedCount number;  --聲明變量存儲要查詢的表中的列是否存在
begin 
    --從系統(tǒng)表中查詢表中的列是否存在
    select count(1) into columnExistedCount from user_tab_columns t where t.table_name = upper('Test') and t.column_name = upper('Age');   
    --如果不存在,使用快速執(zhí)行語句添加Age列
    if columnExistedCount = 0 then 
      execute immediate
      'alter table Test add age number not null';
    end if;
end;

DECLARE
num NUMBER;
BEGIN
SELECT COUNT(1)
INTO num
from cols
where table_name = upper('tableName')
and column_name = upper('columnName');
IF num > 0 THEN
execute immediate 'alter table tableName drop column columnName';
END IF;
END;

三。判斷Oracle表是否存在主鍵的方法

declare primaryKeyExistedCount number;  --聲明變量存儲要查詢的表中的列是否存在
begin 
    --從系統(tǒng)表中查詢表是否存在主鍵(因一個表只可能有一個主鍵,所以只需判斷約束類型即可)
    select count(1) into primaryKeyExistedCount from user_constraints t where t.table_name = upper('Test') and t.constraint_type = 'P';   
    --如果不存在,使用快速執(zhí)行語句添加主鍵約束
    if primaryKeyExistedCount = 0 then 
    execute immediate
    'alter table Test add constraint PK_Test_ID primary key(id)';
    end if;
end;

四。判斷Oracle表是否存在外鍵的方法

declare foreignKeyExistedCount number;  --聲明變量存儲要查詢的表中的列是否存在
begin 
    --從系統(tǒng)表中查詢表是否存在主鍵(因一個表只可能有一個主鍵,所以只需判斷約束類型即可)
    select count(1) into foreignKeyExistedCount from user_constraints t where t.table_name = upper('Test') and t.constraint_type = 'R' and t.constraint_name = '外鍵約束名稱';   
    --如果不存在,使用快速執(zhí)行語句添加主鍵約束
    if foreignKeyExistedCount = 0 then 
      execute immediate
      'alter table Test add constraint 外鍵約束名稱 foreign key references 外鍵引用表(列)';
    end if;
end;

總結(jié)

以上所述是小編給大家介紹的Oracle判斷表、列、主鍵是否存在的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

您可能感興趣的文章:
  • Sql Server中判斷表、列不存在則創(chuàng)建的方法
  • Mysql判斷表字段或索引是否存在
  • sql server判斷數(shù)據(jù)庫、表、列、視圖是否存在
  • 用SQL語句查找Access中某表是否存在的小技巧
  • MySql獲取某個字段存在于哪個表的sql語句
  • sqlserver 各種判斷是否存在(表名、函數(shù)、存儲過程等)
  • sql 判斷數(shù)據(jù)庫,表,存儲過程等是否存在的代碼
  • oracle用什么SQL語句判斷表存不存在
  • SQL判斷字段列是否存在的方法

標(biāo)簽:銅川 衡水 蘭州 湖南 崇左 湘潭 仙桃 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Oracle判斷表、列、主鍵是否存在的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266