Some Useful pgsql

阅读更多
1) How can I find the database OID from simple SQL command?
SELECT oid, * from pg_database

2) How can I find all the foreign keys of a table?
        SELECT f.conname, pg_get_constraintdef(f.oid), t.relname
          FROM pg_class t, pg_constraint f
         WHERE f.conrelid = t.oid
           AND f.contype = 'f'
           AND t.relname = 'table_name'

2) How can I find all the foreign keys which reference a table?
        SELECT f.conname, pg_get_constraintdef(f.oid), t2.relname
          FROM pg_class t, pg_class t2, pg_constraint f
         WHERE f.confrelid = t.oid
           AND f.conrelid = t2.oid
           AND f.contype = 'f'
           AND t.relname = 'table_name'

你可能感兴趣的:(F#,SQL)