473,735 Members | 1,875 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 39425
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
4298
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
8965
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
9466
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...
0
9330
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
9255
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
9202
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...
1
6748
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
6050
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();...
1
3278
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
3
2191
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.