Delete on cascade for oracle 11g - sql

Is there any ways to perform delete on cascade without locking all the child tables.Also the delete is performed on the key which is not the foreign key in the child tables .The foreign key in the child tables is indexed.Currently the situation is that when the delete is happening the locks on all the child tables which does not allow inserts to occur because of the locks
thanks
sri

As a solution, you can add a field with two possible values "Y"/"N" ("1"/"0", etc) that will show that the record has been deleted already. Then you can build a job (it is ok for long term operation) This process will be working in background, it will be collecting deleted rows and delete them.
Second way, try to run your transaction, and when you see your table got a full lock execute:
ALTER TABLE <<YOUR_LOCKED_TABLE_NAME>> DISABLE TABLE LOCKS
Then your transaction has to finish with the exception ORA-00069, if so, your table is really locked and you have to check if you have B-Tree indexes on your foreign key and you don't use any bitmap indexes.

Related

I Cant delete constraints of table so i can drop it. ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired

I want to delete foreign key constraints in sqldeveloper so i can drop all tables, but it shows me the error in title when i try to do it. someone help?
The table is currently being used; someone (maybe even you, in another session) performed DML on it and didn't commit (nor rollback) so the table is "busy". Once that transaction is done (by either committing or rolling back), you'll be able to drop that foreign key constraint.
Here's an example that shows two sessions that run in parallel; follow the numbers:
user A created the table that has a foreign key constraint to the DEPT table
user B - in another session, obviously - inserted a row into that table
user A tried to drop the constraint but was unable to because the table is still "busy"
user B commits
user A is now able to drop the constraint

Truncating a MS SQL Server Express table with one record takes forever

I am setting up a kind of test database in Microsoft SQL Server Express 2017. I have one main table with 10 columns, which is linked to 6 others, ie its primary key is the foreign key of 6 other tables.
I have populated this main table with just one record.
I need to truncate it - ie delete all the rows but not the table. I tried both truncate table and delete from but both take forever: after 4 minutes the query was still executing! I understand there are keys to check etc, but it's only one record. All the other tables are empty. This doesn't seem right. Any ideas what could be wrong and what I can do to fix it?
In response to the comment
It is probably uncommitted transactions, on your child tables.
You would first be deleting all the child records prior to deleting the parent table record. Is the child tables all empty?.
Do you have any uncommitted transactions in any of those tables? If you do then attempt to kill those sessions by engaging a dba.
A table that has foreign key constraints can't be truncated. Either you drop constraint, truncate table then re-create constraints Or make use of delete cascade. here is a link for same.
TRUNCATE TABLE is a DDL command, it cannot check to see whether the records in the table are being referenced by a record in the child table. In case of DELETE, database is able to make sure that it isn't being referenced by another record.

deleting in tables in a specific order

Hy,my question is how can i use a procedure in which i can delete data from one table and then from the other to avoid the problem with the REFERENTIAL INTEGRITY, I tried it like this
create procedure usp_Testovi_Delete
(
#TestoviID int
)
as
delete from Testovi
where TestoviID=#TestoviID
delete from RezultatiTesta
where TestoviID=#TestoviID
but I need to execute it twice to delete data from both tables , ty for your time and help , I'm using SQL server.
There are a variety of ways to handle this:
Delete from the table with the foreign key first.
Wrap the first delete in a blank exception handler, then delete from the second table, and finally delete from the first table again.
Disable the foreign keys while you perform the delete, and re-enable them afterward.
They each have advantages and disadvantages :-/

TRUNCATE TABLE query unable to execute in SSIS because of foreign key

I have a table Person which contains 2 fields.In my another database i have a Participant Table(also have 2 columns).From Participant Table i have to insert values into Person Table.
but before every insertion,i want truncate the person Table.
I have try it out with linking Execute Sql task to Data flow task.But it is showing error that a Primary Foreign key relation exists there.
If I understand correctly, SSIS has nothing to do with your real problem, which is that you want to truncate a table that is referenced by a foreign key constraint. That question has already been answered: Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?
If a table in sql server has foreign key references then you can't truncate. instead in your execute sql task use delete without a where clause.
delete from person_table
If you are really adamant about truncating the table, you could drop the foreign key constraints, then truncate the table then recreate the foreign key constraints. Providing of course, that the user you are running the package as has the privileges to do so.
Create an "Execute SQl" task and run DELETE FROM person
after this task, run your import.
DELETE FROM will give the same result as TRUNCATE TABLE, but if the table has a foreign key pointing to it, it cant be truncated. You have to use the delete command
You won't be able to delete either unless cascading deletes is turned on (or you delete the child records first). Why is this a problem you ask, why can't I do what I want to do? Because if you do then you may lose the integrity of the data. Suppose I have records in table2 which relate to records in table 1. Suppose further that table1 has an autogenerated id. If I could truncate that table, then I leave those records i ntable 2 hanging out without any record to reference them, they have become orphaned. Well but I'm putting the data back in you say. But then they will have new id numbers and you will still lose the relatinoship tothe related data.
Can you drop the FK and truncate and insert and recreate the FK. Yes you can but it is a poor practice and you should not unless you are also recreating those related records.
The best practice is to use a MERGE statement to update or insert depending on what you need.
In SSIS Transfer SQL Server Objects Task Set Property DeleteFirst to TRUE

What is the equivalent effect to Truncating a table, when the table is referenced by a foreign-key

Straight out of the MSDN docs for Sql Server 2005:
You cannot use TRUNCATE TABLE on tables that:
Are referenced by a FOREIGN KEY constraint.
Participate in an indexed view.
Are published by using transactional replication or merge replication.
I want the effect of a TRUNCATE (specifically the fact that it resets IDENTITY type columns), but I can't use one in my case because my table is referenced by a foreign-key elsewhere in the database.
Update: This is for a testing configuration where I am clearing out the referencing table as well, so foreign-key integrity is a non-issue.
What other ways are there to get around this?
You can delete all the rows and then do a DBCC CHECKIDENT (Tablename, RESEED, 0) to reset the identity seed
But again DELETE is fully logged while TRUNCATE is minimally logged and will be many times faster
Another option is to drop the foreign key constrains then do a truncate and then recreating the foreign key constraints
The fact that it is refernced by a foreign key is the clue you need to not truncate a table or you would be creating orphaned records. This is why truncate table is not allowed if a foreign key exists.
The correct process is to first delete the refernced records, if any, then drop the FK constraint, then truncate the table then reinstate the fk constraint. If you skip step one, you will create a data integrity nightmare where the record pointing to oldid 100 is not pointing to the new record that happens to get assigned to 100 and it isn;t the record it should match to.
You can drop the foreign key, truncate the table, and then recreate the foreign key.
You will need to drop the constraint, trunc the table, and then add the constraint back. However, you should be very careful with this. If there are rows in the table that you are dropping the FK reference to you will not be able to add it until those rows are deleted or the FK column in the other table is clear.