为什么要使用同义词?,SQL 中同义词的优点?
3 回答
它们只是数据库中对象的简写名称。例如,Products
如果您在名为ProductionControl.Inventory.Products
. 它们还可以方便地控制对存储过程中其他数据库的命名访问。如果您的 SP 引用其他数据库中的表,则创建同义词并使用它可以让您在同义词的目标发生变化时获得更多控制权。这在您拥有引用开发数据库的 SP 但部署到生产时名称不同的情况下很有用。所以你只需更新同义词就可以了。
从 MSDN理解同义词
同义词是用于以下目的的数据库对象:
为可以存在于本地或远程服务器上的另一个数据库对象(称为基础对象)提供替代名称。
提供一个抽象层,保护客户端应用程序免受对基础对象的名称或位置的更改。
In some enterprise systems, you may have to deal with remote objects over which you have no control. For example, a database that is maintained by another department or team.
Synonyms can help you decouple the name and location of the underlying object from your SQL code. That way you can code against a synonym table even if the table you want is moved to a new server/database or renamed.
For example, I could write a query like this:
insert into MyTable
(...)
select ...
from remoteServer.remoteDatabase.dbo.Employee
but then if the server, or database, schema, or table changes it would impact my code. Instead I can create a synonym for the remote server and use the synonym instead:
insert into MyTable
(...)
select ...
from EmployeeSynonym
If the underlying object changes location or name, I only need to update my synonym to point to the new object.