Can you relate 2 tables of different databases by means of a foreign key?

1

I have 2 database Mysql which can be ejemplo1 and ejemplo2 , one of which ejemplo1 I can not touch its structure at all so I was forced to create my own ejemplo2 , the issue is that the first ejemplo1 has data from many users which I need to bring, but there is no table to save the login data to the system (username and password), this table I am creating in ejemplo2 in this table I plan to create a field dni_user which would relate to the field dni (Primary Key) of ejemplo1 and be able to make the relevant relationships with laravel and thus be able to recover the data of the user who login session.

I am not sure that this is possible to relate 2 tables of Base de datos different.

Or in any case, just connect the 2 databases to my system and do a query like for example:

SELECT 
    base1.*, base2.*
FROM 
    nombreBase1.nombreTabla base1
INNER JOIN 
    nombreBase2.nombreTabla base2 
ON base1.id=base2.id

In which I include my 2 databases in the query

Is it possible to make this relationship through foreign codes? Or should I do it the other way?

    
asked by Edwin Aquino 30.05.2018 в 18:12
source

1 answer

1

can be as follows

SELECT (campo1, campo2, campo3)
FROM base1.tabla1 t1 JOIN base2.tabla2 t2 
ON t2.columna = t1.columna;
  

What you need is that when you name each table, prefix the   name of the database from where you plan to extract them; so that from   this mode can be identified

Obviously you need to verify if your database user has permissions on both bases, if so, you can do it without major problem

    
answered by 30.05.2018 / 18:16
source