473,753 Members | 6,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cascade update / foreign key

Hi!
For the sake of simplicity, I have three tables, Employee, Department and
Work

Employee >---- Department
\ /
\ /
^ ^
Work

The Work table have two columns, empno and depno and consists that the
employee has worked on another department.

Here is my scripts:
create table employee (empno int not null primary key, depno int not null)
create table department (depno int not null primary key)
create table work (empno int not null, depno int not null)

alter table employee add constraint fk_employee_dep artment foreign key
(depno)
references department (depno)
on update cascade

alter table work add constraint fk_work_employe e foreign key (empno)
references employee (empno)
on update cascade

alter table work add constraint fk_work_departm ent foreign key (depno)
references department (depno)
on update cascade

My problem is the last command. SQL Server responds:
Server: Msg 1785, Level 16, State 1, Line 1
Introducing FOREIGN KEY constraint 'fk_work_depart ment' on table 'work' may
cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON
UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

But I want the depno in the work table to be updated when a department.depn o
changes a value.

Does anyone have a suggestion on how to overcome this problem?

Thanks in advance

Best regards,
Gunnar Vøyenli
EDB-konsulent as
NORWAY
Jul 20 '05 #1
2 39426
Hi,
SQL statement that you had provided is generating circular refrence in
updates
Workaround is to use triger instead of foreign key integrity.

Comment the one of foriegn key sql and use triger.

Note this trigger are not tested. but is only for your reference and
other work around for problem.

/*alter table work add constraint fk_work_employe e foreign key (empno)
references employee (empno)
on update cascade
go
*/
create trigger trgUpdateEmpNoI nDept
on employee
for update
as
set nocount on

declare @NewEmpno int , @OldEmpno int

select @NewEmpno = inserted.empno from inserted
select @OldEmpno = deleted.empno from deleted

update work set work.empno = @NewEmpno
where work.empno = @OldEmpno
if @@rowcount=0 or @@error<>0
begin
Rollback tran
end

set nocount off
go

create trigger trgCheckEmpExis ts
on Work
For Update
as

set nocount on

declare @NewEmpno int
select @NewEmpno = inserted.empno from inserted

If Not Exists (Select * from employee where empno = @NewEmpno )
Begin
RAISERROR ('Empno do not exists', 16, 1)
Rollback Tran
End
set nocount off

go
Thanks & Regards, Amit

"Gunnar Vøyenli" <gv@edbkonsulen t.no> wrote in message news:<3f******* *@news.broadpar k.no>...
Hi!
For the sake of simplicity, I have three tables, Employee, Department and
Work

Employee >---- Department
\ /
\ /
^ ^
Work

The Work table have two columns, empno and depno and consists that the
employee has worked on another department.

Here is my scripts:
create table employee (empno int not null primary key, depno int not null)
create table department (depno int not null primary key)
create table work (empno int not null, depno int not null)

alter table employee add constraint fk_employee_dep artment foreign key
(depno)
references department (depno)
on update cascade

alter table work add constraint fk_work_employe e foreign key (empno)
references employee (empno)
on update cascade

alter table work add constraint fk_work_departm ent foreign key (depno)
references department (depno)
on update cascade

My problem is the last command. SQL Server responds:
Server: Msg 1785, Level 16, State 1, Line 1
Introducing FOREIGN KEY constraint 'fk_work_depart ment' on table 'work' may
cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON
UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

But I want the depno in the work table to be updated when a department.depn o
changes a value.

Does anyone have a suggestion on how to overcome this problem?

Thanks in advance

Best regards,
Gunnar Vøyenli
EDB-konsulent as
NORWAY

Jul 20 '05 #2
Thank you very much!
-Gunnar

"AMIT" <am***@zenithin fotech.com> wrote in message
news:f3******** *************** ***@posting.goo gle.com...
Hi,
SQL statement that you had provided is generating circular refrence in
updates
Workaround is to use triger instead of foreign key integrity.

Comment the one of foriegn key sql and use triger.

Note this trigger are not tested. but is only for your reference and
other work around for problem.

/*alter table work add constraint fk_work_employe e foreign key (empno)
references employee (empno)
on update cascade
go
*/
create trigger trgUpdateEmpNoI nDept
on employee
for update
as
set nocount on

declare @NewEmpno int , @OldEmpno int

select @NewEmpno = inserted.empno from inserted
select @OldEmpno = deleted.empno from deleted

update work set work.empno = @NewEmpno
where work.empno = @OldEmpno
if @@rowcount=0 or @@error<>0
begin
Rollback tran
end

set nocount off
go

create trigger trgCheckEmpExis ts
on Work
For Update
as

set nocount on

declare @NewEmpno int
select @NewEmpno = inserted.empno from inserted

If Not Exists (Select * from employee where empno = @NewEmpno )
Begin
RAISERROR ('Empno do not exists', 16, 1)
Rollback Tran
End
set nocount off

go
Thanks & Regards, Amit

"Gunnar Vøyenli" <gv@edbkonsulen t.no> wrote in message

news:<3f******* *@news.broadpar k.no>...
Hi!
For the sake of simplicity, I have three tables, Employee, Department and Work

Employee >---- Department
\ /
\ /
^ ^
Work

The Work table have two columns, empno and depno and consists that the
employee has worked on another department.

Here is my scripts:
create table employee (empno int not null primary key, depno int not null) create table department (depno int not null primary key)
create table work (empno int not null, depno int not null)

alter table employee add constraint fk_employee_dep artment foreign key
(depno)
references department (depno)
on update cascade

alter table work add constraint fk_work_employe e foreign key (empno)
references employee (empno)
on update cascade

alter table work add constraint fk_work_departm ent foreign key (depno)
references department (depno)
on update cascade

My problem is the last command. SQL Server responds:
Server: Msg 1785, Level 16, State 1, Line 1
Introducing FOREIGN KEY constraint 'fk_work_depart ment' on table 'work' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

But I want the depno in the work table to be updated when a department.depn o changes a value.

Does anyone have a suggestion on how to overcome this problem?

Thanks in advance

Best regards,
Gunnar Vøyenli
EDB-konsulent as
NORWAY

Jul 20 '05 #3

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

Similar topics

33
4302
by: Lee C. | last post by:
I'm finding this to be extremely difficult to set up. I understand that Access won't manage the primary key and the cascade updates for a table. Fine. I tried changing the PK type to number and setting default value to a UDF that manages the auto-numbering. Access won't take a UDF as a default value. Okay, I'll use SQL WITHOUT any aggregate functions, for the default value. Access won't do that either. Okay, I create a second...
7
2429
by: PC Datasheet | last post by:
Looking for suggestions ---- A database was designed for a national automobile inspection program. In it's simplest form, the database has two tables: TblOwner OwnerID <Year/Make/Model owned by owner, Owner name/address, etc) TblInspection InspectionID
0
2967
by: hans99 | last post by:
I have made an little database, can someone help me to give an little cascade update script for mine database.. create table Boektypen ( bkt_BoektypeID integer not null, bkt_Boektypenaam varchar(35) not null, bkt_Jeugdgeschikt bit, primary key (bkt_BoektypeID)) go
5
2477
by: alanpang | last post by:
Hi, I've 3 Tables, Return Product, Shipping, and RMA. The Shipping table has 2 foreign key, 1 connected to RMA, another connected to Return_Product. The primary of the Return_Product is a auto generated int. So, how am supose to do update the foregin key when a new product is inserted to Return_Product Table.? Please help. I'm new on designing SQL table. I'll appreaciate your help...thanks. AP
0
9072
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8896
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9451
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6869
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
6151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4771
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
4942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3395
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.