Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read RelationShip from SQL Server

 
Nghiem Nguyen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,
Now, I want to write a program that generate source code, but I have a problem : I don't know how to read the Relationship between 2 tables in SQL Server. (know exactly 1-n, 1-1 or no relationship).
Please help me to have a solution.
Sorry for my poor English and Thanks a lot.
Rock.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.sql.DatabaseMetaData gives you access to table information, their attributes, and their primary and foreign keys. From that you know whether there is a relationship between tables, but to infer what kind it is (1:1, 1:n, n:m) in a general way is not possible - it is not part of the data definition.

There are tools available that generate code based on a DB schema; have you looked at any of those?
 
Nghiem Nguyen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf Dittmer for your reply,It's really helpful.
I really want to write a program like those by myself and that is the last problem I had.
If you have any solution, please tell me.
Thanks a lot.
Rock.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Databases stores its metadata in tables, so you just need to read product documentation to find where and how the database do this. Than it is more safe to use these data than java.sql.DatabaseMetaData.

For example MSSQL provides also lot stored procedures for this, they begin with sp_*.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic