简体   繁体   中英

Create database model using columns names (no FK, no relation)

I have a database with no FKs, PKs and not any documentation to show what tables relates with each other. So i'm in big trouble to do a reverse engineering on the physical data to mind how does one table relates to another one. Does anyone knows a tool to create a model based only by the names of columns?

Example: Table A have a column with name ID_NTP_BAZINGA that relates to table B with the column name ID_NTP_BAZINGA.

Hm, it is hard task to do (and I kind of have done it myself), but I can think of some hints to automate your work, for example commands like sp_msforeachdb or sp_msforeachtable might be handy.

To determine FK relations in a way you mentioned, you could use such query:

select * from (
    select object_name(object_id) [table],
           name,
           count(*) over (partition by name) [cnt]
    from [DB_name].sys.columns
) a where cnt > 1

which, in your case, would return (among others)

Table A | ID_NTP_BAZINGA
Table B | ID_NTP_BAZINGA

and give you already some insight.

For candidates for PK, one could use sp_msforeachtable with dynamic SQL checking if count(distinct *) is equal to count(*) - this would tell you whether you have unique values or not, and in the end you would count(*) with is not null filter in where clause, so you'll know if you have null s in particular column.

These are some general hints and exact queries you have to write yourself. This answer will get you started.

This is data achaeology. It's ironic because one of the reasons for developing databases in the first place was to guarantee that data would be better documented than it had been in files and records.
One of the best handles on the data model in this case is the application code. Look at the sql used by the application, especially at the queries. Retrieval is when data gets turned into information, and that's where you'll get your clues. Pay special attention to the ON and WHERE clauses. You may be able to figure out while columns were functioning as PKs and FKs, even if they weren't declared as such.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM