Deleting Records in Joined Multiple Tables in a DataBase - sqlite

How can I delete a particular record in a DataBase where 6 tables are joined among each other.

It is possible to create the FOREIGN KEY (with "ON DELETE CASCADE" action) Constraint(s) that will automatically remove records from the corresponding referenced tables. Take a look at the “FOREIGN KEY Constraints” Books Online / MSDN topic for more information.

Do it within a transaction. Referential integrity isn't checked until the transaction is closed:
begin;
delete from table1 where ...;
delete from table2 where ...;
delete from table3 where ...;
commit;

Related

Dynamo DB, delete multiple records using Global Secondary Index(GSI) with _id (Partition key)

Is it possible to delete multiple records from a Dynamo table using GSI _id (Partition key), rather than querying first to get the keys and deleting one by one, can someone please suggest what I should do? thanks.
Two reasons you cannot:
You cannot modify GSIs directly, only the base table.
You cannot delete using a query constraint even on the base table, only by enumerating the primary keys of the items to delete.

Is there any way to force SQLite constrains checks?

For example, let say DB has foreign key A.b_id -> B.id with SET NULL on delete.
If record with some B.id get deleted, all b_id references will be set to NULL.
But if A already contains record where A.b_id has value that is not in B.id (it was inserted without foreign keys support), is there a way to force SQLite DB check foreign keys and set to NULL such data?
In fact, in first place I'm solving an DB upgrading task.
On start app checks if internal DB (resource) has higher version than user DB.
If so it backups user DB, copies internal empty DB to user storage. Than turns off foreign keys support and fills new DB with data from backup, inserting automatically in loop table by table for all columns with same name. Turns on foreign keys support back.
Everything works fine, but if in some table in old DB there is no foreign key constrain previously, while new DB has one, the data will be inserted as is and link can point nowhere (possibly wrong links is unavoidable and not related to question).
Yes, I understand a way to insert without turning off foreign keys support, but it would need knowledge of tables dependencies order that I would like to avoid.
Thanks for any help in advance!
Although I don't know of a way that automatically will set to NULL all orphaned values of a column in a table that (should) reference another column in another table, there is a way to get a report of all these cases and then act accordingly.
This is the PRAGMA statement foreign_key_check:
PRAGMA schema.foreign_key_check;
or for a single table check:
PRAGMA schema.foreign_key_check(table-name);
From the documenation:
The foreign_key_check pragma checks the database, or the table called
"table-name", for foreign key constraints that are violated. The
foreign_key_check pragma returns one row output for each foreign key
violation. There are four columns in each result row. The first column
is the name of the table that contains the REFERENCES clause. The
second column is the rowid of the row that contains the invalid
REFERENCES clause, or NULL if the child table is a WITHOUT ROWID
table. The third column is the name of the table that is referred to.
The fourth column is the index of the specific foreign key constraint
that failed. The fourth column in the output of the foreign_key_check
pragma is the same integer as the first column in the output of the
foreign_key_list pragma. When a "table-name" is specified, the only
foreign key constraints checked are those created by REFERENCES
clauses in the CREATE TABLE statement for table-name.
Check a simplified demo of the way to use this PRAGMA statement, or its function counterpart pragma_foreign_key_check().
You can get a list of the rowids of all the problematic rows of each table.
In your case, you can execute an UPDATE statement that will set to NULL all the orphaned b_ids:
UPDATE A
SET b_id = NULL
WHERE rowid IN (SELECT rowid FROM pragma_foreign_key_check() WHERE "table" = 'A')
This also works in later versions of SQLite:
UPDATE A
SET b_id = NULL
WHERE rowid IN (SELECT rowid FROM pragma_foreign_key_check('A'))
but it does not seem to work up to SQLite 3.27.0

deleting values from foreign key tables in SSMS

