SQL How to change value With all References - sql

Is there an easier way to change a single value in a row together with its Reference , and that Reference's reference, Instead of backtracking through 20 tables to find the beginning of it all?
Links to Answers
--On Cascade (Thanks Javier)
ALTER TABLE catalog DROP CONSTRAINT aa
ALTER TABLE catalog ADD CONSTRAINT
(FOREIGN KEY (stock_num, manu_code) REFERENCES stock
ON DELETE CASCADE CONSTRAINT ab)

Have you tried to define the reference as "ON UPDATE CASCADE"?

Related

How to put Cascade on Delete once the Fk constraint has already been defined [duplicate]

I have a table representing users. When a user is deleted I get:
DELETE statement conflicted with the REFERENCE constraint
Apparently, CASCADE DELETE is not as easy as I imagined in SQL Server, and the option needs to be added to the table.
The problem is: I cannot figure out how to add the CASCADE DELETE option.
I'm using: SQL Server 2008. Any ideas how to do this?
Read this Microsoft article first. Read Me. I use the GUI during design so here is a picture of how it is selected in SSMS.
The syntax added to the foreign key is " ON DELETE CASCADE "
Here's the way I would add the "cascading delete" feature to an existing foreign key in SQL Server Management Studio.
First, find your foreign key, and open it's "DROP and CREATE To" in a new Query window.
Then, just add "ON DELETE CASCADE" to the "ADD CONSTRAINT" command:
Then just hit hit the "Execute" button to run the query.
Job done !
Google ALTER TABLE DROP CONSTRAINT, then ALTER TABLE ADD CONSTRAINT:
ALTER TABLE
Here's a quick example:
CREATE TABLE A
(
ID INTEGER NOT NULL UNIQUE
);
CREATE TABLE B
(
ID INTEGER NOT NULL UNIQUE
CONSTRAINT fk__B__A
REFERENCES A (ID)
);
-- Oops! Forgot the CASCADE referential actions.
-- DROP the constraint then recreate it:
ALTER TABLE B DROP
CONSTRAINT fk__B__A;
ALTER TABLE B ADD
CONSTRAINT fk__B__A
FOREIGN KEY (ID)
REFERENCES A (ID)
ON DELETE CASCADE
ON UPDATE CASCADE;

Two FKs pointing to same parent column - ON UPDATE CASCADE - SQL Server

Here's the scenario.
- Parent Table: TEAMMEMBERS, with a primary key RecID
- Child Table: TEAMMEMBERTASKS
I have two columns in the TEAMMEMBERTASKS table, ReportedBy and AssignedTo.
Both of these columns use the RecID to store which team member reported a task and which team member the task is assigned to. The RecID could be the same for both columns, but that is not always the case.
I need to add in a FK for both child columns that check the relationship to the parent, and I would like to add ON UPDATE CASCADE to both of the foreign keys.
Whenever I try to do this, my second foreign key throws a 'may cause cycles or multiple cascade paths' error.
Here's my code:
ALTER TABLE [dbo].[TEAMMEMBERTASKS] WITH CHECK ADD CONSTRAINT
[FK_AssignedTo_TeamMemberRecID] FOREIGN KEY([AssignedTo])
REFERENCES [dbo].[TEAMMEMBERS] ([RecID])
GO
ALTER TABLE [dbo].[TEAMMEMBERTASKS] CHECK CONSTRAINT
[FK_AssignedTo_TeamMemberRecID]
GO
ALTER TABLE [dbo].[TEAMMEMBERTASKS] WITH CHECK ADD CONSTRAINT
[FK_ReportedBy_TeamMemberRecID] FOREIGN KEY([ReportedBy])
REFERENCES [dbo].[TEAMMEMBERS] ([RecID])
ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[TEAMMEMBERTASKS] CHECK CONSTRAINT
[FK_ReportedBy_TeamMemberRecID]
GO
With the current code, will this cause the RecID to be updated in both child columns or will it cause the update command to be restricted?
Should I just go ahead and write up a trigger that deals with this instead?

Foreign key reference for two columns

