Re: BIG BUG in Deleting detali records



Cavalera Guido wrote:
... but there is a workaround ? to use delete details on batchOptimistic ?

You must change the declaration of the client table
in the database in order to enforce referential integrity.

Referential integrity is a "must have" in a database and
should never be left to the application IMHO.


It is easy to implement. Just add a "constraint" to the client
table so that the ID that references the master table becomes
a foreign key.

Specifying "on delete cascade" for that foreign key will cause
all detail records of a customer to be deleted automatically
when the customer himself is deleted from the master table.


E X A M P L E
-------------------

Here is a little example. I haven't double-checked the syntax
but it should give you a pretty good idea what's it about.
In "real life" one would of course also have to add some indexes
for speed and select appropriate data types for the ID's (integers
have a way of wrapping around after a few billion entries).

In this example, the "constraints" enforce that whenever you
delete an order, the items belonging to that order are deleted
also, in a master-detail way. But you can still delete individual items
belonging to an order.

Also, when you delete a customer, all orders for that customer
are deleted from the database (which, in turn, triggers the
deletion of the appropriate items). This snowball effect is called
"cascading".

Deleting a "product" is made impossible if that product has
ever been ordered by anyone. This way the tables are
always in a valid state - guaranteed.





create table [dbo].customers(
customerID int identity (1,1) not null primary key,
customername varchar(50) not null);


create table [dbo].products(
productID int identity (1,1) not null primary key,
productname varchar(50) not null,
productprice currency);


create table [dbo].orders(
orderNR int identity (1,1) not null primary key,
customer int,
orderdate datetime not null default current_timestamp,
paymentdate datetime null,
amountpaid currency,
ordercancelled bit not null default 0,
constraint c1 foreign key(customer) references [dbo].customers(customerID)
on update no action on delete cascade )


create table [dbo].orderitems
ItemID identity (1,1) not null primary key,
ordernr int,
product int,
quantity int,
constraint c2 foreign key (ordernr) references [dbo].orders (orderNR)
on update no action on delete cascade,
constraint c3 foreign key (product) references [dbo].products (productID)
on update no action on delete no action)


























--
Arthur Hoornweg

(In order to reply per e-mail, please just remove the ".net"
from my e-mail address. Leave the rest of the address intact
including the "antispam" part. I had to take this measure to
counteract unsollicited mail.)
.



Relevant Pages

  • Re: need help with nested subquery
    ... I have a table called inbound within a database called Vehicles, ... inbound stores one month of data for each customer, each customer has one or more vehicles, a vehicle sends in up to eighty reports a day similar to the data I posted earlier, ... Custimerid (primary key) int ...
    (microsoft.public.sqlserver.programming)
  • RE: multiple values
    ... create table customer (custID int nor null primary key, ... but how about when a field has multiple choices? ...
    (microsoft.public.sqlserver.programming)
  • Re: set based gurus help wanted
    ... >>store, and s2 holds the orders from customers. ... >>available quantity with the quantity ordered by a customer. ... >customer_id INT NOT NULL PRIMARY KEY, ...
    (microsoft.public.sqlserver.programming)
  • Re: writing strings instead of rabish to file using fprintf
    ... > please and help me finding the reason why this rabish is being ... use either int or unsigned ... And similarly with customer number. ... FAQ below. ...
    (comp.lang.c)
  • Re: Database Design Problem
    ... at your design that you would lose ProductCategoryID from the Orders table ... You should also have a customer and employee table and only include ... [EmployeeID [INT] ... You would also have primarykeys on OrderID in ORders and SerialNumber in ...
    (microsoft.public.sqlserver.programming)