Using PostgreSQL 7.1.3.
I'm running into an issue: Let's say we have two tables:
master
id serial,
data varchar
and
slave
master integer foreign key >> master.id
moredata varchar
And I want to delete a record in master. Without previously being aware of the "slave" table, can I derive all tables that contain a foreign key reference to a particular record in master?
I'd write the sql as something like:
// value of serial key in master:
$recordid=17;
"select table, field from pg_tables where has(foreign_key, master.id=$recordid)";
Which would return all tables and fields that contain at least 1 reference to record #17 in the master table.
Then, we could do something like
while $row=pg_fetch_row($result, $i++)
pg_exec($con, "delete from $row[table]
where $row[field]=$recordid");
Is there a way to do this?