I need to set two foreign key references for two different columns to my article table.
Both of them have to reference to the same column of my material table.
But if i try to create the second i got the following error:
Meldung 1785, Ebene 16, Status 0, Zeile 3
Das Einführen der FOREIGN KEY-Einschränkung 'FK_db00_02_Artikelstamm_WST_db08_01_Werkstoffe_Bezeichnung' für die db00_02_Artikelstamm-Tabelle kann Schleifen oder mehrere Kaskadepfade verursachen. Geben Sie ON DELETE NO ACTION oder ON UPDATE NO ACTION an, oder ändern Sie andere FOREIGN KEY-Einschränkungen.
The introduction of the FOREIGN KEY constraint 'FK_db00_02_Artikelstamm_WST_db08_01_Werkstoffe_Bezeichnung' for db00_02_Artikelstamm table can cause loops or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints
I try to create them with this SQL code:
-- Fremdschlüssel db00_02_Artikelstamm.WST -> db08_Werkstoffe.Bezeichnung
-- FK_db00_02_Artikelstamm_WST_db08_Werkstoffe_Bezeichnung
ALTER TABLE [dbo].[db00_02_Artikelstamm] WITH CHECK
ADD CONSTRAINT [FK_db00_02_Artikelstamm_WST_db08_Werkstoffe_Bezeichnung] FOREIGN KEY ([WST])
REFERENCES [dbo].[db08_Werkstoffe] ([Bezeichnung]) ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[db00_02_Artikelstamm] CHECK CONSTRAINT [FK_db00_02_Artikelstamm_WST_db08_Werkstoffe_Bezeichnung]
GO
-- Fremdschlüssel db00_02_Artikelstamm.WSTgroup -> db08_Werkstoffe.Bezeichnung
-- FK_db00_02_Artikelstamm_WSTgroup_db08_Werkstoffe_Bezeichnung
ALTER TABLE [dbo].[db00_02_Artikelstamm] WITH CHECK
ADD CONSTRAINT [FK_db00_02_Artikelstamm_WSTgroup_db08_Werkstoffe_Bezeichnung] FOREIGN KEY ([WSTgroup])
REFERENCES [dbo].[db08_Werkstoffe] ([Bezeichnung]) ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[db00_02_Artikelstamm] CHECK CONSTRAINT [FK_db00_02_Artikelstamm_WSTgroup_db08_Werkstoffe_Bezeichnung]
GO
What i need is:
A change of the name of a material in the material table automatically updates the material in my article table
A change of a material in the article table do not change the name of a material in the material table
A change in the article table is only possible if the new material is in the material table
I should not be possible to delete a material while this material is used by an article
I use an MS SQL Server.
I hope i could explain it clearly and someone can help me.
Thanks in advance!
Maybe you should use as reference an univoque id and then avoid the "on update cascade", because in that case you can modify the name of article without concern about the propagation of changes to "childs" tables.
In this way, it is already implied the constraint about changes of material reference in article table.
The halting deleting constraint can be declarated on material table with the clause "ON DELETE NO ACTION"
update after comments..
As said, if you have a problem of manteinance and legacy code I think that you have implement some software solution, else I think that the best solution, if the reference of your "materials" table was not spread into many other tables, it is to back-up all tables that have to be modified, add a new primary key to your material table different of current unique name of material (if not already present), then delete the foreign key pointing to the materials table in the articles table, then update the value in the fk_field of each article with the primary of each material corresponding to the current name valu contained by the fk_field, and then add the new foreign key.

Firebird: Alter "anonymous" foreign key

I need to alter an existing foreign key from "on delete restrict" to "on delete cascade". Unfortunaltey this bug sneaked through Q/A.
In my database I have several forign key relationships that were automatically named (INTEG_1, INTEG_2, ...). The name of the constraint I have to fix is another in a new installation than in an Update from Version 2 and even another than when this Version 2 previously has been updated from Version 1.
As the referencing table only has one foreign key, this statement gives me the name of the constraint:
SELECT RDB$CONSTRAINT_NAME
FROM RDB$RELATION_CONSTRAINTS
where RDB$CONSTRAINT_TYPE = 'FOREIGN KEY'
and RDB$RELATION_NAME = 'MY_TABLE_NAME'
then I can drop and afterwards recreate the foreign key (with a "real" name this time)
alter table MY_TABLE_NAME
drop constraint <result from above>;
alter table MY_TABLE_NAME
add constraint fk_my_table_name_purpose foreign key (other_id)
references other_table(id) on delete cascade;
However, I try to avoid working directly with system tables and I'd like to know whether there is a better / more elegant way to alter my foreign key.
There's no better way, the system tables are the only way to figure out the constraint name.

How to delete rows in associated SQL tables?

I want to delete rows in GlassesColor table that associated with GlassesID in Glasses table.
I want to implement this in stored procedure.The stored procedure gets only one parameter from client side , a CollectionID.
Here are the following tables:
Here is example of tables content:
Any idea how can i implement this?
Thank you in advance!
Manually deleting would be something like this:
delete
from GlassesColor
where GlassesID in (select GlassesID from Glasses where CollectionID = 3)
However, unless this is a one time cleanup, you should start specifying foreign key cascading rules, like the other answers already suggested.
The easiest way is to define FOREIGN KEYS with CASCADE options for DELETE.
http://msdn.microsoft.com/en-us/library/aa933119%28v=sql.80%29.aspx
I see you already have Foreign Keys defined, so you just need to make sure they have CASCADE option for DELETE.
Without stored procedure, use Constrains -> http://www.mssqlcity.com/Articles/General/using_constraints.htm OnDelete Cascade, so when you delete row in Glasses, it will deleted in Glasses color also.
In your foreign key constraint for the GlassesColor join table, have on delete cascade. What that means is when the primary key referenced record is deleted, the corresponding foreign key reference row will also be deleted.
So your GlassesColor table definition would look like this:
create table GlassesColor
(
GlassesId int foreign key references Glasses(GlassesID) on delete cascade,
.....
)
go
I answered something similar for using the INFORMATION_SCHEMA in MSSQL
SQL Server: drop table cascade equivalent?
Here's the easiest way to do it in Microsoft SQL Server: when you create the foreign key constraint, add ON UPDATE CASCADE ON DELETE CASCADE to its definition. I'm assuming you want changes to GlassesID to also propogate. ;-) If not, then you don't need the "ON UPDATE CASCADE".
Example:
ALTER TABLE
[GlassesColor]
WITH CHECK ADD CONSTRAINT
[FK_GlassesColor_GlassesID]
FOREIGN KEY
([glassesID]) REFERENCES [Glasses] ([glassesID])
ON UPDATE CASCADE ON DELETE CASCADE