Monday, March 19, 2012

how to set up a 'cascading delte' in mssql

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
Jesper F (askfortheemail@.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, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
|||> 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

No comments:

Post a Comment