What is the use of CASCADE?

  • I have two tables.

    Table1:

    EngineerId,

    EngineerName,

    DeptId,

    ProjectId

    Table 2:

    EngineerId

    PFNO

    ContactNo

    MailId

    Suppose if this is the case, If I remove the EngineerId column in Table1, that delete should applicable to the Table2 also.

    Please give a code for this with atleast minimum explanation? I am using CASCADE this is the first time.

  • ALTER TABLE [dbo].[table1] WITH CHECK ADD FOREIGN KEY([fieldid])

    REFERENCES [dbo].[table2] ([fieldi_id])

    ON DELETE CASCADE

  • Can you please explain this code little bit?

  • chandrasekaran.ganapathy (4/7/2010)


    Can you please explain this code little bit?

    Links:

    Cascading Referential Integrity Constraints

    FOREIGN KEY Constraints

    Post back if you have any questions.

  • Say Eg :

    Table1

    FieldId FieldName

    1 Value1

    2 Value2

    3 Value3

    Table2

    TbkPk FieldId FieldName2

    1 1 Value12

    2 1 Value23

    3 2 Value34

    If Table2.FieldId is foregin keyof table1.FieldId and if we have casecade delete between these two field, when we delete the FieldId '1' from table1 will automatically delete FieldId '1' in Table2 (ie 2 record with fieldid=2)

  • rvasanth (4/7/2010)


    If Table2.FieldId is foregin keyof table1.FieldId and if we have casecade delete between these two field, when we delete the FieldId '1' from table1 will automatically delete FieldId '1' in Table2 (ie 2 record with fieldid=2)

    It can be set up to work that way, yes.

    Why don't you try an experiment?

  • It means that table2 has multiple instance of fieldid (say eg. fieldid =2). If you delete filedid (fieldid 2)then all the fieldid 2 will be deleted in table 2 right?

  • exactly

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply