Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a table in another database

Tags:

alias

mysql

I've been asked to build a module for a web application, which will also be used as a stand alone website. Since this is the case, I wanted to use a separate database, and wondered if there was a way of having a table in one database, be a "pointer" in another database.

For example, I have databases db1 and db2

db1 has table users, so I want to have db2.users point to db1.users.

I know I could setup triggers and what not to sync two seperate tables but this sounds cooler :)

EDIT

So in my code I'm using sql such as

select * from users

Now, at the database level, I want "users" to actually be db1.users. Then, if I want to, I can remove the alias/pointer and "select * from users" will point to the users table in the current database. I guess what I'm looking for is a "global alias" type of thing.

like image 675
Abe Petrillo Avatar asked Apr 15 '11 09:04

Abe Petrillo


2 Answers

Just use it directly from another database?

SELECT ... FROM `db1`.`users` LEFT JOIN `db2`.`something`
like image 112
Emmerman Avatar answered Nov 14 '22 23:11

Emmerman


The federated storage engine offers something similar to the feature you asked for. And if your databases are on the same database server, the federated storage enging sounds a bit like an overkill to me. You may want to create a view instead.

Both methods won't be useful if db1 is not available. As Emmerman already points out, you need to store the data in db2 if you want to prepare for the case of db1 being unavailable.

like image 31
titanoboa Avatar answered Nov 14 '22 23:11

titanoboa