locked
SQL Server CE 3.5 Primary/Foreign Key Setup RRS feed

  • Question

  • Hi,

        I have 3 tables with the structure as below:

    ProjectTable

    ProjectID

    ...

    StudiesTable

    ProjectID

    StudyID

    ...

    StudyDetailTable

    ProjectID

    StudyID

    ...

    I wish that if a project is deleted in the 'Projects' Table then the delete will cascade down to the 'Studies' and 'StudyDetails' tables. I also wish that if a study is deleted in the 'Studies' table that the delete cascades down to the 'StudyDetails' table. What would be the best way to achieve this. Columns in any tables can be changes as necessary.

    Thanks

    Paul.


    Paul Wainwright

    Friday, May 15, 2015 10:42 AM

Answers

  • To cascade your deletes, you need to apply this on your foreign key indexes, e.g.

    ALTER TABLE dbo.T2 ADD CONSTRAINT FK_T1_T2_Cascade
      
    FOREIGN KEY (EmployeeID) REFERENCES dbo.T1(EmployeeID) ON DELETE CASCADE


    Please click "Mark As Answer" if my post helped. Tony C.

    • Marked as answer by pr_wainwright Friday, May 15, 2015 11:15 AM
    Friday, May 15, 2015 10:48 AM