1.查詢所有表的外鍵的:
丘北網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
select table_name, constraint_name from user_constraints where constraint_type = 'R';
2.禁用所有外鍵約束, 使用下面的sql生成對應(yīng)sql腳本:
select 'alter table ' || table_name || ' disable constraint ' || constraint_name || ';' from user_constraints where constraint_type = 'R';
生成的sql類似下面的語句:
alter table BERTH disable constraint BERTH_FK;
alter table BOLLARD disable constraint BOLLARD_FK;
alter table YARD_UNAVAIL_REGION disable constraint YARD_UNAVAIL_REGION_FK;
3.啟用所有外鍵約束, 使用下面的sql生成對應(yīng)sql腳本:
select 'alter table ' || table_name || ' enable constraint ' || constraint_name || ';' from user_constraints where constraint_type = 'R';
生成的sql類似下面的語句:
alter table BERTH enable constraint BERTH_FK;
alter table BOLLARD enable constraint BOLLARD_FK;
alter table YARD_UNAVAIL_REGION enable constraint YARD_UNAVAIL_REGION_FK;
4.刪除所有外鍵約束, 使用下面的sql生成對應(yīng)sql腳本:
select 'alter table ' || table_name || ' drop constraint ' || constraint_name || ';' from user_constraints where constraint_type = 'R';
生成的sql類似下面的語句:
alter table BERTH drop constraint BERTH_FK;
alter table BOLLARD drop constraint BOLLARD_FK;
alter table YARD_UNAVAIL_REGION drop constraint YARD_UNAVAIL_REGION_FK;
禁用了外鍵約束,那么你的數(shù)據(jù)可能就不符合 此處外鍵約定的規(guī)則。
對你的數(shù)據(jù)的邏輯肯定有影響,對你的應(yīng)用程序可能產(chǎn)生影響。
至于Oracle系統(tǒng)本身,則沒有什么影響
特總結(jié)了Oracle和DB2數(shù)據(jù)庫下如何禁用外鍵約束的方法。
一、Oracle數(shù)據(jù)庫:
禁用約束基本語法:
alter table 數(shù)據(jù)庫表名 disable constraint 約束名
假設(shè)現(xiàn)在需要關(guān)閉pub_organ的外鍵約束:
1、 首先查詢pub_organ存在哪些外鍵約束,此時需要用到oracle的字典表user_constraints。
select * from user_constraints where table_name='PUB_ORGAN';
上圖就是查詢結(jié)果,其中各字段含義如下:
OWNER: 表的所有者
CONSTRAINT_NAME: 約束名稱
CONSTRAINT_TYPE: 約束類型(R代表外鍵,P代表主鍵,C代表check約束)
TABLE_NAME: 表名稱
SEARCH_CONDITION: check約束的具體信息
STATUS: ENABLED表示當(dāng)前約束是啟用的,DISABLED表示當(dāng)前約束未啟用。
2、 查詢出表存在哪些約束后,即可以通過alter語句啟用或禁用指定的約束了。
如禁用pub_organ表的外鍵PUBORGAN_FK1,則可以使用如下命令實(shí)現(xiàn):
alter table PUB_ORGAN disable constraint PUBORGAN_FK1;
執(zhí)行后,再次查詢字典表user_constraints,如下:
此時往數(shù)據(jù)庫表pub_organ中插入數(shù)據(jù)時就不再受外鍵約束的影響了。
啟用約束基本語法:
alter table 數(shù)據(jù)庫表名 enable constraint 約束名
如現(xiàn)在需要重新啟用pub_organ的外鍵約束,可以使用如下命令:
alter table PUB_ORGAN enable constraint PUBORGAN_FK1;
二、DB2數(shù)據(jù)庫:
禁用約束基本語法:
ALTER TABLE 表名稱 ALTER FOREIGN KEY 約束名稱 NOT ENFORCED
啟用約束基本語法:
ALTER TABLE 表名稱 ALTER FOREIGN KEY 約束名稱 ENFORCED
相關(guān)字典表:SYSIBM.SYSTABCONST
如:select * from SYSIBM.SYSTABCONST where tbname='PUB_ORGAN';
各字段含義如下:
NAME: 約束名稱
DEFINER: 定義者
CONSTRAINTTYP: 約束類型(P代表主鍵,F(xiàn)代表外鍵)
TBNAME: 表名稱
ENFORCED: 是否啟用(Y代表啟用,N代表未啟用)
三、封裝成java接口、批量執(zhí)行
在實(shí)際工作中,經(jīng)常會將若干個表,或者所有數(shù)據(jù)庫表的外鍵一起禁用,此時需要批量執(zhí)行相關(guān)命令,筆者根據(jù)工作實(shí)際,使用java封裝了相關(guān)接口,以方便使用。
對外暴露接口如下:
/*
* 啟用當(dāng)前用戶指定tableName的所有外鍵約束
* 入?yún)⑹褂每勺儏?shù)(jdk5新特性)
* 調(diào)用方式:
* 1、enableFK("pub_organ")
* 2、enableFK("pub_organ","pub_stru")
* 3、enableFK(new String[]{"pub_organ","pub_stru"})
*/
public static void enableFK(String...tableNames){
disableORenbaleFK(true,tableNames);
}
/*
* 禁用當(dāng)前用戶指定tableName的所有外鍵約束
*/
public static void disableFK(String...tableNames){
disableORenbaleFK(false,tableNames);
}
/*
* 啟用當(dāng)前用戶所有表的外鍵約束
*/
public static void enableAllFK(){
disableORenableAllConstraint(true);
}
/*
* 禁用當(dāng)前用戶所有表的外鍵約束
*/
public static void disableAllFK(){
disableORenableAllConstraint(false);
}
其中核心處理代碼如下:
if(tableNames==null||tableNames.length==0){
throw new RuntimeException("入?yún)ableNames不能為空!");
}
//查詢指定表的外鍵約束
String sql = null;
String dbType = getDBType();
if(dbType.contains("ORACLE")){
sql = "select 'alter table ' || table_name || ' disable constraint ' || constraint_name from user_constraints where constraint_type='R' and TABLE_NAME in(";
if(isEnable){
sql = sql.replace("disable", "enable");
}
}else if(dbType.contains("DB2")){
sql = "select 'ALTER TABLE ' || TBNAME || ' ALTER FOREIGN KEY ' || NAME ||' NOT ENFORCED ' FROM SYSIBM.SYSTABCONST WHERE CONSTRAINTYP='F' and TBNAME in(";
if(isEnable){
sql = sql.replace("NOT ENFORCED", "ENFORCED");
}
}else{
throw new RuntimeException("數(shù)據(jù)庫類型無效(僅支持Oracle和DB2),dbType="+dbType);
}
StringBuffer generateSQL = new StringBuffer(sql);
for(int i=0;itableNames.length;i++){
generateSQL.append(" '");
generateSQL.append(tableNames[i].toUpperCase());//注意須轉(zhuǎn)換成大寫
generateSQL.append("',");
}
generateSQL.deleteCharAt(generateSQL.length()-1);
generateSQL.append(")");
ListMapString, Object dataSet = DBTool.executeQuery(generateSQL.toString());
//啟用or停止查詢出的外鍵約束
for(int i=0;idataSet.size();i++){
MapString, Object record = dataSet.get(i);
IteratorEntryString, Object itor = record.entrySet().iterator();
while(itor.hasNext())
{
EntryString, Object e = itor.next();
DBTool.executeUpdate(e.getValue().toString(),UpdateType.ALTER);
}
}
可用sql語句刪除外鍵約束,也可以用其他工具操作(如PL/SQL)。
一、語句刪除:
alter?table?表名?drop?constraint?外鍵名;
二、工具刪除:
1、登錄PL/SQL到指定數(shù)據(jù)庫。
2、左側(cè)找到Tables選項(xiàng)。
3、找到相應(yīng)的表,如emp,然后找到Foreign keys選項(xiàng)。
4、右鍵顯示出來的外鍵,點(diǎn)擊“刪掉”即可。
網(wǎng)頁題目:oracle怎么禁用外鍵,oracle添加外鍵語句
網(wǎng)頁地址:http://www.2m8n56k.cn/article12/hcesdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、外貿(mào)網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、動態(tài)網(wǎng)站、外貿(mào)建站、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)