Let's say I have tbl1, tbl2, and tbl3, this is what they look like
tbl1
UnitID PK
SomeField varchar
SomeField2 varchar
tbl2
UnitID PK
ServiceID PK
SomeField3 varchar
tbl3
UnitID PK
ChangeID PK
SomeField4 varchar
SO I have three tables in my simple app. Due to some constraints, the data needs to be split up in multiple tables
The user will go to enter a UnitID values in tbl1, then they will add additional data and that data will be stored in tbl2 and tbl3 with the same UnitID and their own corresponding PK's (ServiceID, ChangeID).
WHat I'm looking to see if it can be done is if i delete a value from tbl1, say UnitID=2, am I able to automatically delete all corresponding values from tbl2 and tbl3 with that same UnitID? Or do I have to basically have a delete statement for each table?
I found a doc that is referring to delete foreign key relationships but it shows that it is starting SQL 2016, I'm working in SSMS 2012.
Is something like this possible? The reason why I'm asking is because this is a very simplified view of my tables, I will actually have about 20 tables, and there might be a time where I need to delete that UnitID from every single table in my app. I'm trying to make the code as efficient as possible.
The app is coded with Asp.Net, and VB.NET
I understand that I can simply do
Delete * from tbl1 where UnitID=2
Delete * from tbl2 where UnitID=2
but this seems tedious. Any help will be greatly appreciated. Thanks.
Drop your existing foreign key constraint if you have already created your table relationships and you can add new foreign key constraint with cascade delete.
ALTER TABLE child_table
ADD CONSTRAINT fk_name
FOREIGN KEY (child_col1, child_col2, ... child_col_n)
REFERENCES parent_table (parent_col1, parent_col2, ... parent_col_n)
ON DELETE CASCADE;
If you are going to create new tables then you can have foreign key constraint with Cascade delete.
CREATE TABLE child_table
( parent_col1 INT PRIMARY KEY,
parent_col1 INT NOT NULL,
CONSTRAINT fk_name
FOREIGN KEY (child_col1, child_col2, ... child_col_n)
REFERENCES products (parent_col1, parent_col2, ... parent_col_n)
ON DELETE CASCADE
);

Deleting rows from two tables using inner join SQLITE

How do you delete a row from two separate tables? I thought it would be possible to do this using an inner join
DELETE a.*, b.* FROM Holiday INNER JOIN Accommodation b on a.LocationID = b.LocationID
Here i try to delete by matching the primary key location in the first table to the location id in the second table. I get an SQL Exception "sqlException near a"
Im doing this in SQLITE, java
In SQLite, one DELETE command deletes from only one table.
Your query, as written, doesn't actually restrict the records to be deleted, so if you really want to delete all records, you would use this:
DELETE FROM Holiday;
DELETE FROM Accommodation;
If you want to delete one record in the master table and all corresponding records in the child table, you just filter by that key value:
DELETE FROM Holiday WHERE LocationID = 1;
DELETE FROM Accommodation WHERE LocationID = 1;

SQLite how to delete all connected rows from other tables?

i have the following problem. I have three tables, TABLE_1, TABLE_2 and TABLE_3.
Between TABLE_1 and TABLE_2 is a m:n relationship so theres a connecting table
lets call it TABLE_1_2.
Between TABLE_2 and TABLE_3 is a m:n relationship so theres a connecting table
lets call it TABLE_2_3.
If i delete now a row from TABLE_1 i want that all rows from the other tables connecte also will be deleted.
How can i handle this? i read that sqlite doesnt supports joins for delete statements.
In MS SQL this can be made via altering the table1 and corresponding column by adding ON DELETE CASCADE CONSTRAINT on this column. But in SQLite there is no ALTER COLUMN and ALTER TABLE is also much less functional than in MS SQL. That's why you'll probably have to
Rename the table1 to a table3
Create a new table with ON DELETE CASCADE CONSTRAINT and all other constraints you've already used.
Copy the content of the old table to the new one.
Remove the old table
See also the similar topic.

Resources