简体   繁体   English

从表中删除SQL中的行

[英]Delete row in sql from table

Sorry for the title but I didn't know how to name it.. 对不起,标题,但我不知道该如何命名。

I'm using C# and have a WinForms application I have 2 tables, each of them have a primary key, those 2 tables are strangers. 我正在使用C#,并且有一个WinForms应用程序,我有2个表,每个表都有一个主键,那2个表是陌生人。 It means that I have a third table that connects between them. 这意味着我有连接它们之间的第三个表。 the third table have as columns : table's A primary key and table's B primary key. 第三张表的列为:表的A主键和表的B主键。

I just want to know that if I'm deleting one row from the third table, is the related data from table A and B will be also deleted? 我只想知道,如果我要从第三张表中删除一行,那么表A和B中的相关数据也会被删除吗?

If you created a foreign key constraint with the ON DELETE CASCADE option, then yes, it will delete the related rows in other tables. 如果使用ON DELETE CASCADE选项创建了外键约束,则可以,它将删除其他表中的相关行。

If you created a foreign key constraint WITHOUT the ON DELETE CASCADE option, then the DBMS will prevent you from deleting the original row at all. 如果在没有ON DELETE CASCADE选项的ON DELETE CASCADE创建了外键约束,则DBMS将完全阻止您删除原始行。

If you did not create a foreign key constraint then only the original row will be deleted. 如果未创建外键约束,则仅原始行将被删除。

See this SO answer for example usage of the cascading delete option. 有关级联删除选项的用法示例,请参见此SO答案

No, it doesn't your third table is just associate table which stores references of table A and B. 不,不是您的第三个表仅仅是存储表A和B的引用的关联表。

But it will have different functionality other way, if a record is deleted in table A and it has any references in associate table C. Depending upon your cascade options, record in table C will also be deleted. 但是,如果在表A中删除一条记录,并且在关联表C中具有任何引用,它将具有其他功能。根据您的级联选项,表C中的记录也将被删除。 If there are no cascade options mentioned, it gives exception. 如果没有提到级联选项,它将给出异常。

Sql Server wouldn't let you delete the row from 3rd table because of a Foreign Key constraint. 由于外键约束,SQL Server不允许您从第3个表中删除行。 You'd have to delete the values referencing the 3rd table from table A and B first and then delete from your 3rd table. 您必须先从表A和B中删除引用第三表的值,然后再从第三表中删除。

否,因为第三个表是表a和表b的关联表,如果删除在第三个表上,则不会删除表a和表b中的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM