sql 刪除帶外來鍵約束的表的語句是什麼

時間 2021-10-27 04:38:38

1樓:匿名使用者

1、建立主鍵表,test_class,並建立class_id欄位為主鍵;

create table test_class(class_id number, class_name varchar2(20));

-- create/recreate indexes

alter table test_class

add constraint p_class_id primary key (class_id)

using index

tablespace users

pctfree 10

initrans 2

maxtrans 255;

2、建立外來鍵表,test_stu,其中欄位class_id為test_class表的class_id欄位;

create table test_stu(id number, class_id number);

-- create/recreate primary, unique and foreign key constraints

alter table test_stu

add constraint f_class_id foreign key (class_id)

references test_class (class_id) on delete cascade;

3、兩張表分別插入記錄;

insert into test_class values(1001,'語文');

insert into test_class values(1002,'數學');

insert into test_class values(1003,'英語');

insert into test_stu values(1,1001);

insert into test_stu values(2,1001);

insert into test_stu values(3,1002);

insert into test_stu values(4,1003);

4、查詢test_stu表中的記錄;select t.*, rowid from test_stu;

5、刪除主表test_class中class_id=1001的記錄,會發現從表test_stu中class_id中的記錄也被刪除;

delete test_class where class_id = 1001;

commit;

select t.*, t.rowid from test_stu t

2樓:阿冬

擁有主鍵的表稱為主表,擁有外來鍵的表稱為從表,從表可以直接刪除,不受限制,但主表無法在從表還存在的情況下直接刪除。

另外,根據樓主的意思,我猜想你可能想問的是有外碼關聯的資料刪除的問題。以下簡述:

通常情況下,從表中外來鍵所在欄位的資料取值只能取主鍵中存在的值或者取空值,從表中的資料可以隨時刪除,而主表中的資料如果被從表所參照,則不能直接刪除,如果想直接刪除主表中被參照的資料,需要在建立外來鍵時指定on delete cascade或者on delete set null,即

foreign key(從表中的某個欄位) references 主表(主鍵) on delete cascade

或者foreign key(從表中的某個欄位) references 主表(主鍵) on delete set null

其中第一種方法表示刪除主表中的資料時,從表中對應資料一起被強制刪除;

第二種方法表示刪除主表中的資料時,從表中對應資料設定為null

最後要注意的一點,這兩個短語並非所有資料庫管理系統都支援,印象中db2、oracle和sql server都支援,mysql沒試過,不知道你用哪種資料庫,最好在你的系統中親自試一下。

以上僅供參考。

3樓:匿名使用者

刪除語句如下:

alter table 表名 drop constraint 外來鍵約束名

使用如下sql語句查詢出表中外來鍵約束名稱:

select name

from  sys.foreign_key_columns f join sys.objects o on f.constraint_object_id=o.object_id

where f.parent_object_id=object_id('表名')

各個資料庫有細微區別,以上提供的為sqlserver的

4樓:匿名使用者

刪除外來鍵語句:

現刪除關聯的外來鍵名,才能再刪除外來鍵欄位;

alter table 表名 drop foreign key 外來鍵關聯名;

alter table 表名 drop column 『列名』

oracle語句怎麼刪除外來鍵約束,只是刪約束,不是刪表

5樓:匿名使用者

假如有主表 test_main 與 子表 test_subsql> -- 建立外來鍵(預設選項)

sql> alter table test_sub add constraint main_id_cons foreign key (main_id) references test_main;

table altered.

刪除外來鍵約束

alter table test_sub drop constraint main_id_cons;

6樓:匿名使用者

alter table 表名 drop constraint 外來鍵約束名;

7樓:匿名使用者

alter table table_name drop constraint 外來鍵約束名稱;

sql語句的外來鍵約束是什麼?

8樓:

create table score。

sql的主鍵和外來鍵的作用:外來鍵取值規則:空值或參照的主鍵值。

(1)插入非空值時,如果主鍵表中沒有這個值,則不能插入。

(2)更新時,不能改為主鍵表中沒有的值。

(3)刪除主鍵表記錄時,你可以在建外來鍵時選定外來鍵記錄一起級聯刪除還是拒絕刪除。

(4)更新主鍵記錄時,同樣有級聯更新和拒絕執行的選擇。

簡而言之,sql的主鍵和外來鍵就是起約束作用。

alter table 外來鍵表名 add constraint 約束名稱 foreign key (外來鍵欄位) references 主鍵表名(約束列名)。

如果表a中的ids是主鍵,要約束表b中得aid列,那麼語句應該是:alter table b add constraint a_b_ids foreign key(aid) references a(ids)。

sql表關聯,SQL語句 關聯表的問題

我覺得,應該加限定語句 where 比如 4 刪除資料 delete from table name where conditions 說明 刪除符合條件的資料。說明 關於where條件後面如果包含有日期的比較,不同資料庫有 不同的表示式。具體如下 1 如果是access資料庫,則為 where m...

SQL的雙主鍵刪除語句

對於一個表來說,主鍵可能由一個欄位構成,但是也可能有多個欄位構成。後者就是複合主鍵。你的薪資發放表中主鍵是 員工號,發放日期 即唯一確定一條記錄需要這兩個欄位的聯合取值,缺一不可。故,所以可使用如下的語句 delete from 薪資表where 工號 and 發放日期 date or 工號 and...

在oracle中查詢表之間外來鍵的執行語句怎麼寫

查詢表的外來鍵 包括名稱,引用表的表名和對應的鍵名,下面是分成多步查詢 select from user constraints c where c.constraint type r and c.table name 要查詢的表 查詢外來鍵約束的列名 select from user cons c...