Delete on cascade for two foreign keys - sql

I have the following problem, I have a table that contains two foreign keys from an unique column like this:
Note: id_maquina_camino is "unique" and both of tables are needed as are shown.
If I add the foreign keys everything go fine, but when I try to add delete on cascade I have an error:
Msg 1785, Level 16, State 0, Line 11
Introducing FOREIGN KEY constraint 'FK__camino__destino__300424B4' on table 'camino' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Msg 1750, Level 16, State 0, Line 11
Could not create constraint. See previous errors.
The code:
ALTER TEST.dbo.camino
ADD FOREIGN KEY (origen)
REFERENCES maquina (id_maquina_camino)
ON DELETE CASCADE;
ALTER TEST.dbo.camino
ADD FOREIGN KEY (origen)
REFERENCES maquina (id_maquina_camino)
ON UPDATE CASCADE;
ALTER TEST.dbo.camino
ADD FOREIGN KEY (destino)
REFERENCES maquina (id_maquina_camino)
ON DELETE CASCADE;
ALTER TEST.dbo.camino
ADD FOREIGN KEY (destino)
REFERENCES maquina (id_maquina_camino)
ON UPDATE CASCADE;

Related

When creating a foreign key in firebird, the error violation of the FOREIGN KEY restriction "PK_ROLES1" in the "roles" table comes out?

I am trying to create a foreign key in the user_roles table.It has a user_id and a roles_id. I created a foreign key for user_id. And for roles_id it is not possible to create. I work at ibexpert.
An error comes out: violation of FOREIGN KEY constraint "PK_RULES 1" on table "rules".I will be glad of any help.
alter table "users_roles"
add constraint FK_USERS_ROLES_2
foreign key (ROLE_ID)
references "roles"(ID)
on delete CASCADE
on update CASCADE
using index FK_USERS_ROLES_2

How to make multiple referential integrity conditions in SQL

I have a SQL statement that should add me some constraints. In that statement I want to set on two constraints two referential integrity conditions.
Here is my statement:
ALTER TABLE Vertraege
ADD CONSTRAINT FK_Kunde_Vertraege FOREIGN KEY(K_ID)
REFERENCES Kunde(K_ID),
CONSTRAINT FK_Standort_Vertraege FOREIGN KEY(S_ID)
REFERENCES Standort(S_ID) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT FK_Mitarbeiter_Vertraege FOREIGN KEY(M_ID)
REFERENCES Mitarbeiter(M_ID) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT FK_Dienstleistung_Vertraege FOREIGN KEY(D_ID)
REFERENCES Dienstleistung(D_ID),
CONSTRAINT FK_Compliance_Vertraege FOREIGN KEY(C_ID)
REFERENCES Compliance(C_ID);
So the problem ist, that I get an error because of this.
Notification 1785, level 16, status 0, line 28
Introducing FOREIGN KEY constraint 'FK_Mitarbeiter_Vertraege' on table 'vertreage' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Notification 1750, level 16, status 1, line 28
Could not create constraint or index. See previous errors.
Is it possible to set multiple referential integrity conditions and if not, how can I make multiple of them in SQL? Something like ALTER CONSTRAINT...
EDIT
Brian asked for this. On my table mitarbeiter I have following constraints. The strange thing is, they work
ALTER TABLE Mitarbeiter
ADD CONSTRAINT FK_Adresse_Mitarbeiter FOREIGN KEY(AD_ID)
REFERENCES Adresse(AD_ID) ON UPDATE CASCADE,
CONSTRAINT FK_Standort_Mitarbeiter FOREIGN KEY(S_ID)
REFERENCES Standort(S_ID) ON UPDATE CASCADE,
CONSTRAINT FK_Abteilung_Mitarbeiter FOREIGN KEY(AB_ID)
REFERENCES Abteilung(AB_ID) ON UPDATE CASCADE;
The error message mentions "multiple cascade paths", and with the additional constraints that you added to your question, my first suspicion that there is an interaction between the following items (edited from your initial post):
ALTER TABLE Vertraege
CONSTRAINT FK_Standort_Vertraege FOREIGN KEY(S_ID)
REFERENCES Standort(S_ID) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT FK_Mitarbeiter_Vertraege FOREIGN KEY(M_ID)
REFERENCES Mitarbeiter(M_ID) ON UPDATE CASCADE ON DELETE CASCADE,
and
ALTER TABLE Mitarbeiter
CONSTRAINT FK_Standort_Mitarbeiter FOREIGN KEY(S_ID)
REFERENCES Standort(S_ID) ON UPDATE CASCADE,
So you've got a constraint that goes directly from Vertraege to Standort, and another that goes from Vertraege through Mitarbeiter to Standort. My first suggestion would be to remove FK_Standort_Vertraege and see if that fixes your issue.
This is a guess, since the question does not include all the related tables.
You are trying to create a foreign key constraint from the table Vertraege to the table Mitarbeiter. It seems, however, there's already another [reverse] foreign key constraint (not shown) from the table Mitarbeiter to the table Vertraege. I can't be sure since you don't include the definition of the table Mitarbeiter.
SQL Server considers this a "cyclic relationship". This is all "legal", however. Nothing wrong with it. Well... as long as some of them are nullable, or they can be deferrable (but SQL Server does not implement the latter). The cyclic relationship may not only involve two tables, but also three or more. It's not clear from the question how many tables are involved, however.
The problem stems from the "cascade delete/update" rules you want to specify. This rules can possibly lead to massive data deletion or updating by a simple innocuous DELETE or UPDATE, such as:
delete from Vertraege where ID = 123
This statement seems to be deleting a single row. However, your cascade deletion rule can end up deleting thousands of rows from multiple tables at once without further confirmation. Is that what you want?
In any case, SQL Server doesn't like those cyclic cascade deletes/updates, since it considers them [justifiable] dangerous, and decides not to allow them.

