SQLite multiple constraints on 1 FK - sql

I have a table in SQLite with specified FK as follows:
CONSTRAINT "model" FOREIGN KEY("category_id") REFERENCES "category"("id") deferrable initially deferred and I need to add ON DELETE CASCADE. I know that in SQLite you can't add constraint with ALTER so how can I combine these 2 constraints when creating a new table?

In the CREATE statement of the new table use:
CONSTRAINT model FOREIGN KEY(category_id) REFERENCES category(id)
ON DELETE CASCADE
DEFERRABLE INITIALLY DEFERRED
See the demo.

Related

create a constraint between tables

I am new to sql and have not have much experience in it
Trying to create a relationship between two tables using a constraint.
adding a unique constraint and relationship for force the userid and consumerid between table1 and table2
any sample will help me
Thanks
You can use this FOREIGN KEY (blabla) REFERENCES secondTable(blabla) but only if the table is not created yet. Write it inside your CREATE TABLE query.
But if it already exists you can add it using ALTER TABLE tablename ADD CONSTRAINT `FK_myKey` FOREIGN KEY (`blabla`) REFERENCES `secondTable` (`blabla`) ON DELETE CASCADE ON UPDATE CASCADE;

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.

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

Can't add foreign key to a table

I've checked that none of the two tables has any foreign keys. I've checked that they both have Id of type uniqueidentifier. I runt the script and get this eror.
ALTER TABLE [dbo].[Records]
ADD CONSTRAINT [FK_dbo.Records_dbo.Users_UserId]
FOREIGN KEY ([UserId])
REFERENCES [dbo].[Users] ([Id])
--ON DELETE CASCADE
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Records_dbo.Users_UserId". The conflict occurred in database "MyDb", table "dbo.Users", column 'Id'.
Not sure how to troubleshoot it... Tested both with and without the cascade...
It must be something to do with the existing data. There must be some records conflicting the foreign key creation. Try to create the key on an empty schema to see if it works. Use WITH NOCHECK to not to check existing rows at the time of foreign key creation, if you need that data.
Most likely you have a UserId in the Records table that does not exist in the Users table. This will certainly happen if you've been using 0 or some other value as a "default" value.

tsql script to add delete cascade to existing tables

is there a script that can be used to enable cascaded deletion for existing tables.
Thanks.
ALTER TABLE [wm].[TABLE_NAME] WITH NOCHECK ADD CONSTRAINT [FK_TABLE_NAME_PARENT_TABLE_NAME] FOREIGN KEY([FOREIGN_KEY])
REFERENCES [wm].[PARENT_TABLE_NAME] ([PRIVATE_KEY])
ON DELETE CASCADE
GO
TABLE_NAME: name of the table where the children are stored.
PARENT_TABLE_NAME: name of the table where the parents are stored.
This placeholders can be equal
FK_TABLE_NAME_PARENT_TABLE_NAME: just name for the constraint
FOREIGN_KEY: field in the child table for the connection with the parents, for example - ParentID
PRIMARY_KEY: field in the parents table, for example - ID
ALTER TABLE [wm].[Thumbs] WITH NOCHECK ADD CONSTRAINT [FK_Thumbs_Documents] FOREIGN KEY([DocID])
REFERENCES [wm].[Documents] ([ID])
ON DELETE CASCADE
GO