不同数据库之间的共享表(SQL Server)

时间:2022-12-31 09:07:16

Database1

  • --X_Table

  • --Y_Table

  • --Z_Table

Database2

  • --A_Table

  • --B_Table

  • --Z_Table

I have a table which is include product details and I use them for 2 seperated databases .Can i make Z_Table as a shared table?

我有一个包含产品详细信息的表,我将它们用于2个独立的数据库。我可以将Z_Table作为共享表吗?

Because when i want to update Z_Table i have to do that for both DB's.

因为当我想要更新Z_Table时,我必须为两个DB做到这一点。

**i know that i can deal with it with serverside coding (queries).But if i can do that on Sql server that would be nice.

**我知道我可以使用服务器端编码(查询)处理它。但是如果我能在Sql服务器上做到这一点会很好。

1 个解决方案

#1


2  

You can't actually have a shared table. But you can use synonyms. That means you could have Z_Table in Database1 and a synonym in Database2 which points to that table in Database1.

你实际上不能拥有共享表。但你可以使用同义词。这意味着您可以在Database1中使用Z_Table,在Database2中使用指向Database1中该表的同义词。

In Database2 you could create it like this.

在Database2中,您可以像这样创建它。

CREATE SYNONYM Z_Table FOR Database1.Z_Table

Then in queries from Database2 you just reference it by the name. You can treat it just like any other table.

然后在Database2的查询中,您只需通过名称引用它。您可以像对待任何其他表一样对待它。

You can read more about synonyms here. https://docs.microsoft.com/en-us/sql/relational-databases/synonyms/synonyms-database-engine

你可以在这里阅读更多关于同义词https://docs.microsoft.com/en-us/sql/relational-databases/synonyms/synonyms-database-engine

#1


2  

You can't actually have a shared table. But you can use synonyms. That means you could have Z_Table in Database1 and a synonym in Database2 which points to that table in Database1.

你实际上不能拥有共享表。但你可以使用同义词。这意味着您可以在Database1中使用Z_Table,在Database2中使用指向Database1中该表的同义词。

In Database2 you could create it like this.

在Database2中,您可以像这样创建它。

CREATE SYNONYM Z_Table FOR Database1.Z_Table

Then in queries from Database2 you just reference it by the name. You can treat it just like any other table.

然后在Database2的查询中,您只需通过名称引用它。您可以像对待任何其他表一样对待它。

You can read more about synonyms here. https://docs.microsoft.com/en-us/sql/relational-databases/synonyms/synonyms-database-engine

你可以在这里阅读更多关于同义词https://docs.microsoft.com/en-us/sql/relational-databases/synonyms/synonyms-database-engine