C# .NET - deleting foreign key

Asked By saran vasan
10-Feb-12 04:05 AM
how to delete a primary key column in one table that has been used in another table as a foreign key.
  Sandeep Mittal replied to saran vasan
10-Feb-12 04:10 AM
1. Use ON DELETE CASCADE option. this will remove all referencing records from all referencing table based on value deleted from referenced column (primary key) of referenced table

2. First delete the records from the referencing columns, then delete from the referenced table

  kalpana aparnathi replied to saran vasan
10-Feb-12 04:14 AM
hi,

ON DELETE with  NO ACTION

Specifies that if an attempt is made to delete a row with a key referenced by foreign keys in existing rows in other tables, an error is raised and the DELETE is rolled back.


ON DELETE CASCADE

Specifies that if an attempt is made to delete a row with a key referenced by foreign keys in existing rows in other tables, all rows containing those foreign keys are also deleted. If cascading referential actions have also been defined on the target tables, the specified cascading actions are also taken for the rows deleted from those tables.

  saran vasan replied to Sandeep Mittal
10-Feb-12 04:25 AM
how to use casecase option
  Somesh Yadav replied to saran vasan
10-Feb-12 04:38 AM
Hi for deleting a column which is referenced in both tables we can do it by the following.

CASCADE: Delete or update the row from the parent table, and automatically delete or update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported. Between two tables, do not define several ON UPDATE CASCADE clauses that act on the same column in the parent table or in the child table.

[CONSTRAINT [symbol]] FOREIGN KEY
    [index_name] (index_col_name, ...)
    REFERENCES tbl_name (index_col_name,...)
    [ON DELETE reference_option]
    [ON UPDATE reference_option]

reference_option:
    RESTRICT | CASCADE | SET NULL | NO ACTION
we can use ondelete cascade orOndelete Noaction.

see some examples:-

ALTER TABLE <TABLE identifier>
   ADD [ CONSTRAINT <CONSTRAINT identifier> ]
      FOREIGN KEY ( <COLUMN expression> {, <COLUMN expression>}... )
      REFERENCES <TABLE identifier> [ ( <COLUMN expression> {, <COLUMN expression>}... ) ]
      [ ON UPDATE <referential action> ]
      [ ON DELETE <referential action> ]

Likewise, foreign keys can be defined as part of the CREATE TABLE SQL statement.

CREATE TABLE TABLE_NAME (
   id    INTEGER  PRIMARY KEY,
   col2  CHARACTER VARYING(20),
   col3  INTEGER,
   ...
   FOREIGN KEY(col3)
      REFERENCES other_table(key_col) ON DELETE CASCADE,
   ... )
I hope it helps you.
  Reena Jain replied to saran vasan
10-Feb-12 04:56 AM
hi,

you canUse the SQL DROP FOREIGN KEY Clause
SQL DROP FOREIGN KEY Clause Syntax

ALTER TABLE <table_name>
DROP FOREIGN KEY <foreignkey_name>

The following example drops a foreign key named FK_SALES_ORDER_LINE_PRODUCT on a table named SALES_ORDER_LINE
ALTER TABLE SALES_ORDER_LINE
DROP FOREIGN KEY FK_SALES_ORDER_LINE_PRODUCT

hope this will help you
Create New Account
help
cascade delete within a table SQL Server Is there a way to setup a relationship within a The table has records that refer to other records within the same table. If I delete a record with a certain ItemID I want to cascade delete all records with ParentID with this value. (Is there something wrong with google groups search I am not getting anywhere near the number of hits I expect. When I search "cascade delete group:microsoft.*" I get 44 hits. I would expect thousands of results.) SQL Server Programming Discussions SQL Server (1) Nonclustered (1) CREATE TABLE (1) FOREIGN KEY (1) Privileges (1) Raiserror (1) Clustered (1) Describe (1) Here is
Cascades on a table Is there any way to delete an update cascade from a table without deleting the Foreign key constraint? hi, The REFERENTIAL_CONSTRAINTS information schema view returns CASCADE in the UPDATE_RULE or DELETE_RULE column when either ON UPDATE CASCADE or ON DELETE CASCADE is specified. NO ACTION is returned when either ON UPDATE NO ACTION or ON DELETE NO ACTION is specified, or if ON UPDATE or ON DELETE is not specified at all. Using an ALTER TABLE statement The syntax for creating a
Cascade delete with Linq To SQL .NET Framework Hi I have a problem getting Linq To SQL to cascade delete OnToMany relations. Lets say I have an Order with a collection of OrderLines. I never when saving the order. Can anyone tell me how to get Linq To SQL to cascade delete my OrderLines? I already tried DeleteRule = "CASCADE" on the EntitySet<OrderLine> (on the Order class), but it does not work. Here are Order { [Column(Name = "Id", IsPrimaryKey = true)] public Guid OrderId { get; set; } [Association(OtherKey = "OrderId", DeleteRule = "CASCADE")] public EntitySet<OrderLine> OrderLines { get; set; } } [Table] public class OrderLine { private EntityRef<Order> order; [Column
Cascade delete related records with Management studio 2005 SQL Server Hi, I can't find anymore the option for the relationshiph "Cascade delete related records" I had with SQL Server Entreprise Manager with SQL Server Management Studio for And if so where it is ? Thank you SQL Server Programming Discussions SQL Server (1) Cascade (1) Entreprise (1) ClickThe (1) Relationshiph (1) Maneger (1) right click in your table and click modify then right click again and click Relationships. Create a relationship and set cascade delete. click The problem is that I do not have this option with Management Studio. I
VS2008 cascade delete in sqlexpress Hi I've got 2 tables: tbl_contacts and tbl_addresses tbl_contacts has FKs to tbl_addresses with cascade delete option Whenever i delete a row in tbl_contacts, no tbl_addresses row gets cascade deleted. Whenever i delete a row in tbl_addresses a tbl_contacts row gets cascade deleted. I want to reverse this, i want the address row for tbl_contacts.addressID to