join the MSSQLTips community

Today's Site Sponsor


 

SQL Compare quickly and easily compares and synchronizes SQL Server database schemas
 



…try SQL Backup pro for faster, smaller, more robust backups.

How and why should I use SQL Server 2005 synonyms?

Written By: Jeremy Kadlec -- 9/18/2006 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
Changing the name of an table once an application has been deployed has traditionally been a difficult task in SQL Server 2000.  The typical solution in SQL Server 2000 for referencing a different object was to use a View.  A second option was to use the sp_rename system stored procedure to rename objects as needed.  Unfortunately, this limits the capabilities of referencing other objects.  The need to be able to reference another object can become critical during system migrations, application testing, data corruption, etc.  Does SQL Server 2005 handle this in a more elegant manner and support more than just tables? 

Solution
SQL Server 2005 has introduced synonyms which enables the reference of another object (View, Table, Stored Procedure or Function) potentially on a different server, database or schema in your environment.  In short, this means that the original object that is referenced in all of your code is really using a completely different underlying object, but no coding changes are necessary.  Think of this as an alias as a means to simplify migrations and application testing without the need to make any dependent coding changes.

How do I create a synonym via SQL Server 2005 Management Studio?

ID Directions Screen Shot
1 Launch GUI - To access the synonym interface navigate to SQL Server 2005 Management Studio | Server Name | Databases | Database Name | Synonyms | Right click on the Synonyms folder | New Synonym... Not applicable
2 General Tab - Complete the Synonym name which should be a new name in the schema followed by the information for the aliased object
3 Permission Tab - Click the 'Add' button to grant rights to the synonym for specific database users
4 Extended Properties Tab - Add any extended properties for documentation purposes

For more information visit - Using Extended Properties on Database Objects

How do I create a synonym via T-SQL?

USE [UserDefinedDatabase]
GO
CREATE SYNONYM [dbo].[Customer]
FOR
[JTKNotebook].[JTKTest].[dbo].[Customer]
GO
EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'This is a sample synonym referencing the JTKTest database' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'SYNONYM',@level1name=N'Customer'

How can I verify the statements issued against the synonym are affecting the base object?

Issue an INSERT statement against the synonym:

INSERT INTO [BaltSSUG].[dbo].[Customer]
([CustID] ,[CustName] ,[CustAddress] ,[CustCity] ,[CustState] ,[CustZipCode])
VALUES (99 ,'DBA Dave' ,'123 Main Street' ,'Warren' ,'OH' ,12345)

Issue a SELECT statement on the synonym:

SELECT *
FROM [BaltSSUG].[dbo].[Customer]

1 AAA 123 Main Washington DC 11111
2 BBB 456 Main New York NY 22222
3 CCC 789 Main St. Louis MO 33333
4 DDD 123 Maple Denver CO 44444
5 EEE 456 Maple San Fran CA 55555
99 DBA Dave 123 Main Street Warren OH 12345

Can I still issue statements against the base object?

Issue an INSERT statement against the base table:

INSERT INTO [JTKTest].[dbo].[Customer]
([CustID] ,[CustName] ,[CustAddress] ,[CustCity] ,[CustState] ,[CustZipCode])
VALUES (100 ,'Developer Donna' ,'456 Main Street' ,'Chicago' ,'IL' ,67890)

Issue a SELECT statement on the base table:

SELECT *
FROM [JTKTest].[dbo].[Customer]

1 AAA 123 Main Washington DC 11111
2 BBB 456 Main New York NY 22222
3 CCC 789 Main St. Louis MO 33333
4 DDD 123 Maple Denver CO 44444
5 EEE 456 Maple San Fran CA 55555
99 DBA Dave 123 Main Street Warren OH 12345
100 Developer Donna 456 Main Street Chicago IL 67890

Next Steps

  • As you begin to migrate to SQL Server 2005 consider synonyms as a means to alias objects in your environment without the need to change the dependent code.
  • The next time you need to make significant code changes due to application testing or a system migration and you are concerned about the level of effort to support the changes, first test the synonym functionality to determine if this will streamline the process and save time.
  • Do not limit yourself to uses for the synonyms, consider other applications such as in data warehousing when managing fact tables.
  • Check out our recent testing related tips:
Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Try Red Gate SQL Backup Pro for smaller, more robust SQL Server backups. Download a free trial now!

SQL Server Consultants - What you don't know could be your biggest asset - Guaranteed Results

Looking for SQL Server interview questions and answers?

Free whitepaper - How to Achieve 40:1 Backup Compression with LiteSpeed® for SQL Server’s


Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.

Idera - SQL diagnostic manager

Idera SQL diagnostic manager is an award-winning performance monitoring solution for SQL Server that provides agent-less, real-time monitoring, customizable alerting, and extensive historical reporting. SQL diagnostic manager also puts must-have troubleshooting information at the DBA’s fingertips such as worst-performing code, long-running or frequently run queries, and blocking or blocked sessions.

Download now!



More SQL Server Tools
SQL Backup

SQL diagnostic manager

SQL comparison toolset

SQL safe backup

SQL secure




Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com