What is the use of SYNONYM?

后端 未结 7 658
长情又很酷
长情又很酷 2020-12-04 17:50

What is the use of SYNONYM in SQL Server 2008?

7条回答
  •  天涯浪人
    2020-12-04 18:20

    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.

    http://www.mssqltips.com/sqlservertip/1820/use-synonyms-to-abstract-the-location-of-sql-server-database-objects/

提交回复
热议问题