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

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?

Related

Bypass database constraints during record deletion

I have around 20 mapping tables which refer to a single table.
The single table being referenced is,
field (
id integer,
value char
)
The mapping tables are as,
employee_field_map (
employee_id integer references employee(id),
field_id references field(id)
)
dept_field_map (
dept_id integer references dept(id),
field_id references field(id)
)
and similar additional 18 mapping tables.
Now if I want to delete number of records from the field table where field.id = employee_field_map.field_id it takes very long amount of time because there are 20 mapping tables which refer to the field table; And for each of that mapping table a constraint violation check is performed before deleting a record from the field table.
A field table rcord will always be referenced by only one of the mapping table at a time.
In above scenario before deleting a record from field table of course the corresponding record in employee_field_map table is deleted first. So I know for sure that none of the mapping table contains a reference to the field table record being deleted. So is there a way to tell the database engine not to perform those constraint checks when the delete on field table is being performed?
Disabling the constraints is not an option unfortunately. Please advise.
Assuming each of the mapping tables has an index on field_id, then the lookups should not be expensive.
I am wondering why you are not declaring them using cascading delete foreign key references:
employee_field_map (
employee_id integer references employee(id),
field_id references field(id) on delete cascade
);
Nothing in your data model is saying that the field is in only one mapping table. In fact, I don't see why things are broken out the way they are. Presumably there is a reason for breaking the fields apart like this instead of just having a "type" column in the fields table.

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.

SQL how to delete rows across tables with same ID

In my tables for SQL, I have many tables each with an ID column I wish to keep incrementing, along with a main name table with an ID which will be the same value as the corresponding IDs in other tables for the relevant information. I wish to be able to make it so that, when I delete a row from the name table with a certain ID, all values across other tables with the same ID get deleted also. The issue appears to be that, if i create foreign key constraints from the name ID on the name table to the other IDs across other tables, it means these tables are no longer allowed to automatically increment.

Deleting rows that have no referring rows

Lets say I have a table with a fk constraint to the table pk.
I want to delete all rows that has no relation to it.
Is there a way to skip the rows that has a constraint violation and remove all others.
Perhaps a loop with a transaction for each row..
I think you want to delete all rows in pk that have no referring rows. If so:
delete from pk
where not exists (select 1
from fk
where fk.fk = pk.pk
);
Note: You do not want to use not in here, because fk.fk could be NULL.

SQL Server , inserting existing rows into the same table

I have a table say Table A
Col A is the primary key (identity).
Col b is a foreign key from some other table.
Now, for example I want to select the rows where colB = 2 , and insert the rows back into the table again but with a different value of col B say 5 . This can be done easily. Next once the rows are inserted again, I need to find the relation between the copies, especially.
Say new rows are inserted with primary key 6,7,8. Now I want to check which parent row (row with primary key value 1/2/3) has the same data as row with primary key 6. Similarly, I need to find relative for the row with primary key 7, 8. One way is to do the insert one row at a time so that we can say that row with primary key 1 is the copy of row with primary key 6. Another way to do is to join all the columns. I am trying to avoid all these to see if there is any easy solution possible.
Use SCOPE_IDENTITY() to get last primary key inserted value on your table. If another process could be modifying the table then do your update & query in one transaction.