Remote foriegn key on SQL Server - mysql

I am building a database on SQL Server 2014. I have a users table and a profiles table and I need to have a relationship with both these tables. I am relating the userid (primary key on user table) to the profiles table (userid there as foreign key). This is just an example to consider.
What I need to know is, what if the profiles table is on another server instance? Is there a way with which I can link both? The reason is that I don't want to overload the sql server with too many tables and data...
Thanks,
Sarin Gopalan

It is not at all possible to create foreign key relations between databases - let alone between server instances.
You might be able to create a trigger on the Profiles table, that checks if a userid exists in the User table in the other database, but I fear the performance of this approach will be very bad.
A much better solution would be to replicate one table to the other database, and then create the foreign key relations in a normal way. How you replicate the table (SSIS, CDC, triggers, etc.) is up to you.

Related

Talend Open Studio data migration with MySQL on parent-child relation

I am using Talend Open Studio for data migration as I am upgrading my existing application architecture to a new one. I am using MySQL in both the applications but with different schema. I have migrated data successfully between single tables but while I am transferring data from a single table to a parent-child relationship table with a foreign key constraint, the data transfer is tremendously slow.
For e.g. I am migrating my Cities table to Cities and Citiesi18n and below is the schema for them:
My old schema :
CITIES (
id
city_name
status
created_at
)
The newly created schema where I need to migrate the data :
CITIES (
id
status
created_at
)
CITIESI18N (
id
lang_code
name
fk_city_id (// foreign key of cities table)
)
Below are the snapshots from my Talend jobs:
And here is the tmap configuration :
Now when I transfer the data without the foreign key the result are super fast. See below :
But the same when I transfer with a foreign key, my transfer is super slow :
(Note: I have taken province table for example as it is similar to cities table)
I think with Foreign key constraint it must be indexing the columns while transferring the data making it slower, but I am not sure. Is there any way I can fix this as I have a lot of tables similar to this which needs to be migrated. I am just curious to know the reason.
I don't know why you have this behaviour : you can try to redirect 'provience_i18n' to a tHashOutput (cache component), then link to a subjob with tHashInput (refering to your tHashOutput)-->tMySQLOutput. You'll have 2 subjobs, one for each insertion.
You are loading data to the parent & child at the same time. Using one tmap. When you are inserting foreign key in the second table, there is also insertion being made in the foreign/parent table. What you could alternatively do is: Load the data in the main CITIES table first, then onSubJobOk, load into child/CITIESI18N table. It would be faster. Let me know if it works.

Can a mysql database has foreign key that refer to tables in another db server

Say I have two tables, Address and Person, which resides on two totally separate servers. I want to refer to Address in Person by a foreign key.
I want to put them in separate servers, because they are with the databases that perform jobs closer to each of them. e.g. the DB person resides in may define a lot of tables that relate to Person, but none of them except Person relate to Address, and vice versa. Failing of the server Person resides on will have no impact on the Address's server, unless the foreign key is involved in the action.
If this makes sense, how can I do that in mysql? And how can I do that in java using hibernate?
No, but you can have trigger to ensure foreign key constraint.

using asp.net membership database with my own database

My plan is such as following:
I have the Asp.Net membership database that keeps info about users.
I want to create my own database in which some of the tables will have fields like userId as a foreign key to primary key field in the membership table Users. So how would I achieve this and what is the best practice for this? Or should I get the value of the user during run time, and then copy that value to my table without the need of any foreign key relations.
Just add tables to the existing database, and reference the pk from the Users table as needed. There's no need to use a separate database as long as it's all logically the same application.

How to 'follow' foreign keys in a SQL client

I am currently using Sequel Pro to view my database. I find much of my time spent on 'following' foreign keys. For example, if an entry in table A links to table B, I find myself writing down the id number, then clicking the other table, scrolling down to find that id, etc. It becomes quite tedious.
Is there a way in Sequel Pro or in another SQL client to link to the FK (e.g., double click the FK and it takes you to that entry)?
You can benefit from the foreign key lookup editor for easy editing of fields referencing other tables in dbForge Studio for MySQL.

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.