cannot alter table to add a foreign key

alter table course_relation
add foreign key (offered_By)
references department_relation(dept_cd)
alter table department_relation
add foreign key(hod)
references staff_relation(staff_no)
alter table staff_relation
add foreign key (dept_cd)
references department_relation(dept_cd)
//the third para doesn't work but the first two works fine
error message:
Msg 547, Level 16, State 0, Line 10 The ALTER TABLE statement
conflicted with the FOREIGN KEY constraint
"FK__staff_rela__dept__1BC821DD". The conflict occurred in database
"Royal_Poly_DB", table "dbo.Department_Relation", column 'Dept_Cd'.
You can't create circular foreign key references.
In this Statement
alter table department_relation
add foreign key(hod)
references staff_relation(staff_no)
In this alter query you create foreign key reference in 'department_relation' from 'staff_relation' table.
alter table staff_relation
add foreign key (dept_cd)
references department_relation(dept_cd)
In this alter query you create foreign key reference in 'staff_relation' from 'department_relation' table.
This is circular dependency of foreign key and it is allowed.

Database Diagram not showing table relation when new foreign key reference is added in SSMS 20114

I have three tables as follows -
But when I add the foreign key reference, the relation is ( a straight line in the Database Diagram) not shown.
Following is the reference I wrote.
ALTER TABLE [dbo].EmployeeDesignation
ADD CONSTRAINT FK_EmployeeDesignation_Employee FOREIGN KEY (EmployeeId)
REFERENCES Employee (EmployeeId)
ON DELETE CASCADE
ON UPDATE CASCADE
;
ALTER TABLE [dbo].[EmployeeDesignation]
ADD CONSTRAINT FK_EmployeeDesignation_Designation FOREIGN KEY (DesignationId)
REFERENCES Designation (DesignationId)
ON DELETE CASCADE
ON UPDATE CASCADE
;
In addition, When I add another two tables (Department and EmployeeDepartment) I can see the relation in the diagram
The code for the reference is as follows --
ALTER TABLE [dbo].EmployeeDepartment
ADD CONSTRAINT FK_EmployeeDepartment_Department FOREIGN KEY (DepartmentId)
REFERENCES Department (DepartmentId)
ON DELETE CASCADE
ON UPDATE CASCADE
;
ALTER TABLE [dbo].EmployeeDepartment
ADD CONSTRAINT FK_EmployeeDepartment_Employee FOREIGN KEY (EmployeeId)
REFERENCES Employee (EmployeeId)
ON DELETE CASCADE
ON UPDATE CASCADE
;
What might be the reason That the later one showing relation line while the previous one is not showing?
Am I missing something?
Thanks !
Try closing SSMS and then reopening it again. Seems like the cache used by SSMS for some of functionality is not refreshed even upon closing and re-opening the database connection. check https://stackoverflow.com/a/4316415/364084

Add Relationship

I am trying to add a foreign key into an existing table, this is what I have so far:
ALTER TABLE INVOICE_ITEM
ADD CONSTRAINT Invoice_ItemFK FOREIGN KEY (ProdID);
I get this error:
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.
Any clue how to circumvent this?
You need a REFERENCES clause to list which table you want the foreign key to point to. For example, assuming ProdID is the primary key of the PRODUCTS table:
ALTER TABLE INVOICE_ITEM
ADD CONSTRAINT Invoice_ItemFK
FOREIGN KEY (ProdID)
REFERENCES PRODUCTS (ProdID);
You need to specify the REFERENCES clause. I have guessed at reference here - but you get the idea. The actual issue was the name of the Foreign Key. It should start with FK_.
ALTER TABLE INVOICE_ITEM
ADD CONSTRAINT FK_Invoice_Item
FOREIGN KEY (ProdID)
REFERENCES Prod(ProdID);