Deleting the record from a table referenced by 60 foreign keys - sql-server-2012

Table takes a long time to delete one record. It is referenced by 60 foreign key tables and when all the child records are deleted, I started to delete one record but it takes one minute.

Related

Not able to delete records from table(order) having its id column as foreign key in multiple order tables in postgres

I have an "orders" table
with its id as foreign key in below tables
dispatch_details
order_histories
sales_return_details
promotion_orders
While creating the table we haven't added ON DELETE CASCADE.
so I have deleted all the required records from referenced tables i.e. dispatch_details,
order_histories, sales_return_details and promotion_orders.
It's taking way too much time still not able to delete records from orders even though I am not getting any error. It is only processing but doesn't complete.
I have further added index for all the referenced table.
create index idx_dispatch_details_order_id on dispatch_details(order_id);
create index idx_order_histories_order_id on order_histories(order_id);
create index idx_sales_return_details_order_id on sales_return_details(order_id);
create index idx_promotion_orders_order_id on promotion_orders(order_id);
Still, I am not able to delete the records.
Is there any way to delete the records table from orders efficiently? Currently, I am not able to delete even single records from orders.
I have further added an index to all table calls and daily_order_info table. Also, the orders table has parent_order_id FK reference to itself. Added index for that column as well. Basically, for all the tables referenced: created an index and that solved my issue.

SQL Delete Rows with Constraints

I am doing some Exercises and for that I want to delete a Row which is referenced with a Primary/Foreign Key.
Is it possible to delete those Rows without deleting the ones which it has a Relationship with? (like On Delete Cascade)
I also know that I can Disable the constraints, however I want to try deleting without disabling the Constraints.
A row can always be deleted unless its primary key is referenced as a foreign key in a row from another table. In this case, attempting to delete rows will result in an error when the primary/foreign key relationship against data is encountered. The ON DELETE CASCADE option can be used to work around this and delete records from the child table when rows are removed from the parent table.
If you only want to delete the rows that do not have a relationship, then you can exclude the rows that do have a relationship from the delete operation using NOT EXISTS:
DELETE FROM table1 a
WHERE NOT EXISTS (
SELECT FROM table2 b
WHERE b.table1Id = a.id
)

How to check if foreign key constraint fails when deleting record

I have created a database in SQL Server and front end is PHP - CodeIgniter. In the database I have created multiple foreign keys with other tables. Now when the user tries to delete the record, instead of really deleting I want to flag the record as deleted = 1, this should only be done when there will be no reference records are available in child table. Below are example tables:
Parent_Table
Id INT(PK), Name Varchar, deleted INT
Child_Table
Id INT(PK), FK_Parent_Table_ID INT, address varchar, deleted INT
Above is just example of my tables. Now whenever a user tries to delete a record from the parent table foreign key will check for constraint and then delete the record, here instead of actual deletion I want it flag as deleted = 1.
I have tried using transaction->start and transaction->complete so if foreign key fails the transaction gets aborted but here the problem is if the foreign key not failing then the rollback will occur and in that case the PRIMARY KEY of the record will be changed that should not be done.
So, I want a way to check the foreign key conflict before transaction starts without actual deletion of the record
To implement what you are asking, just check for the existence of a record in the child table e.g.
declare #RecordToDelete int = 123;
-- Delete the record if no child records exist
delete
from Parent_Table
where id = #RecordToDelete
and not exists (select 1 from Child_Table where FK_Parent_Table_ID = #RecordToDelete);
-- Flag the record as deleted if child records exist
update Parent_Table set
Deleted = 1
where id = #RecordToDelete
and exists (select 1 from Child_Table where FK_Parent_Table_ID = #RecordToDelete);
Depending on whether you really need to keep the record, because you could always create your foreign keys with a cascade delete.
With 15+ child tables I would seriously consider just always flagging the record as deleted and never bothering to actually delete those without child records. A few extra records is unlikely to make much difference to your database.
Actually in my experience child tables fall into 2 categories:
Those that can automatically be deleted using a cascade delete
Those that should prevent us from deleting the parent record
If this is the case the checks required should become more manageable.
Also for these situations I recommend encapsulating the delete logic within a stored procedure in order to keep it all in one place, and be easy to modify if the database schemes changes in future.
Note: Personally I would make the Deleted column a bit rather than an int as it more accurately reflects the intention.

How to find the total number of orphan records in a schema in oracle. Result should be displayed as Child_table,Parent_table, No. of orphan records

How to find the total number of orphan records in a schema in oracle. Result should be displayed as Child_table,Parent_table, No. of orphan records
How do would the database know there is an orphan record?
If you have foreign key constraints set up on the tables then you should not have any orphan records unless you are allowing NULL values in the foreign key or using ON DELETE SET NULL - in which case you just need to go through every foreign key in the data dictionary and count the NULL values in the foreign key columns of those tables.

How to get a count of rows (across tables) dependent on a particular column which is referenced as a foreign key in Postgres

I want to delete a particular row in a table. the id column of that row is used as a foreign key by many tables. How can I get a count of such references to that particular row value of the id column across all tables?
I dont want to do a delete cascade or set null value. So, I want to know if the row to be deleted has something depending upon it or not and if there is nothing depending on that row go ahead with the delete.
table employee has a row with id_employee = 1001, which i want to delete.
id_employee = 1001 occurs in 3 other tables which reference it using a foreign key constraint (checking manually).
How to get the count of same by a select query?