473,804 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

restrict record deletions between tables.

Here is an example of what I want to do (syntax might not be entirely
correct as this is just an example):

CREATE TABLE ParentA
(
name CHAR (6) NOT NULL;
address CHAR(64);
) IN CUSTOMER_TS INDEX IN CUSTOMER_TS;

CREATE TABLE Child1
(
name CHAR(6) NOT NULL;
otherinfo CHAR(64);
) IN CUSTOMER_TS INDEX IN CUSTOMER_TS:

ALTER TABLE ParentA PRIMARY KEY (name);
ALTER TABLE Child1 FOREIGN KEY CUSTNAME (name) REFERENCES ParentA ON
DELETE CASCASE;
CREATE TRIGGER name_insert AFTER INSERT ON ParentA REFERENCING NEW AS
new_temp FOR EACH ROW INSERT INTO Child1(name) VALUES (new_temp.name) ;

So basically, when a record is inserted into the ParentA table, a
Child1 records is also created. Also, If the ParentA record is
deleted, the Child1 record is also deleted.

What I also want to do is to restrict someone from deleting the record
in Child1 if the record in Parent1 exists (which it should always
exists). I just dont want them to be able to delete the child...if the
record is to be remove, then its both or nothing. I am also worried
about getting caught up behind the delete cascade too if I am able to
restrict the child from being deleted.

Any suggestions?

Jan 30 '06 #1
8 2079
shorti,
The following trigger over the child(s) might help u out !

=============== =============== =============== =============== =====

-- This script basically aborts the transaction!!
CREATE TRIGGER abortChildDel
NO CASCADE BEFORE DELETE ON Child1
REFERENCING OLD AS t1
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
DECLARE parent_name char(6) default '-1';

set parent_name= (select t2.name from parentA as t2, Child1 as t3
where t2.name=t3.name and t3.name=t1.name ); -- U can replace this with
the surrogate keys

