codecamp

Oracle 怎么删除外键

在Oracle中,如何去删除自己创建的外键呢?本教程就为大家介绍Oracle中删除外键的方法。

在Oracle中,我们可以使用ALTER TABLE语句来对外键进行删除。

语法

alter table  "表名" drop constraint "外键名"

alter table  "表名" drop primary key

示例

我们先使用下方的代码创建一个外键:

CREATE TABLE supplier
(
    supplier_id numeric(10) not null,
    supplier_name varchar2(50) not null,
    contact_name varchar2(50),
    CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);

CREATE TABLE products
( 
    product_id numeric(10) not null,
    supplier_id numeric(10) not null,
    CONSTRAINT fk_supplier
    FOREIGN KEY (supplier_id)
    REFERENCES supplier(supplier_id)
);

通过以上代码,我们在 supplier 表上创建了一个名为 supplier_pk 的主键。然后再在 products 表上创建了一个名为 fk_supplier 的外键。

如果我们想要删除这个新创建的外键,我们可以执行以下的代码:

ALTER TABLE products 
DROP CONSTRAINT fk_supplier

这样,我们就成功的删除了这个新建的fk_supplier外键。


Oracle 级联删除外键
Oracle 怎么禁用外键
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }