外键对删除的约束

外键对删除的约束

本文介绍了外键对删除的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我创建了两个表,并将它们分别命名为table1和table2.表1的主键称为表2的外键.

所以现在我需要从表2中删除一些数据,但是我不能.
有人可以帮助我使用级联"命令执行此操作吗?

Hey All,

I have created two tables and named them as table1 and table2. Primary key of table1 refer as foreign key of table2.

So now i need to delete some data from table two and i can''t.
Can somebody help me to use "On cascade" command to do this

Create table Catalogue6(
Cat_Id int primary key,
Name Varchar(100),
Category Varchar(100)
)


Create Table Item6(
Item_no int primary key,
Cat_id int,
Serial_no int,
Capacity Varchar(45),
Descriptio Varchar(150)
Foreign key (Cat_id) References Catalogue6
)

推荐答案

Strider87写道:
Strider87 wrote:

所以现在我需要从表二中删除一些数据

So now i need to delete some data from table two



这是一个外键约束",不允许您直接从table2中删除数据.

说明:您已在Table1中使用了Table2的某些键作为外键.现在,只要该引用存在于表1中,您将无法删除表2中的该键行.由于外键关系而受到限制.

解决方案:
假设您要从Table2中删除具有主键121的行.这也是Table1中的外键.
步骤1:从表1中删除具有外键引用121
的行步骤2:从Table2中删除ID为121的行.



It''s a ''Foreign Key Constraint'' that ain''t allowing you to directly delete the data from table2.

Explaination: You have used some Key of Table2 in your Table1 as foreign Key. Now as long as this reference is present in Table1, you won''t be able to delete that key row in Table2. It''s constrained because of Foreign key relation.

Resolution:
Lets say you want to delete a row from Table2 that has the Primary Key 121. This is also the foreign key in Table1.
Step1: Delete the row(s) from Table1 that has foreign key reference of 121
Step2: Delete the row from Table2 that has ID 121.


这篇关于外键对删除的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:55