IF parent_name is not null THEN
SIGNAL SQLSTATE 'Z0001' ('Deleting child not possible when parent is
alive');
END IF;
END@
=============== =============== =============== =============== ===

Jan 31 '06 #2
Can't you just REVOKE the DELETE privelidge from the user for the child
table?

Hmm... does CASCADE work on a child TABLE where the user only has
DELETE privilidges on the parent?

B.

Jan 31 '06 #3

Shyam Peri wrote:
shorti,
The following trigger over the child(s) might help u out !

=============== =============== =============== =============== =====

-- This script basically aborts the transaction!!
CREATE TRIGGER abortChildDel
NO CASCADE BEFORE DELETE ON Child1
REFERENCING OLD AS t1
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
DECLARE parent_name char(6) default '-1';

set parent_name= (select t2.name from parentA as t2, Child1 as t3
where t2.name=t3.name and t3.name=t1.name ); -- U can replace this with
the surrogate keys

IF parent_name is not null THEN
SIGNAL SQLSTATE 'Z0001' ('Deleting child not possible when parent is
alive');
END IF;
END@
=============== =============== =============== =============== ===


That seems to work well. I will be testing it some more but the
initial runs show it does what I need. Thanks for the info!

Jan 31 '06 #4
Brian Tkatch wrote:
Hmm... does CASCADE work on a child TABLE where the user only has
DELETE privilidges on the parent?


Yes, that's possible.

As long as the user has DELETE privileges on the parent table, he is not
doing anything forbidden. The FK was defined by the owner (or admin
or ...) of the child table and it says to delete the depending rows. So
the delete there is performed with the authorization of the table owner and
it runs through.

Things are different if you have ON DELETE RESTRICT/NO ACTION. Then an
explicit DELETE on the child table becomes necessary anyways, and that is
restricted as the user has not the privileges on the child table.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Jan 31 '06 #5
Ah, thanx. I don't have the ability to test it myself. For some reason
even the dev box here is restricted.

Perhaps then this is the best solution for the OP? I would certainly
avoid the TRIGGER is possible. Magically canceling a transaction can
cause confusion down the road.

B.

Jan 31 '06 #6
There were other reasons we could not revoke privileges on these table.

Also, the transaction will not be magically canceled. The failure
generates an sqlcode -438 with the SQLSTATE Z0001. I can flag it and
know precisely where the failure is.

Its good to know the CASCADE will not cause a problem if later I need
to revoke delete privileges on a table.

Thanks for the great info!

Feb 1 '06 #7
There were reasons in the past we did not want to revoke delete
privileges on these tables...I dont remember off hand what the concern
was but will do some research on it.

Also, the abort transaction above will not be magically canceled. The
failure generates an sqlcode -438 with the SQLSTATE Z0001. I can flag
it and know precisely where the failure is.

Its good to know the CASCADE will not cause a problem if later I need
to revoke delete privileges on a table.

Thanks for the great info!

Feb 1 '06 #8
>I dont remember off hand what the concern was but will do some research on it.

Just REVOKE it and see what breaks. :P
Also, the abort transaction above will not be magically canceled. The failure generates an sqlcode -438 with the SQLSTATE
Z0001. I can flag it and know precisely where the failure is.


And that it was effected by a TRIGGER? What if a later coder needs to
find out what is happening? I've just seen too many databases where
TRIGGERs are overused. So, in general, i relegate TRIGGERs to data
maintenance (e.g. automatically populated a history TABLE) or business
rules that cannot be formed as a data rule in a CONSTRAINT.

Ultimately, you are tyring to remove a priveledge from the user, and
the database has a way of supporting that directly. So, i'd try to use
that method, if possible.

I don't mean to tell you what to do, i just want argue the idea
properly. :)

B.

Feb 1 '06 #9

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

Similar topics

17
4478
by: Rick | last post by:
Hi all, Is there a MySQL function to get the first record through a query? I would like to open a page of client records with the first one shown. The other records can be accessed from a hyperlinked list. Thanks for any advice, Rick
2
1481
by: Rednelle | last post by:
Greetings all. Please help a real beginner using Access 2000. During experimentation with this program I have deleted many records from my main Table but the bottom left window always shows the actual number of records still existing. (Record ! of 32) When I open the corresponding Form the bottom left window shows 'Record 1 of 1'; I can add say 5 new records and look back perhaps to 'Record 2 of 5' - no problem so far. But if I close...
13
4993
by: Jim M | last post by:
I've been playing with Allen Browne's audit code and found it very useful. I need to track record insertions, deletions, and edits for several tables. I am planning to replace Access with Microsoft SQL server for my back end, but continue to use Access for the front end. I understand I can create an audit trail of record changes in SQL at the table level, instead of at the form level in Access. I have been playing with Access since the...
4
7853
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 event routine shows that AllowDeletions is TRUE. When the Delete button is clicked (without TRACE ON), I get a 'beep', the recordselector (vertical bar on left of form) gets dark in color, but the record is not deleted. Also, there is no error...
4
4353
by: Neil Coleclough | last post by:
I am constructing a database to process product returns for my Company. I have a number of toggle buttons to identify the stage to which each return has been processed. For example, clicking the first button confirms that the goods are now in our building awaiting initial processing. This button then fires off an email to our warehouse staff alerting them to the fact. I want to restrict these buttons to single use, so that once the email...
5
2973
by: toddles666 | last post by:
Hi- Is there any way of restricting access to a database by application & account? For example, I only want the application APP1 to access the database using the USER1 account. I've tried to use the DB2 Governor to do this, but it seems it always allows the the first query to be processed before forcing the connection. Here is my db2gov config file: --- start config
19
2151
by: MaXX | last post by:
Hi, I hope I'm not OT. I have the following issue: I want to delete a record from my db with a php script. Let's say I'm auth'd and I want to delete the record id 440. With a simple form (get or post), I send the id to my script and delete the record (DELETE FROM table WHERE id=some_validated_input). The problem is if I'm a nasty guy I just write my own form and delete any
6
4306
by: jpatchak | last post by:
Hello, I have a main form with one subform. I have a command button on the main form to delete the current record. Below is my code. The tables on which the main form and subform are based cascade deletions and updates, so getting rid of the record in the table for the main form should delete any corresponding records in the table for the subform. Everything appears to be working correctly in this code from the standpoint of the tables. ...
4
4409
by: Phil Stanton | last post by:
Sorry to repost, but am having another look at deleting a record. I have a form (Member) and have removed all the event procedures associated with the Form (OnCurrent, OnDelete, OnActivate etc) When I click on the record bar and press delete it comes up with the correct message "You are about to delete 1 record ....", the record count drops by 1 and I say "Yes - delete it". Seems OK, record is no longer there. Than after closing the form...
0
10562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10303
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10070
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.