473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to delete a record in a one to many situation when using a list in form

I am building a database and I am using a list where I can make
multiple choices. The data is like this

Master table
item1id
item2
index(item1id)

detail table
item1id
item2id
index (item1id, item2id)

item2 table
item2id
item2
index(item2id)

My form would have

Item1

list of all the choices for item2 table where I can make multiple
choices.

This piece is easy enough to do inserts with. Loop on the listbox
array and insert.
Now the question is what do I do to:
1. Make changes
2. Delete an item.

Do I instead of update, build a function where I delete all the items
in the detail table belonging to item1 and then insert the new list?
Or is there a more elegant way to handle this?

May 24 '06 #1
2 1397
rich wrote:
I am building a database and I am using a list where I can make
multiple choices. The data is like this

Master table
item1id
item2
index(item1id)

detail table
item1id
item2id
index (item1id, item2id)

item2 table
item2id
item2
index(item2id)

My form would have

Item1

list of all the choices for item2 table where I can make multiple
choices.

This piece is easy enough to do inserts with. Loop on the listbox
array and insert.
Now the question is what do I do to:
1. Make changes
2. Delete an item.

Do I instead of update, build a function where I delete all the items
in the detail table belonging to item1 and then insert the new list?
Or is there a more elegant way to handle this?


Nope, you should just define the foreign key with "ON DELETE CASCADE"
option. You should do something like:

ALTER TABLE DETAIL ADD CONSTRAINT FK_MASTER_DETAIL
FOREIGN KEY(ITEM1) REFERENCES MASTER(ITEM1) ON DELETE CASCADE;

This is how it works on the known EMP and DEPT tables:

SQL> alter table emp add constraint fk_dept_emp
2 foreign key(deptno) references dept(deptno) on delete cascade;

Table altered.

SQL>

--
Mladen Gogala
http://www.mgogala.com
May 24 '06 #2
Easiest way is to delete and then insert. But you should do it one
record at a time. You dont want to delete all at once and then insert.
Otherwise if you have a system crash right after the delete you could
lose massive amounts of data instead of just one record.

To do this you need to add a new character type 'delete' flag column in
your items table which would be used for this purpose.

1) Update all the records you want to delete by putting in X in the
deleteFlag.
..
2) Delete the first one whose key matches the one you are inserting
from the list.

3) Insert the new one from the list (leave the deleteFlag column null
or put a blank)

Repeat the 1-3 process until you've inserted all your records from the
list.

4) Now delete all the records that still have an X in the delete flag
column. These would be the left over records.

You can also use Transactions but its only safe as long as your server
will not crash during commit.

May 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tony Williams | last post by:
I have two tables one lists the names of committees and the other is a list of documents they generate. I have a form based on the documents table which gives details of the document including...
8
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: ...
3
by: Uwe Range | last post by:
Hi to all, I am displaying a list of records in a subform which is embedded in a popup main form (in order to ensure that users close the form when leaving it). It seems to be impossible to...
6
by: Bernd Smits | last post by:
Hi, I would like to delete a record (with commandbutton) of a table associated to a combobox, when I select a certain value in the combobox (the value I select is associated with the record that I...
4
by: Susan Bricker | last post by:
I have a command button on a form that is supposed to Delete the record being displayed. The record is displayed one to a form. The form is not a Pop-Up nor is it Modal. Tracing the btnDelete...
7
by: Stephen Poley | last post by:
I have the following situation: - a table of employees, keyed on employee-id; - a table of training sessions, keyed on session-id; - a requirement to log who was present at which session, plus...
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called...
5
by: Bob Bridges | last post by:
Start with two tables, parent records in one and child records in the other, a one-to-many relationship. Create a select statement joining the two. Display the query in datasheet mode. When I...
3
by: blakerrr | last post by:
Hi All, I have a strange situation that I can't figure out. The task is quite simple, delete a record from a table. Here is my situation: I have a form called Order Create which cycles through...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.