Cascade Update / Foreign Key

Jul 20, 2005

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_department foreign key
(depno)
references department (depno)
on update cascade

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

alter table work add constraint fk_work_department 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_department' 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.depno
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

View 2 Replies


ADVERTISEMENT

Foreign Key Cascade Delete Failure

Mar 7, 2007

I have a SQL Mobile DB that I am having problems with deletes cascading via foreign keys to delete all child records. The DB is on a WIndows CE5 device that is running a C#.net application.

The are three tables in my DB that relate to this issue (Tests, TestRawInfo, and TestRawData). The Tests table is the main table. TestRawInfo is a child table of Tests and has a foreign key defined that references the Tests primary key (the relationship is 1:1 with the Tests table). TestRawData is a child table of TestRawInfo and has a foreign key defined that references the TestRawInfo primary key (The relationship is 1:many with the TestRawInfo table). All foreign keys are defined with a Cascade on delete. When I delete one or more records from the Tests table I expect the delete to cascade so that all child records are also deleted. Not all the data gets deleted from the TestRawData table, this results in orphan records. I only see a failure however the next time I attempt to compact the database.

Interestingly I can reproduce the problem by opening my Mobile DB in SQL 2005 on my Desktop and deleting data from the Tests table. If however, I add additional records to these tables through SQL 2005 before attempting to delete, the delete works as expected.

Do you have any ideas on what is going on here? Has any one else reported a similar issue? My current work around is to delete data directly from child tables and not rely on the foreign keys to cascade the deletes.

View 5 Replies View Related

Cascade Delete Can Not Apply To Two Foreign Keys

Jul 31, 2004

I have a table that contains two foreign keys of two different tables. I want to build a relationship so that when either primary keys deleted in the two tables, the record in the table should be deleted. But, SQL Server does not allow me to save the relationship, it complains that the circling delete might exist. I do not know why, how can I solve this?

Table A:
ID
ProductID <foreign key>
CustomerID <foreign key>

Table Product
ProductID <primary key>

Table Customer
CustomerID <primary key>

I want to cascade delete the record in Table A when either the ProductID is deleted from Product table or the CustomerID is deleted from Customer table.

View 1 Replies View Related

Conflict Between (cascade) DELETE Trigger And Foreign Key Constrain

Oct 19, 2005

I'm trying to create relational database with some triggers in SQL Server 7.0, but it doesn't work as expected. Let's say that I have 'Office' database with two tables, 'Users' and 'UserRights' (userRights table should have much more rights, but that's not relevant for this problem):

