How to discover associations between tables in a PostgreSQL database? - database

I'm dealing with a huge ERP database, hundreds of tables, and am having trouble figuring out how one entity is referenced from another. Let's call them the "sale" table and the "shipment" table. Each has FK relationships with numerous other tables, but no FK links either one to the other, and no obvious associative table is linked to both.
Is there any good way using SQL or the psql command line to discover the chains of relationships that connect the two tables?
Is there any good way to discover the chains that connect a specific row or PK of "sale" and a specific row/PK of "shipment"?

You should look at a tool like schemacrawler. It's good a opensource tool for data modelling retro-engineering.
You can find documentation here.

Related

Better practice for SQL? One database for shared resources or tables in each Database with those resources

I have shared resources across all of my databases. Users, Companies etc. These are shared between all of my databases and the tables are the same. I want to create on Database for these tables and have all of my databases reference this one instead of having multiple tables that are the same. I come from a C# background and I am not very proficient in SQL. I am writing a new application that uses several of the databases we have.
Question: Should I make one database an authoritative source on these resources? The problem I see is I need Foreign Key relationships between databases and without triggers this is not possible. Not to mention when I write my linq statements I cannot query by these items.
We were able to achieve this by having one central database as the source of truth, then having copies of the applicable tables moved out to all the databases that needed it via triggers. You have to make sure all CRUD is done to the source of truth database, otherwise it gets very complicated to manage everything. You can then create the foreign keys to the copy tables.

Transfer several joined tables from one database to another using SSIS?

I have several tables in my database A which are interconnected via foreign keys and contain values. These values need to be transfered to another database B, all dependencies must be preserved, but the actual (numeric) values of primary and foreign keys are, of course, of no importance.
What would be the easiest way to fulfill this task using SSIS?
Here are the approaches I tried but with no much success:
I implemented a really very sophisticated view with flattened data and a lot of redundancy in the data and bumped into the problem how to split the data from this flattened view into several tables connected via foreign keys. This might be a solution, but I would personally prefer to avoid the data flatenning step if possible.
I tried to copy the tables one-to-one using NOCHECK options to lift up the constraint checks and to perform insertion into PK and FK fields. This, however, confines my transfer to a complete new import, I cannot just "add" some new data to existing set of data that would be nice.
Any other suggestions?
Integration Services has a Control Flow called Transfer Database Task and Transfer SQL Server Objects Task exclusive for what you need.
Here is a tutorial for what you need LINK.

Too many linked tables in a project

I am using H2 Database for my Swing based Retail ERP (Point of Sale and Accounting) application.
We have two databases namely MainDB and CompanyDB in our application.
Main database to store all user information and company information. Whenever the user creates a Company, those data will be stored in company_table in MainDB subsequently CompanyDB will be created for each company.
We have same master tables (around 60) in the MainDB and in CompanyDB. I will be inserting and updating to both databases, whenever any master created or edited in the application.
Very recently I came to know about Linked Table, which is very useful for me in many occasions.
My question is, if I create linked table for all masters in my CompanyDB
Can I have only linked table for all masters in CompanyDB?
I cannot reference foreign key for all transaction and other tables, as the master table is a linked table. Is it acceptable not to have foreign key relationship?
Creating around 60 linked tables, will reduce performance?
At present, I am very satisfied with the performance.
Please advise and throw some light on this.
Thanks and regards.
Can I have only linked table for all masters in CompanyDB? Yes it is possible, even thought not very common. It really depends on the use case.
Is it acceptable not to have foreign key relationship? If you define a foreign key relationship between two tables in the MainDB then this will still be checked even if you do the updates in the CompanyDB. You can not have foreign key relationships across databases, but whether this is OK depends on the use case. For example in NoSQL databases, you typically don't have any foreign key relationships.
Creating around 60 linked tables, will reduce performance? The number of linked tables isn't a problem. But please note that each linked table individually is slower than a regular table. Whether this is acceptable depends on the use case.

SqlServer: How to get meta-data about tables and their relationships?

I was wondering if there was a way (relatively simple I hope) to get information about the table and its attributes and realtionships?
Clarification: I want to grab all tables in the database and get the meta-model for the whole database, tables, column data, indicies, unique constraints, relationships between tables etc.
The system has a data dictionary in sys.tables, sys.columns, sys.indexes and various other tables. You can query these tables to get metadata about the database structure. This posting has a script I wrote a few years ago to reverse engineer a database schema. If you take a look at it you can see some examples of how to use the system data dictionary tables.
there are a whole bunch of system views in the information_schema schema in sql server 2005+. is there anything in particular you're wanting?
some of those views include:
check_contraints,
columns,
tables,
views
Try sp_help <tablename>. This will show you foreign key refrences and data about the columns, etc - that is, if you are interested in a specific table, as your question seemed to indicate.
If using .NET code is an option SMO is the best way to do it.
It abstracts away all these system views and tables hiding them behind nice and easy to use classes and collections.
http://msdn.microsoft.com/en-us/library/ms162169.aspx
This is the same infrastructure SQL Server Management Studio uses itself. It even supports scripting.
Abstraction comes at a cost though so you need maximum performance you'd still have to use system views and custom SQL.
You can try to use this library db-meta

Separating weakly linked database schemas

I've been tasked with revisiting a database schema we designed and use internally for various ticketing and reporting systems. Currently there exists about 40 tables in one Oracle database schema supporting perhaps six webapps.
However, there's one unifying relationship amongst them all: a rooms table describing the room. Room name, purpose and other data are thrown into a shared table for each app. My initial idea was to pull each of these applications into a separate database, and perform joins between a given database and the room database. But I've discovered this solution prevents foreign key constraints in SQL Server 2005. It seems silly to duplicate one table for each app and keep those multiple copies synchronized.
Should I just leave everything in one large DB, or is there something else I can do separate the tables without losing FK constraints?
The only way to achieve built-in referential integrity is to have the table inside the database in which it is referenced. You might be able to achieve the equivalent of referential integrity using triggers but it would likely be deathly slow.
You might be able to use SQL Server replication, in it's "Transactional replication" mode/form. http://msdn.microsoft.com/en-us/library/ms151176.aspx
if all the apps truly use and depend on the rooms - then keep them all in one DB.
you can still set privilege on the tables properly, and manage the data sets in the non overlapping areas normally -
is there any task you imagine you will not be able to perform when things are together?

Resources