cascade foreign key on multiple columns - sql-server

I am trying to create UPDATE CASCADE Foreign key reference to a table using below scripts.I am getting error saying it may cause cycles or multiple cascade paths.
ALTER TABLE STA_STATIONARY_REQUEST_MASTER WITH CHECK ADD CONSTRAINT [FK_STA_STATIONARY_REQUEST_MASTER_REQUESTOR_ID] FOREIGN KEY([REQUESTOR_ID])
REFERENCES STA_EMPLOYEE_MASTER ([EMPLOYEE_ID])
ALTER TABLE STA_STATIONARY_REQUEST_MASTER WITH CHECK ADD CONSTRAINT [FK_STA_STATIONARY_REQUEST_MASTER_CREATED_BY] FOREIGN KEY([CREATED_BY])
REFERENCES STA_EMPLOYEE_MASTER ([EMPLOYEE_ID])
But when I am creating using below scripts it is created successfully.But still i am not able to update the employee id.
ALTER TABLE STA_STATIONARY_REQUEST_MASTER ADD CONSTRAINT
FK_STA_STATIONARY_REQUEST_MASTER_EMP1
FOREIGN KEY (REQUESTOR_ID) REFERENCES STA_EMPLOYEE_MASTER(EMPLOYEE_ID),
FOREIGN KEY (CREATED_BY) REFERENCES STA_EMPLOYEE_MASTER (EMPLOYEE_ID)
ON UPDATE CASCADE
Please help.

Related

In the FK of the two tables, error to cycles or multiple cascade paths

enter image description here
The three tables have a FK relationship.
Post.PostBy is NOT NULL, Post.Category_id is Nullable.
An error has occurred.
Introducing FOREIGN KEY constraint 'FK_Post_Category' on table 'Post' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
ALTER TABLE [Category] ADD CONSTRAINT [FK_Category_CreatedBy] FOREIGN KEY ([CreatedBy])
REFERENCES [Account]([_id])
ON DELETE CASCADE
ALTER TABLE [Post] ADD CONSTRAINT [FK_Post_PostBy] FOREIGN KEY ([PostBy])
REFERENCES [Account]([_id])
ON DELETE CASCADE
ALTER TABLE [Post] ADD CONSTRAINT [FK_Post_Category_id] FOREIGN KEY ([Category_id])
REFERENCES [Category]([_id])
ON DELETE SET NULL
I don't understand.
PostBy is NOT NULL, so when the referenced row is deleted, we want it to be deleted along with it.
Category_id is NULLABLE, so if the reference row is deleted, set a NULL value.
Category and Post refer to Account, but I think it should work because they have different Account values.

Can We Create On Delete Cascade on exiting foreign Key

Can we create On Delete Cascade on an existing Foreign Key?
I know we can create from SQL design of table going to relationships But I want to do this with the script query?
I also not want to drop foreign key first and then create the foreign key with on delete cascade, I not want this.
There is no ALTER CONSTRAINT syntax. If you have created a CONSTRAINT and you need to change it, you need to DROP the CONSTRAINT and then CREATE it again; there is no work around for this.
In this case, using the definition in the comments, you would need to do:
ALTER TABLE [dbo].[HL7_Rx_Resident_NonVerified] DROP CONSTRAINT [FK_HL7_Rx_Resident_NonVerified];
GO
ALTER TABLE [dbo].[HL7_Rx_Resident_NonVerified] WITH CHECK
ADD CONSTRAINT [FK_HL7_Rx_Resident_NonVerified]
FOREIGN KEY ([MedicationId])
REFERENCES [dbo].[RxOrder_Resident_NonVerified] ([Id]) ON DELETE CASCADE;
GO

Cannot add cascade on update on two column of table referencing to same table same column

I'm using SQL Server.
I have one table named PROGRAM_MASTER which has two columns created_by and updated_by which are referencing to USERS table on column id.
Now I want to define cascade on update option to both these columns using below SQL command.
alter table program_master add constraint program_master_created_by_foreign foreign key (created_by) references users (id) on update cascade
alter table program_master add constraint program_master_upated_by_foreign foreign key (updated_by) references users (id) on update cascade
But foreign key constraint gets added to created_by, but while creating one for updated_by I get following error.
SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL
Server]Introducing FOREIGN KEY constraint
'program_master_updated_by_foreign' on table 'program_master' may
cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or
ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
How should I resolve this error?

sqlplus error creating table with multiple foreign keys

So I am trying to create a table with multiple foreign keys however it keeps saying missing or invalid option. I have successfully created all the other tables that the keys reference without issue. What's strange is that I have found that the table creates if I remove either foreign key, but as soon as I have two foreign keys it gives me the error above. Any help is appreciated! :)
create table Class(
Class# int(5),
Class_Size int(30),
Module# char(5),
Student# char(4),
Constraint Class_PK primary key(Class#),
Constraint Class_Module_FK foreign key(Module#) references Modules(Module#) on delete cascade ),
Constraint Class_Student_FK foreign key(Student#) references Students(Student#) on delete cascade );
Looks like you have an extra ) at the end of second last line.
This should work
create table Class(
Class# int(5),
Class_Size int(30),
Module# char(5),
Student# char(4),
Constraint Class_PK primary key(Class#),
Constraint Class_Module_FK foreign key(Module#) references Modules(Module#) on delete cascade,
Constraint Class_Student_FK foreign key(Student#) references Students(Student#) on delete cascade );

Delete foreign key with primary key reference

I tried many forums but was not satisfied - I have a table that has a primary key and foreign key relation.
I have to delete table rows with primary key so I need to remove the constraints before deleting.
I used:
delete from [docd_metadata].[docd_metadata].[STATEMENT_IMAGES]
where [statement_image_id]= 05291520275
I got error:
The DELETE statement conflicted with the REFERENCE constraint "fk_stmnt_image_StmntImageId". The conflict occurred in database "docd_metadata", table "docd_metadata.STATEMENT_CAMPAIGN", column 'STATEMENT_IMAGE_ID'.
so I tried :
ALTER TABLE[docd_metadata].[docd_metadata].[STATEMENT_IMAGES]
DROP CONSTRAINT [fk_stmnt_image_StmntImageId]
Now I am getting :
Constraint 'fk_stmnt_image_StmntImageId' does not belong to table 'STATEMENT_IMAGES'
Schema:
Also:
Any suggestion please?
If you really closely read the error message, it's clear that the FK constraint is on table docd_metadata.STATEMENT_CAMPAIGN and not on STATEMENT_IMAGES - so therefore, you must use this SQL to drop the FK constraint:
ALTER TABLE [docd_metadata].[STATEMENT_CAMPAIGN]
DROP CONSTRAINT [fk_stmnt_image_StmntImageId]
The FK goes from table [docd_metadata].[STATEMENT_CAMPAIGN] (column STATEMENT_IMAGE_ID) to [docd_metadata].[STATEMENT_IMAGES] - one table has the primary key, which another table references via its foreign key.

Resources