CREATE TABLE [Users] (
[FS_Username] [nvarchar] (8) NOT NULL ,
[FS_Password] [nvarchar] (32) NOT NULL ,
CONSTRAINT [PK_Users] PRIMARY KEY NONCLUSTERED
(
[FS_Username]
) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [UserRights] (
[FS_Username] [nvarchar] (8) NOT NULL ,
[FI_UserType] [int] NOT NULL CONSTRAINT [DF_UserRights_FI_UserType] DEFAULT (1),
[FI_AllowLogin] [int] NOT NULL CONSTRAINT [DF_UserRights_FI_AllowLogin] DEFAULT (1),
CONSTRAINT [PK_UserRights] PRIMARY KEY NONCLUSTERED
(
[FS_Username]
) ON [PRIMARY] ,
CONSTRAINT [FK_UserRights_Users] FOREIGN KEY
(
[FS_Username]
) REFERENCES [Users] (
[FS_Username]
)
) ON [PRIMARY]
GO

Foreign Key CONSTRAIN above is created by adding both tables to the diagram and defining relationship between these two tables FS_Username field, where 'Enable relationship for INSERT and UPDATE' option is turned ON. You can easily see this if you create diagram youself and insert these two tables in it.
Next to this, I created two triggers that should handle inserting/deleting rows in UserRights table as consequence of inserting/deleting rows in Users table:

CREATE TRIGGER InsertUserRights ON Users
FOR INSERT
AS
BEGIN
INSERT INTO UserRights (FS_Username) (SELECT FS_Username FROM Inserted)
END

CREATE TRIGGER DeleteUserRights ON Users
FOR DELETE
AS
BEGIN
DELETE UserRights WHERE FS_Username IN
(SELECT FS_Username FROM Users)
END

Now, when (manually) I insert row in Users table, UserRights table gets updated accordingly. HOWEVER, when I try to delete one or more entries from Users table, I get error report. For example, if you try to execute following two commands:

Insert Into Users (FS_Username, FS_Password) VALUES ('John', 's')

Delete from Users

... first command will succede, but second one will fail with message:

DELETE statement conflicted with COLUMN REFERENCE constraint 'FK_UserRights_Users'. The conflict occurred in database 'Office', table 'UserRights', column 'FS_Username'.
The statement has been terminated.

Does anyone know how to resolve this problem without loosing constrains and triggers ? (If I turn off 'Enable relationship for INSERT and UPDATE' option for relationship, things will work fine, but than I can make inconsistent data in UserRights table).

tnx a lot,
Dejan

View 1 Replies View Related

Copy And Delete Table With Foreign Key References(...,...) On Delete Cascade?

Oct 23, 2004

Hello:
Need some serious help with this one...

Background:
Am working on completing an ORM that can not only handles CRUD actions -- but that can also updates the structure of a table transparently when the class defs change. Reason for this is that I can't get the SQL scripts that would work for updating a software on SqlServer to be portable to other DBMS systems. Doing it by code, rather than SQL batch has a chance of making cross-platform, updateable, software...

Anyway, because it needs to be cross-DBMS capable, the constraints are that the system used must work for the lowest common denominator....ie, a 'recipe' of steps that will work on all DBMS's.

The Problem:
There might be simpler ways to do this with SqlServer (all ears :-) - just in case I can't make it cross platform right now) but, with simplistic DBMS's (SqlLite, etc) there is no way to ALTER table once formed: one has to COPY the Table to a new TMP name, adding a Column in the process, then delete the original, then rename the TMP to the original name.

This appears possible in SqlServer too --...as long as there are no CASCADE operations.
Truncate table doesn't seem to be the solution, nor drop, as they all seem to trigger a Cascade delete in the Foreign Table.

So -- please correct me if I am wrong here -- it appears that the operations would be
along the lines of:
a) Remove the Foreign Key references
b) Copy the table structure, and make a new temp table, adding the column
c) Copy the data over
d) Add the FK relations, that used to be in the first table, to the new table
e) Delete the original
f) Done?

The questions are:
a) How does one alter a table to REMOVE the Foreign Key References part, if it has no 'name'.
b) Anyone know of a good clean way to get, and save these constraints to reapply them to the new table. Hopefully with some cross platform ADO.NET solution? GetSchema etc appears to me to be very dbms dependant?
c) ANY and all tips on things I might run into later that I have not mentioned, are also greatly appreciated.

Thanks!
Sky

View 1 Replies View Related

Cascade Update

Jul 8, 2006

Does anyone know how to do a cascade update in SQL 2005 using the studio manager?Basically, I have a project table with a status column. If the status is set to 2, then I need to update another table that references the project id.

View 1 Replies View Related

On Update Cascade

Aug 27, 2006

Hello all,
I am new to SQL and I was hoping someone could explain something to me about 'on update cascade'.
I understand what 'on update cascade' does (i think) - it updates the child table when the parent table is updated.
What i do not understand is that the 'on update cascade' works on the primary key of the parent table, but i was on the understanding that the primary key doesn't change often if at all so why would the 'on update cascade' be of use?
Sorry for my ignorance and i realise i must be missing something simple but would be grateful for any help.
Thanks

View 5 Replies View Related

Cascade Delete And Update In SQL 7.0

Jul 23, 1999

Is it possible to cascade update and delete while DRI remains there ? That
is without deleteing refrential integrity. If anyone have example of cascade delete and Update using Pubs or Northwind Example , i'll be really obliged.

View 1 Replies View Related

Problem With Cascade Update

Apr 8, 2007

i want to reffer A1,A2 column from a Table A to B1 column of Table B. when i use the on update cascade option for the columns A1 & A2 i get an error for the 2nd one. sql:

alter table A
add constraint fk_key1 foreign key (a1) references b(b1) on update cascade;

alter table A
add constraint fk_key2 foreign key (a2) references b(b1) on update cascade;

after executing the sql i get the following error for the 2nd sql:

Introducing FOREIGN KEY constraint 'FK_key2' on table 'A' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

if i dont use cascade option then i dont get any error.

but i need to synchronize the data. now can anyone help me how to add the cascade option working for the 2nd column of Table A without seperating the column in a different table?

thnx

