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 - sql

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.

Related

Delete based on primary key from associative table - Error

I'm trying to delete 1 record based on primary key from (3) tables.
Here is the statement that I'm using
DELETE FROM CUSTOMER
WHERE EXISTS
( SELECT MERCHANTNAME
FROM CREDITCARD
WHERE MERCHANTNAME = 'VISA');
Deleting record of a customer with a VISA from customer table.
Here is the error that I'm getting
ORA-02292: integrity constraint
(PLATINUMAUTOGROUP.CDRIVERLICENSENUM_FK) violated - child record found
I'm guessing CDRIVERLICENSENUM is the foreign key in the 3rd table that I have. How do I go about this? Is it possible to delete 1 record from 3 tables in 1 statement?
the three tables are
customer / customer_creditcard / creditcard
You need to delete the records in the foreing tables before deleting the record in the primary table.
But perhaps your delete command in the CUSTOMER table might be wrong, because if the EXISTS command return true, all records will be deleted from the CUSTOMER table. Check if this is the result you expect.

Insert into tables with primary and foreign key at same time

Very new to SQL and have spent a day on this already.
Here are my two tables:
Centre(cid, name, location, nurse_supervisor)
Nurse(nid, name, centre_id, certificate)
I have a big problem. The (nurse_supervisor) in Centre is a foreign key to Nurse (nid).
The (centre_id) in Nurse is a foreign key to (Centre cid).
I can't figure out how to populate these tables. I have tried:
INSERT ALL, which produces "A foreign key value has no matching primary key value"
I have tried removing the foreign key constraints and adding them after populating the tables but when I do that it says I can't add a constraint to tables with preexisting data.
I tried removing NOT NULL - but realized that was silly as the constraints will be enforced anyways.
Everything I look through says populate the parent table first and then the child, but these tables are linked to each other.
I am using SQL developer.
This is a poor schema design, but one way to get around it would be to:
Make both centre_id and nurse_supervisor columns NULL in the two table definitions
Insert all rows into both tables, but with NULL for those two columns
Update centre_id to the correct value for each row in the Nurse table
Update nurse_supervisor to the correct value for each row in the Centre table

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?

How to delete a row ONLY in parent table, which is referenced by a Foregin Key from the child table

I want to delete a row/tuple from a parent table, but it is throwing an error message because it has a FOREIGN KEY reference in its child table.
However, in my case I want to delete the record only from the parent table and maintain the data in the child table.
Is it possible to achieve this?
I know the usage of ON DELETE CASCADE, but I want to know if there is a solution for the secenario I described?
It is possible with some agreements in your data. To maintain child table data you'll have to do ON DELETE SET NULL. This will leave data, but set FK to NULL value (in child table). And that is because of data-integrity: while you can keep your data, your FK can not refer to non-existent row of parent table in terms of enforcing FK constraint. Thus, it will be set to NULL by this.
If you want to "save" value of FK - then you definitely should not use FK at all because such behavior violates what FK is. So then just don't use that constraint, but be aware of possible integrity fails.
The point of a foreign key constraint is to prevent orphan records in the child table. So, no, it's not possible to do that, unless you drop the foreign key relationship.
If you rely on 'ON DELETE CASCADE', then deleting the parent record will result in all the corresponding children to be deleted.
If you want to delete the parent, but keep the children, you need to drop the foreign key constraint, or set the constraint to be 'ON DELETE SET NULL'. If you set 'ON DELETE SET NULL', then when you delete the parent record, the child records will remain, but the foreign key column value will be set to NULL.
delete a row ONLY in parent table, which is referenced by a Foregin Key from the child table
If Multiple table has been mapped in one table in that case all foreign key i.e :-
$table->integer('customer_id')->unsigned()->nullable();
$table->foreign('customer_id')->references('id')
->on('customers')->onDelete(`SET NULL`);

Database table id-key Null value and referential integrity

I'm learning databases, using SQLce. Got some problems, with this error:
A foreign key value cannot be inserted because a corresponding primary key value does not exist.
How does the integrity and acceptance of data work when attempting to save a data row that does not have specified one foreign key. Isn't it possible to set it to NULL in some way, meaning it will not reference the other table? In case, how would I do that? (For an integer key field)
Also, what if you save a row with a valid foreign key that corresponds to an existing primary key in other table. But then decide to delete that entry in this other table. So the foreign key will no longer be valid. Will I be allowed to delete? How does it work? I would think it should then be simply reset to a null value.. But maybe it's not that simple?
What you need to do is insert your data starting from the parent down.
So if you have an orders table and an items table that refers to orders, you have to create the new order first before adding all the children to the list.
Many of the data access libraries that you can get (in C# there is Linq to SQL) which will try and abstract this problem.
If you need to delete data you actually have to go the other way, delete the items before you delete the parent order record.
Of course, this assumes you are enforcing the foreign key, it is possible to not enforce the key, which might be useful during a bulk delete.
This is because of "bad data" you have in the tables. Check if you have all corresponding values in the primary table.
DBMS checks the referential integrity for ensuring the "correctness" of data within database.
For example, if you have a column called some_id in TableA with values 1 through 10 and a column called some_id in TableB with values 1 through 11 then TableA has no corresponding value (11) for that which you have already in TableB.
You can make a foreign key nullable but I don't recommend it. There are too many problems and inconsistencies that can arise. Redesign your tables so that you don't need to populate the foreign key for values that don't exist. Usually you can do that by moving the column to a new table for example.