Home All Groups Group Topic Archive Search About

how to set up a 'cascading delte' in mssql

Author
2 Feb 2006 8:26 PM
Jesper F
I'm used to working with Access but have moved to MSSQL 2005.
I have these tables:

tblCustomer: customerID (auto), name (nvarchar,50)
tblOrders: orderID (auto), customerID (int)

I use SSMSE. How can I set it up so that the related records in tblOrders
are deleted if I delete a customer.
Thanks.


Jesper

Author
2 Feb 2006 11:03 PM
Erland Sommarskog
Jesper F (askfortheem***@ask.com) writes:
> I'm used to working with Access but have moved to MSSQL 2005.
> I have these tables:
>
> tblCustomer: customerID (auto), name (nvarchar,50)
> tblOrders: orderID (auto), customerID (int)
>
> I use SSMSE. How can I set it up so that the related records in tblOrders
> are deleted if I delete a customer.

ALTER TABLE tblOrders ADD
    CONSTRAINT fk_order_customers FOREIGN KEY (customerID)
        REFERENCES tblCustomers (customerID) ON CASCADE DELETE

Although, I must say that from my corners, this looks like a funny
business rule...


--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Author
3 Feb 2006 11:40 AM
Jesper F
> ALTER TABLE tblOrders ADD
>    CONSTRAINT fk_order_customers FOREIGN KEY (customerID)
>        REFERENCES tblCustomers (customerID) ON CASCADE DELETE
>
> Although, I must say that from my corners, this looks like a funny
> business rule...

Those were imaginary tables, but thanks for pointing me in the right
direction.


Jesper

AddThis Social Bookmark Button