Cascade delete using Foreign Keys

Cascade delete using Foreign Keys

Post by Somnath Uki » Sun, 05 Apr 1998 04:00:00



Hi,
I have two tables for example:
1. Customer
2. Sales
Sales table has a foreign key that references customer. Now I ud like to
delete all sales for the customer whenever a customer is deleted.
Cascade delete is not yet supported by SQL Server 6.5. So I wrote triggers
in Customer to do the job. But I find that the before the delete trigger on
customer gets executed the referential intigrity rules (foreign key) gets
applied and the delete fails giving a RI error that foreign key values
exist on Sales.
Well.... what to do? Can anybody help me?
Now, I thought of something... how about deleting the referential
intigrities. If  I delete the foreign key relationships then I have to
rewrite the triggers to do all the validations that are required for
checking the foreign key values.
Thanks in advance,
Somnath
--
Quint Software Solutions
EC-5, Sector 1, Salt Lake,
Calcutta, INDIA. 700 064
WWW: http://members.tripod.com/~quintsoftwares


Ring: 91-33-3343538, 91-33-3581574
Fax: 91-33-3581574
* IBM Solution Developer
* IBM Partner in Development
* Microsoft Value Added Provider

 
 
 

Cascade delete using Foreign Keys

Post by Mikael Hakma » Sun, 05 Apr 1998 04:00:00


Your referencial integrity constrain says that every sales must point to a
vaild customer. So you can not remove a customer who has "outstanding"
sales. This is as it should be. Removing referencial integrity would work
but it will open a possibility for other applications to insert rows into
sales that do not point to a valid customer (during the period of time when
referencial integrity is off). Another solution is to write an sp, say,

specified customer and then remove the customer itself. This sp could then
be used by all applications that need to remove a customer. This has the
addtitional benefit that you do not have to give "delete" access to
customer and sales tables to your users. Execute on REMOVE_CUSTOMER is
enough.

On a philosophical plane this leads to another and broader question. What
should we use triggers for? Should we use them in order to give the
database means to defend itself or should we use them in order to do the
job that applications should do? I vote for the first one.

/Mikael



> Hi,
> I have two tables for example:
> 1. Customer
> 2. Sales
> Sales table has a foreign key that references customer. Now I ud like to
> delete all sales for the customer whenever a customer is deleted.
> Cascade delete is not yet supported by SQL Server 6.5. So I wrote
triggers
> in Customer to do the job. But I find that the before the delete trigger
on
> customer gets executed the referential intigrity rules (foreign key) gets
> applied and the delete fails giving a RI error that foreign key values
> exist on Sales.
> Well.... what to do? Can anybody help me?
> Now, I thought of something... how about deleting the referential
> intigrities. If  I delete the foreign key relationships then I have to
> rewrite the triggers to do all the validations that are required for
> checking the foreign key values.
> Thanks in advance,
> Somnath
> --
> Quint Software Solutions
> EC-5, Sector 1, Salt Lake,
> Calcutta, INDIA. 700 064
> WWW: http://members.tripod.com/~quintsoftwares


> Ring: 91-33-3343538, 91-33-3581574
> Fax: 91-33-3581574
> * IBM Solution Developer
> * IBM Partner in Development
> * Microsoft Value Added Provider


 
 
 

Cascade delete using Foreign Keys

Post by Samuel Won » Tue, 07 Apr 1998 04:00:00


That leads to Cascading Delete & Update........
It is not the Application problem..... It is part of the Schema Design.
Oracle has this features for quite some time......... When MS is going to put
this important features in ??????? SQL 8.0 or 9.0.....

Samuel Wong
MCSE


> Your referencial integrity constrain says that every sales must point to a
> vaild customer. So you can not remove a customer who has "outstanding"
> sales. This is as it should be. Removing referencial integrity would work
> but it will open a possibility for other applications to insert rows into
> sales that do not point to a valid customer (during the period of time when
> referencial integrity is off). Another solution is to write an sp, say,

> specified customer and then remove the customer itself. This sp could then
> be used by all applications that need to remove a customer. This has the
> addtitional benefit that you do not have to give "delete" access to
> customer and sales tables to your users. Execute on REMOVE_CUSTOMER is
> enough.

> On a philosophical plane this leads to another and broader question. What
> should we use triggers for? Should we use them in order to give the
> database means to defend itself or should we use them in order to do the
> job that applications should do? I vote for the first one.

> /Mikael



> > Hi,
> > I have two tables for example:
> > 1. Customer
> > 2. Sales
> > Sales table has a foreign key that references customer. Now I ud like to
> > delete all sales for the customer whenever a customer is deleted.
> > Cascade delete is not yet supported by SQL Server 6.5. So I wrote
> triggers
> > in Customer to do the job. But I find that the before the delete trigger
> on
> > customer gets executed the referential intigrity rules (foreign key) gets
> > applied and the delete fails giving a RI error that foreign key values
> > exist on Sales.
> > Well.... what to do? Can anybody help me?
> > Now, I thought of something... how about deleting the referential
> > intigrities. If  I delete the foreign key relationships then I have to
> > rewrite the triggers to do all the validations that are required for
> > checking the foreign key values.
> > Thanks in advance,
> > Somnath
> > --
> > Quint Software Solutions
> > EC-5, Sector 1, Salt Lake,
> > Calcutta, INDIA. 700 064
> > WWW: http://members.tripod.com/~quintsoftwares


