使用另一个数据库中的表
我被要求为 Web 应用程序构建一个模块,该模块也将用作独立网站。既然是这种情况,我想使用一个单独的数据库,并且想知道是否有一种方法可以在一个数据库中拥有一个表,在另一个数据库中成为一个“指针”。
例如,我有数据库db1和db2,
db1有表users,所以我想让db2.users指向db1.users。
我知道我可以设置触发器以及不同步两个单独的表的内容,但这听起来更酷:)
编辑
所以在我的代码中我使用 sql 现在
select * from users
,在数据库级别,我想要“用户“实际上是 db1.users。然后,如果我愿意,我可以删除别名/指针,并且“select * from users”将指向当前数据库中的用户表。我想我正在寻找的是“全局别名”类型的东西。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
直接从另一个数据库使用它吗?
Just use it directly from another database?
联合存储引擎提供了类似于您要求的功能。
如果您的数据库位于同一数据库服务器上,那么联合存储引擎对我来说听起来有点大材小用。您可能需要创建一个视图。
如果 db1 不可用,这两种方法都没有用。正如 Emmerman 已经指出的那样,如果您想为 db1 不可用的情况做好准备,则需要将数据存储在 db2 中。
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.