How to get relationship diagram of primary and foreign key in two tables of a database in sql server - sql-server-2014

I have a doubt about sql server: how to get primary and foreign keys
of relationship of 2 tables in sql server except the way sp_help table name?

You can use SchemaCrawler to generate a textual representation of your database schema including relationships indicated by foreign keys. You will need to install a Java runtime, though.
Sualeh Fatehi, SchemaCrawler

Related

JPA tool - Generate entities from tables with foreign keys mapped with non-primary key

I have a MySQL database on which I want to generate entities with Dali JPA Tool built into Eclipse.
The database is not mine, and I prefer not to ask for the change of architecture.
The problem exists when I create entities because a table has a foreign key that is mapped to a column in another table that is not a primary key.
I have noticed that for this reason the entity of this table is not created by the tool (but it's added in persistence.xml).
Is there a way to generate the entity without having to change the database architecture?
Thanks a lot for every possible idea.
PS: I'm not a database expert, but is it ok to create foreign keys that are mapped to columns that are not primary keys?

MySQL DB Foreign Key

What are the pros and cons of creating table relationships in a MySQL database using queries (JOINS) as opposed to doing it with DDL using Foreign key and referential integrity constraints? I have received a database that has not relationships (No FK) on its tables to identify relationships among tables. The relationships are being created on JOINS when data is being queried.
The main reason for using foreign keys is that data is always consistent in the database. This is "independent" from joins - when you use foreign keys you still need to use joins.
In MySQL it also has a nice side effect: if you use foreign keys, you have to define indexes which might speed up queries.
But with foreign keys you can make sure, that the row which you are referring must exist in the database. See https://en.wikipedia.org/wiki/Foreign_key.

Access not importing relationships for MySQL linked tables

I have successfully linked my MySQL database with my Access database file. Everything is working fine except the relationships in the MySQL database are not appearing in Access.
I have made a plenty of relationships in the MySQL tables using foreign keys, but these relationships are not reflected in Access. Kindly help me to import the relationships from the MySQL database into Access.
Software I'm using: MySQL version 5, Microsoft Office 2013, Access file format: .accdb
While it is true that the MySQL foreign key constraints don't show up by default in the Relationships tab in Access, those constraints are still in place in MySQL and are still enforced for linked tables.
For example, say I have two MySQL tables, [customers] and [orders], with a foreign-key constraint on [orders]. If I link to those tables in Access and I try to insert a row into my [orders] linked table where the [customerID] does not match a [customerID] in my [customers] linked table the insert fails:
ODBC --insert on a linked table 'orders' failed.
[MySQL][ODBC 5.2(w) Driver][mysqld-5.5.29-0ubuntu0.12.04.2]Cannot add or update a child row: a foreign key constraint fails (`zzzTest`,`orders`, CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`customerID`) REFERENCES `customers` (`customerID`)) (#1452)
You can go into the Relationships tab in Access and create "Access-side" relationships for the MySQL tables...
...but notice that the "Enforce Referential Integrity" options are greyed out because that is a function of the database setup at the server, not in Access. So really, the only benefits that the "Access-side" relationships would offer are:
"documentation" of the relationships (which you could get from a database diagram generated against the MySQL database), and
"automatic" joins between the linked tables in the Access query designer (which can also happen without [Access] Relationships if tables have columns with the same name).
It's up to you to decide whether it would be worth the trouble to create those "Access-side" relationships.
Since this is cross databases, it may have to recreated manually.
Check out this one, Importing .sql into MS Access using OBDC

how to make foreign key relationship on physically distributed data?

three servers
server 1 is central point which links the other two servers.
the other two servers have a table which has a field which should act like a foreign key
problem
well i do not know how to do this, using vs08 or sql server 08
diagram view
table on server 1
sv1pg1 id -- primary key
details
tables same schema on srvr2 and srvr3
linkedSRVid -- pk
linkedto -- fk constrain should be between (server 1 primary key and this field)
note that
"this is just a simplest! way i can think of to represent the prob, the real dbs contain
many fields and tables and the following applies"
it's not always that the network connection between the server remains up.
the link only need for "writing purpose" not viewing, as viewing done by direct
connection to the respective server.
A foreign key constraint can reference other tables only within the same database. This means that even if those databases were on the same server it still would not work.
Considering that the schemas are the same, you may want to look into replication.

SQL 2008 Management Studio-- where to find table relationships?

I'm brand new to SQL Server 2008, and have some newbie questions about the diagram pane. I just dragged two tables onto it to do an inner join, and the console "knew" to create a one-to-many relationship between them. Where is this information kept in the Management Studio for me to look at closer?
Thanks!
Much of this information is actually available in system tables in the database containing the user tables.
Here are two of the available tables of interest. sys.tables contains table information, sys.foreign_keys contains relationship information via foreign key constraints.
sys.tables (Transact-SQL) # MSDN
sys.foreign_keys (Transact-SQL) #MSDN
Also handy is the following information on Pinal Dave's site on using this information for your own lookups.
Find tables with Foreign Key Constraint in Database # SQLAuthority.com
Find Relationship of Primary key and Foreign Key using T-SQL # SQLAuthority.com
One of the tables has a foreign key referencing the other table. In Object Explorer, expand the information for the tables involved and look under Keys.