これは、特にあなたが言及したすべてのデータベースに対して、そのような自動化されたスクリプトを作成するための大きなタスクです。あなたはあなたのためにそれをするためにperl/pythonの知識を持ついくつかの良いDBAを雇うことを検討するべきです。
最小限のチェックで(キーの列の数と列のタイプはクロスチェックされません)、以下はPostgreSQLの概念実証になる可能性があります。
WITH colnames(oid,colnum,colname) AS (
SELECT a.attrelid AS oid, a.attnum AS colnum, a.attname AS colname
FROM pg_attribute a WHERE a.attnum > 0 AND NOT a.attisdropped),
pkcols(oid,colnum) AS (
SELECT c.conrelid AS oid, unnest(c.conkey)
FROM pg_constraint c
WHERE contype = 'p'),
pks(oid,colnum,colname) AS (
SELECT pkc.oid, pkc.colnum, cn.colname
FROM pkcols pkc
JOIN colnames cn ON pkc.oid = cn.oid AND pkc.colnum = cn.colnum),
pktabs AS (
SELECT t.oid AS oid, n.nspname AS sname, t.relname AS tname,
pks.colnum AS colnum, pks.colname AS colname
FROM pg_class t
JOIN pg_namespace n ON n.oid = t.relnamespace
JOIN pks ON pks.oid = t.oid
WHERE t.relkind = 'r'),
coltabs AS (
SELECT t.oid AS oid, n.nspname AS sname, t.relname AS tname,
cn.colnum AS colnum, cn.colname AS colname
FROM pg_class t
JOIN pg_namespace n ON n.oid = t.relnamespace
JOIN colnames cn ON cn.oid = t.oid
WHERE t.relkind = 'r')
SELECT 'ALTER TABLE '||quote_ident(ct.sname)||'.'||quote_ident(ct.tname)||
' ADD FOREIGN KEY ('||string_agg(quote_ident(pk.colname),',')||
') REFERENCES '||
quote_ident(pk.sname)||'.'||quote_ident(pk.tname)||' ('||
string_agg(quote_ident(pk.colname),',')||');'
FROM pktabs pk
JOIN coltabs ct ON pk.oid != ct.oid AND pk.colname = ct.colname
GROUP BY pk.sname,pk.tname,ct.sname,ct.tname;