> > Ring: 91-33-3343538, 91-33-3581574
> > Fax: 91-33-3581574
> > * IBM Solution Developer
> > * IBM Partner in Development
> > * Microsoft Value Added Provider

 
 
 

Cascade delete using Foreign Keys

Post by David Mundfro » Thu, 09 Apr 1998 04:00:00



> That leads to Cascading Delete & Update........
> It is not the Application problem..... It is part of the Schema Design.
> Oracle has this features for quite some time......... When MS is going to put
> this important features in ??????? SQL 8.0 or 9.0.....

> Samuel Wong
> MCSE


> > Your referencial integrity constrain says that every sales must point to a
> > vaild customer. So you can not remove a customer who has "outstanding"
> > sales. This is as it should be. Removing referencial integrity would work
> > but it will open a possibility for other applications to insert rows into
> > sales that do not point to a valid customer (during the period of time when
> > referencial integrity is off). Another solution is to write an sp, say,

> > specified customer and then remove the customer itself. This sp could then
> > be used by all applications that need to remove a customer. This has the
> > addtitional benefit that you do not have to give "delete" access to
> > customer and sales tables to your users. Execute on REMOVE_CUSTOMER is
> > enough.

> > On a philosophical plane this leads to another and broader question. What
> > should we use triggers for? Should we use them in order to give the
> > database means to defend itself or should we use them in order to do the
> > job that applications should do? I vote for the first one.

> > /Mikael



> > > Hi,
> > > I have two tables for example:
> > > 1. Customer
> > > 2. Sales
> > > Sales table has a foreign key that references customer. Now I ud like to
> > > delete all sales for the customer whenever a customer is deleted.
> > > Cascade delete is not yet supported by SQL Server 6.5. So I wrote
> > triggers
> > > in Customer to do the job. But I find that the before the delete trigger
> > on
> > > customer gets executed the referential intigrity rules (foreign key) gets
> > > applied and the delete fails giving a RI error that foreign key values
> > > exist on Sales.
> > > Well.... what to do? Can anybody help me?
> > > Now, I thought of something... how about deleting the referential
> > > intigrities. If  I delete the foreign key relationships then I have to
> > > rewrite the triggers to do all the validations that are required for
> > > checking the foreign key values.
> > > Thanks in advance,
> > > Somnath
> > > --
> > > Quint Software Solutions
> > > EC-5, Sector 1, Salt Lake,
> > > Calcutta, INDIA. 700 064
> > > WWW: http://members.tripod.com/~quintsoftwares


> > > Ring: 91-33-3343538, 91-33-3581574
> > > Fax: 91-33-3581574
> > > * IBM Solution Developer
> > > * IBM Partner in Development
> > > * Microsoft Value Added Provider

SQL Anywhere (Sybase's low end product) has had it for several releases
also.  I've had equal amounts of experience with SQL Server and SQL
Anywhere and find the MS product strong on GUI interfaces and tools for
people who don't know much about RDBMS's, but if you want to get serious
work done use the Sybase product.
 
 
 

Cascade delete using Foreign Keys

Post by Perchero » Fri, 01 May 1998 04:00:00




>That leads to Cascading Delete & Update........
>It is not the Application problem..... It is part of the Schema Design.
>Oracle has this features for quite some time......... When MS is going to put
>this important features in ??????? SQL 8.0 or 9.0.....

Not quite correct...
Cascade Delete...Yes since 7.0, not really that long ago...There were bugs that
did not get solved until 7.0.15, for most sites that meant 7.1
Cascade Update...Not in any of the 7.x releases...Only if you were skillful in
your PL/SQL coding techniques

Paul in VT
with IDX Systems
Paul Christenson with IDX Systems

 
 
 

1. FOREIGN KEY ... ON DELETE CASCADE - syntax error

I am using Microsoft Access 2000 with
"Microsoft Access Driver (*.mdb)" version 4.00.5303.01

I executed following SQL statement:

CREATE TABLE Orders (OrderId INTEGER PRIMARY KEY, CustId INTEGER,
CONSTRAINT FKOrdersCustId FOREIGN KEY (CustId) REFERENCES Customers)

Everything was ok, table was created than I executed the same statement
with "ON DELETE CASCADE" at the end:

CREATE TABLE Orders (OrderId INTEGER PRIMARY KEY, CustId INTEGER,
CONSTRAINT FKOrdersCustId FOREIGN KEY (CustId) REFERENCES Customers ON DELETE
CASCADE)

I got following error:
[Microsoft][ODBC Microsoft Access Driver] Syntax error in CONSTRAINT clause.

Can somebody help me?
Is there any tool which says me where exactly the the syntax error is?

Thank you
Jiri Trunkat

2. convert SQL 7 query to work with JET

3. newbie: foreign key "on delete cascade"?

4. Return first row of duplicates

5. Cascade deletes with foreign key constraints.

6. Invalid call Statement method

7. Urgent: Cascade deleting with the presence of a foreign key

8. NEWBIE problem with indexing a recordset

9. Enforce Cascade Update/Delete on Foreign Keys

10. Foreign Keys and Cascading Deletes

11. Foreign Key and Cascading delete dilema

12. Foreign Keys and Cascading Deletes

13. Using SQL-DMO to create cascading foreign keys