How do you document your database code to see dependencies between database objects? [closed]

我怕爱的太早我们不能终老 提交于 2019-12-04 14:01:52

You can actually collect some of this information by querying the database's internal "depends" information. If something depends on another, that suggests it uses it. Here's a sample query to give you an idea how to traverse the two structures involved:

SELECT
  c1.oid as relid,
  n1.nspname || '.' || c1.relname as relation,
  c1.relkind,
  c2.oid as relid,
  n2.nspname || '.' || c2.relname as dependency,
  c2.relkind
FROM 
  pg_depend d,
  pg_class c1,
  pg_namespace n1,
  pg_class c2,
  pg_namespace n2
WHERE 
  d.objid = c1.oid AND
  c1.relnamespace = n1.oid AND 
  n1.nspname NOT IN('information_schema', 'pg_catalog') AND
  n1.nspname !~ '^pg_toast' AND
  d.refobjid = c2.oid AND
  c2.relnamespace = n2.oid AND 
  n2.nspname NOT IN('information_schema', 'pg_catalog') AND
  n2.nspname !~ '^pg_toast' AND
  c1.oid != c2.oid
GROUP BY n1.nspname,c1.relname,c1.oid,c1.relkind,
  n2.nspname,c2.relname,c2.oid,c2.relkind
ORDER BY n1.nspname,c1.relname;

Information about all these internal bits can be found in the system catalog documentation.

I don't document in order to see dependencies. Documentation is automatically out of date.

I use a tool for that. At present I use the products from ApexSQL, but I've used the Redgate tools in the past.

Just draw a EAR diagram. Entity-Relation diagram.

This tutorial should give you a good understanding of it. http://www.scribd.com/doc/7500847/entity-relationship-diagram

And you have this one http://www.getahead-direct.com/gwentrel.htm

Edit:

Lets say you have a tabe CAR, then you draw a box CAR:

CAR
----------
id (int)
name (vchar)
numbSeats (int)
----------
GetCar
SetCar
DeleteCar

Thelast 3 are your functions.

consider using a naming convention to reinforce SQL object dependencies:

Table1

Table2

vw_table1_byField1

vw_table1_byField1_table2

vw_table2

fn_table1

fn_table1_table2

I finally build a huge hmtl file.

File contains alot of anchors and it's easy to navigate to different objects.

It's a lot of work but it was exactly what I want :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!