S ]-[ /- | ]-[ /- N

View 2 Replies View Related

Cascade Update/deletes

Apr 28, 2008

Hi guys,

A simple question of good practices: is it better to have a cascade update/delete on a table? Or is it better to do it "by hand" in a SP?


Thanks a lot.

View 1 Replies View Related

Trigger Cascade Update

Jul 29, 2007

i run this cmd to execute a cascade update to another database but did not work.

USE [testdb1]
GO
/****** Object: Trigger [dbo].[tgr_Upddb2] Script Date: 07/29/2007 08:34:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tgr_Upddb2]
ON [dbo].[Table1]
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON

UPDATE U
SET u.fk_fld = i.pk_fld
FROM inserted AS I
JOIN testdb2.dbo.table2 AS U
ON U.fk_fld = I.pk_fld
END


heres the db structure

View 3 Replies View Related

Update And Delete Rule - Cascade

Jan 15, 2007

Hi, I have a database which saves data about bus links. I want to provide a information to passenger about price of their journay. The price depends on three factors: starting busstop, ending busstop and type of ticket (full, part - for students and old people, ...).
So I created a table with three foreign key constraints (two for busstops and one for type).
When the busstop is deleted or type of ticket I want all data connected with it to be deleted automatically. I wanted to use cascade deleting.
But I receive a following exception: Introducing FOREIGN KEY constraint 'FK_TicketPrices_BusStops1' on table 'TicketPrices' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
How can I achieve my task? Why should it cause cycles or multiple cascade paths?

View 1 Replies View Related

SQL Query For Setting Cascade On Update

Aug 22, 2007

Hi,

I'm looking for a query I can use to alter table relationships. What I want to do in particular, is to set every relationship to cascade on update. Can anyone point me out to a solution? MSDN seems very vague in this subject.

Thanks,
Tiago

View 1 Replies View Related

On DELETE On UPDATE Cascade Syntax Error

Dec 13, 2006

Hello
I need to be able to regularly, update or delete data from my parent table and subsequent child tables from A to Z, each table contains data.  However, I have having problems.
I have already created  the tables with primary keys on each table and foreign keys linking each table to the next.
I tried to delete a row from the parent table and was given this error:
DELETE FROM [dbo].[DomNam]WHERE [DomNam]=N' football '
Error: Query(1/1) DELETE statement conflicted with COLUMN REFERENCE constraint 'FK_DomNam'. The conflict occurred in database 'DomDB', table 'Dom_CatA', column 'DomNam'.
 
I tried to insert an alter table query:
ALTER TABLE dbo.DomNamADD CONSTRAINT FK_Dom_ID
REFERENCES dbo.Dom_CatA (Dom_ID)
ON DELETE CASCADE ON UPDATE CASCADE
But on Execute I saw this error:
Error]  Incorrect syntax for definition of the 'TABLE' constraint
What is wrong with the above syntax?
Or would it be better if I used a trigger instead because I already have foreign keys set within the tables?If so please give an example of the syntax for the trigger I would need to update and cascade data from all tables. 
I would be grateful for any advice.  Thanks.
 
 
 

View 8 Replies View Related

Remove Cascade Update And Delete From Table

Jul 12, 2007

hello, once upon a time when i created my db (originally in access then used the conversion tool, which i now know is wrong!) i thought it would be an amazing idea to have cascading updates and deletes, however it turns out now this is exactly not what i want! if i leave them in then it throws errors when i delete records out of my stock table as related records are in the order_line table here is the code (well i think so, im not the best at sqlserver as you probably can tell already) that im using if anyone can help or point me in the right direction that would be great, thanks USE [nashdfDB1]GO/****** Object:  Table [dbo].[tbl_stock]    Script Date: 07/13/2007 02:52:14 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[tbl_stock](            [Stock_ID] [int] IDENTITY(1,1) NOT NULL,            [cat_id] [int] NOT NULL CONSTRAINT [DF__tbl_stock__cat_i__15502E78]  DEFAULT ((0)),            [sub_cat_id] [int] NULL CONSTRAINT [DF__tbl_stock__sub_c__164452B1]  DEFAULT ((0)),            [location] [int] NULL CONSTRAINT [DF__tbl_stock__locat__173876EA]  DEFAULT ((0)),            [n_or_sh] [varchar](50) NULL,            [title] [varchar](255) NULL,            [description] [varchar](255) NULL,            [size] [varchar](50) NULL,            [colour] [varchar](50) NULL,            [cost_price] [decimal](9, 2) NULL CONSTRAINT [DF__tbl_stock__cost___182C9B23]  DEFAULT ((0)),            [selling_price] [decimal](9, 2) NULL CONSTRAINT [DF__tbl_stock__selli__1920BF5C]  DEFAULT ((0)),            [qty] [varchar](50) NULL,            [date] [datetime] NULL CONSTRAINT [DF__tbl_stock__date__1A14E395]  DEFAULT (getdate()),            [condition] [varchar](255) NULL,            [notes] [varchar](255) NULL,            [visible] [bit] NULL CONSTRAINT [DF__tbl_stock__visib__1B0907CE]  DEFAULT ((1)),            [picture1] [varchar](50) NULL,            [picture1_thumb] [varchar](50) NULL,            [picture2] [varchar](50) NULL,            [picture2_thumb] [varchar](50) NULL,            [picture3] [varchar](50) NULL,            [picture3_thumb] [varchar](50) NULL,            [picture4] [varchar](50) NULL,            [picture4_thumb] [varchar](50) NULL,            [display_price] [varchar](50) NULL,            [created_by] [varchar](50) NULL,            [buying_in_recipt] [varchar](255) NULL, CONSTRAINT [tbl_stock$PrimaryKey] PRIMARY KEY CLUSTERED (            [Stock_ID] ASC)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]) ON [PRIMARY] GO
SET ANSI_PADDING OFF
 
Regards
Jez

View 1 Replies View Related

Query For Setting Cascade On Update In Table Relationships

Aug 22, 2007

Hi,

I'm looking for a query I can use to alter table relationships. What I want to do in particular, is to set every relationship to cascade on update. Can anyone point me out to a solution? MSDN seems very vague in this subject.

Thanks,
Tiago

View 2 Replies View Related

How To Alter The Table With Delete/update Cascade Without Recreating The Table

Jul 26, 2004

I have contract table which has built in foreign key constrains. How can I alter this table for delete/ update cascade without recreating the table so whenever studentId/ contactId is modified, the change is effected to the contract table.

Thanks


************************************************** ******
Contract table DDL is

create table contract(
contractNum int identity(1,1) primary key,
contractDate smalldatetime not null,
tuition money not null,
studentId char(4) not null foreign key references student (studentId),
contactId int not null foreign key references contact (contactId)
);

View 3 Replies View Related

Automatic Foreign Key Update

Apr 13, 1999

Hello to everyone:

Aside from using a trigger to automatically update a Foreign key,
is there a way you can automatically update a Foreign key when you
update its corresponding Primary key using the select statement?

Grigio

View 1 Replies View Related

Update Row With Foreign Key Relationships

Mar 3, 2008

Hi,

I have 2 tables Publication and Advertisment_Codes. I want to update the publication_code in both tables where country_id=1 and publication_code='A'. However, it gives me an error because of foreign key relationship. I tried first updating in Advertisment_Codes and then in Publication table but it didn't work. I tried the other way round but also it didn't work.
How can I update the publication code?
Thanks


CREATE TABLE [dbo].[Advertisment_Codes](
[Advertising_Code] [nvarchar](20) NOT NULL,
[Country_id] [int] NOT NULL CONSTRAINT [DF_Advertisment_Codes_Country_id] DEFAULT ((0)),
[Publication_code] [nvarchar](20) NULL,
[Advert_type] [nchar](1) NULL,
[Date_Entered] [datetime] NULL,
[User_Id_Ent] [int] NULL CONSTRAINT [DF_Advertisment_Codes_User_Id_Ent] DEFAULT ((0)),
[Date_Updated] [datetime] NULL,
[User_Id_Upd] [int] NULL CONSTRAINT [DF_Advertisment_Codes_User_Id_Upd] DEFAULT ((0)),
[Default_Code] [bit] NULL CONSTRAINT [DF_Advertisment_Codes_Default_Code] DEFAULT ((0)),
[Active] [bit] NULL CONSTRAINT [DF_Advertisment_Codes_Active] DEFAULT ((0)),
CONSTRAINT [PK_Advertisment_Codes_1] PRIMARY KEY CLUSTERED
(
[Advertising_Code] ASC,
[Country_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Advertisment_Codes] WITH CHECK ADD CONSTRAINT [FK_Advertisment_Codes_Countries] FOREIGN KEY([Country_id])
REFERENCES [dbo].[Countries] ([Country_id])
GO
ALTER TABLE [dbo].[Advertisment_Codes] CHECK CONSTRAINT [FK_Advertisment_Codes_Countries]
GO
ALTER TABLE [dbo].[Advertisment_Codes] WITH CHECK ADD CONSTRAINT [FK_Advertisment_Codes_Publications] FOREIGN KEY([Country_id], [Publication_code])
REFERENCES [dbo].[Publications] ([Country_id], [Publication_code])
GO
ALTER TABLE [dbo].[Advertisment_Codes] CHECK CONSTRAINT [FK_Advertisment_Codes_Publications]

CREATE TABLE [dbo].[Publications](
[Country_id] [int] NOT NULL,
[Publication_code] [nvarchar](20) NOT NULL,
[Publication_name] [nvarchar](50) NOT NULL,
[Media_type] [int] NOT NULL CONSTRAINT [DF_Puclications_Media_type] DEFAULT ((0)),
[Date_Entered] [datetime] NULL,
[User_Id_Ent] [int] NULL CONSTRAINT [DF_Publications_User_Id_Ent] DEFAULT ((0)),
[Date_Updated] [datetime] NULL,
[User_Id_Upd] [int] NULL CONSTRAINT [DF_Publications_User_Id_Upd] DEFAULT ((0)),
CONSTRAINT [PK_Puclications] PRIMARY KEY CLUSTERED
(
[Country_id] ASC,
[Publication_code] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Publications] WITH CHECK ADD CONSTRAINT [FK_Publications_Countries] FOREIGN KEY([Country_id])
REFERENCES [dbo].[Countries] ([Country_id])
GO
ALTER TABLE [dbo].[Publications] CHECK CONSTRAINT [FK_Publications_Countries]

Whisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much.

View 2 Replies View Related

Update Table, Foreign Keys Etc

Aug 16, 2004

I have two tables as below. I want to update a quote and change the item for which it is for. So I want to do an update statement to change the cat_ref that the quote is for. However, when I do this I get a foreign key conflict with cat_ref in the item table. How do I get around this? Thanks


Code:

CREATE TABLE Item (
cat_ref VARCHAR(5)PRIMARY KEY,
descrip VARCHAR(50),
date_added SMALLDATETIME,
cat_type VARCHAR(20))


CREATE TABLE has_quote (
quote_id INT IDENTITY (1,1) PRIMARY KEY,
date_last_pricecheck SMALLDATETIME,
cat_ref VARCHAR(5)FOREIGN KEY
REFERENCES Item(cat_ref)
ON DELETE CASCADE,
first_name VARCHAR(10),
surname VARCHAR(10),
FOREIGN KEY (first_name, surname)
REFERENCES Customer(first_name, surname)
ON DELETE CASCADE
ON UPDATE CASCADE)

View 5 Replies View Related

UPDATE Problem With Foreign Key Constraints

Feb 9, 2004

I have a table that I want to run an UPDATE statement to change some data in the table, but I get this error:

Server: Msg 547, Level 16, State 1, Line 1
UPDATE statement conflicted with COLUMN FOREIGN KEY constraint 'FK__Yield__Financial_Product__ProductCode'. The conflict occurred in database 'lsmdb', table 'Financial_Product', column 'ProductCode'.
The statement has been terminated.


How do I update the ProductCode column in both tables to reflect my updated data?

Thanks in advance.

View 6 Replies View Related

Update Foreign Keys Using Triggers

May 18, 2008

Hello,

I would like to update foreign keys using triggers. At least thats what I think is the solution when having multiple references to a table in order to avoid the "cycles or multiple cascade paths" error message.

Anyway, here are three tables
Dentist table
dentist_id int identity (auto increment)

Patient table:
patient_id int identity (auto increment)

Appointment:
apointment_id int identity (auto increment)
id_dentist int FK to dentist_id
id_patient int FK to patient_id

I am gooling but cant find a way to make a trigger run only when the dentist_id from dentist table is updated. Also, is there a way to get the new id and old id somehow? I saw some posts with new.dentist_id and old.dentist_id but apparently is not for sql server.

Thank you.

View 2 Replies View Related

Commit Foreign Key/field Update

Jul 26, 2007

is there a way to cascade update to foreign keys where both fields (PK & FK) are located in two different database?

View 2 Replies View Related

Identity Column As Foreign Key In Bulk Update

Apr 17, 2007

We need to import data from flat/xml files into our database.
we plan to do so in bulk as amount of data is huge, 2GB+
we need to do some validation checks in our code after that we create insert queries.

We have identity columns that are used as foreign keys in child tables. Question is how can I write a bulk/batch insert statement that will propogate the identity column to the child, as for all other we are creating the query in the application memory.
there are 2 parent tables and 1st table value needs to be referred to in 7 tables and second table's value in 6.


Thanks much for your help.

View 1 Replies View Related

Cascade

Mar 6, 2000

Hi! I'm new to SQL server. I need help.

I have 2 tables:

Table A (A_ID, X) where A_ID is the primary key.
Table B (B_ID, A_ID, Y) where B_ID is the primary key, and A_ID is the foreign key.

I want to change the value of A_ID of Table A, say A1 into A2. How can I automatically change the value of A_ID in Table B if the record exists?

How can I delete a record in Table A and the corresponding record in Table B is deleted automatically?

Thanks in advance!

Chung

View 1 Replies View Related

Can't Cascade Delete

Feb 14, 2007

I have a Sql Server 2005 table with 3 fields, an ID field (primary key), a parent ID field, and Name.  The parent ID references the ID field (foreign to primary - many to one) within the same table so that records can reference their parent.  I would like to place a cascade delete on the ID field so that when the primary ID is removed it will automatically remove all those records with a parent ID that match.  Sql server does not allow me to establish this cascade delete.I was considering a trigger instead but only know how tio use the AFTER paramter and not an alternative. Thanks 

View 2 Replies View Related

Cascade Deletes

Jun 11, 2002

Hi,

We are developing a new oltp application using SQL Server 2000 and are debating whether to use "cascade delets" or not. In earlier apps, we had issues using cascade deletes like someone deleted parent by mistake and all child records got deleted OR SQL Server crashed in middle of cascade delete and records were lost or performance became an issue when there were huge # of child records to be deleted,etc.

Are there any recommendations for/against using Cascade deletes and when to use and when NOT to use cascade deletes ?

Thanks

Satish

View 2 Replies View Related

CASCADE Question

Nov 6, 2006

OK, so now I'm just being lazy, but after seeing a question about this, I wonder which is actually better?

Assuming that you have SPROC only access to your data, which is better/more effeicient.

Having the stored procedure actually do the deletes/updates (which is the way I always do this) or enabling CASCADE DELETE/UPADTE in the database?

Just curious, what do most people do.

Logically to me, the "UPDATE" is actually a logical DELETE and INSERT anyway because it's a new "thing"

Unless of course you buy into surrogates



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam

View 12 Replies View Related

How To Use Cascade Trigger

Apr 23, 2007

i just got a job and i have to learn triggers to pass my test , however,i'mfinding the cascade trigger a bit difficult, does it update other tables automatically when a column is updated?

View 2 Replies View Related

Cascade Delete

Nov 13, 2007

I am a C# programmer also acting as the dba for a SQL Server project. I have a table called Folder that has child records in a table called FolderItems. When a Folder is deleted I want the FolderItems to be deleted. I know how to accomplish via code, but I would prefer to have SQL Server perform the delete just in case a folder is deleted via SQL Management Studio or other method.

Is a trigger the way to go about this? I know how to create triggers For Delete, but I just wondering is this the best method to create a Cascade delete or does SQL have any built in "Cascade" delete features.

I am using SQL Server 2005 if this matters.

Thank you,

Corby Nichols
Flying Elephant Software

View 7 Replies View Related

ON DELETE CASCADE

Mar 7, 2008

Helle people. The question is:
I have two tables: Directories and SubDirectories. SubDirectories table has foreign key for DirectoryId. Now if I define this key with ON DELETE CASCADE attribute then in case of I delete a Directory record, all SubDirectory records will also be deleted. But if I have only one table Directories that have field named ParentId and in this field I save an Id of parent direcory. Can I define a parentId field as a foreign key to the same table and define it with ON DELETE CASCADE? Will it work too? Thanks

View 1 Replies View Related

Delete Cascade

Mar 12, 2008



Hi,

I want to know more about delete cascade feature of SQL Server 2005.

View 6 Replies View Related

How Can I Use Cascade Delete For The For The Following Situation.

Jan 25, 2008

Hi, I have the following tables:
Categories {Category_ID, Column2, ...} 
Articles { Article_ID, Category_FK, Column3, ...}
Discussions {Discussion_ID, Article_FK, Column3, ...}
Now, all what I have is just category_ID value (Let us say 3), how can I do cascade delete to delete category's record that its ID = 3 and delete all articles and all discussions that found in that category? 
 

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved