﻿{"id":314,"date":"2014-10-15T12:00:22","date_gmt":"2014-10-15T11:00:22","guid":{"rendered":"http:\/\/www.codereview.co\/?p=314"},"modified":"2014-10-15T12:08:36","modified_gmt":"2014-10-15T11:08:36","slug":"synonyms-in-sql-server-2005-benefits-and-limitations","status":"publish","type":"post","link":"http:\/\/www.codereview.co\/index.php\/sql-server-tutorials\/synonyms-in-sql-server-2005-benefits-and-limitations\/","title":{"rendered":"Synonyms in SQL Server 2005 (benefits and limitations)"},"content":{"rendered":"<p><span style=\"color: #000000;\"><strong>Scenario<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">I faced a situation where archived tables were created in the production database and now there was a requirement to move them as the database was growing. These archival tables were being used by several jobs and also in the application code. Moving them was demanding and also a very complicated process. I wanted to find a way to minimize the amount of work that the development team had to do, since their time was limited too.\u00a0 Based on the needs and the limited time I wasn&#8217;t sure what was the best option.\u00a0.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Solution<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">Fortunately I found a feature in SQL Server 2005 which solved my problems and provided further optimization and facilities. The feature is SYNONYMs in SQL Server 2005. A SYNONYM is new to SQL Server 2005. It is a way to give an alias to an already existing or potential new object. It is just a pointer or reference, so it is not considered to be an object.\u00a0<\/span><\/p>\n<p><span style=\"color: #000000;\">In order to create a synonym, you need to have CREATE SYNONYM permissions. If you are a sysadmin or db_owner you will have these privileges or you can grant this permission to other users. Also, you create a synonym that crosses databases you will need permissions for the other database as well..<\/span><\/p>\n<p><span style=\"color: #000000;\">Here is an example to create the SYNONYM in a database that references another object in the database.<\/span><\/p>\n<table width=\"100%\">\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\">USE AdventureWorks GO CREATE SYNONYM MySyn FOR Production.Location GO<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\">To check that this works you can issue a query such as below that uses the new SYNONYM.<\/span><\/p>\n<table width=\"100%\">\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\">SELECT * FROM MySyn<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\">Here is an example to create the SYNONYM in one database that references an object in another database.<\/span><\/p>\n<table width=\"100%\">\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\">USE master GO CREATE SYNONYM dbo.MySyn FOR AdventureWorks.Production.Location GO<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\">To get the meta data for all synonyms use the following command<\/span><\/p>\n<table width=\"100%\">\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\">SELECT * FROM sysobjects WHERE xtype = &#8216;SN&#8217; ORDER BY NAME<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\">And to drop the synonym use the following command<\/span><\/p>\n<table width=\"100%\">\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\">USE AdventureWorks; GO DROP SYNONYM MySyn GO<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\">SYNONYM&#8217;s can be very useful and can be created for<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">Tables<\/span><\/li>\n<li><span style=\"color: #000000;\">Views<\/span><\/li>\n<li><span style=\"color: #000000;\">Assembly Stored Procedures, Table Valued Functions, Aggregations<\/span><\/li>\n<li><span style=\"color: #000000;\">SQL Scalar Functions<\/span><\/li>\n<li><span style=\"color: #000000;\">SQL Stored Procedures<\/span><\/li>\n<li><span style=\"color: #000000;\">SQL Table Valued Functions<\/span><\/li>\n<li><span style=\"color: #000000;\">SQL Inline-Table-Valued Functions<\/span><\/li>\n<li><span style=\"color: #000000;\">Local and Global Temporary Tables<\/span><\/li>\n<li><span style=\"color: #000000;\">Replication-filter-procedures<\/span><\/li>\n<li><span style=\"color: #000000;\">Extended Stored Procedures<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><strong>Benefits<\/strong><\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">SYNONYMs provide a layer of abstraction over the referenced object<\/span><\/li>\n<li><span style=\"color: #000000;\">Allow changes to complicated (multi part) and lengthy names with a simplified alias as a same server resident object.<\/span><\/li>\n<li><span style=\"color: #000000;\">Provides flexibility for changing the location of objects without changing existing code.<\/span><\/li>\n<li><span style=\"color: #000000;\">SYNONYMs can be created in the same database to provide backward compatibility for older applications in case of drop or rename of objects.<\/span><\/li>\n<li><span style=\"color: #000000;\">SYNONYMs can be useful if you give the front-end query tools like spreadsheets and Access linked tables direct links in to the tables.<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><strong>Limitations<\/strong><\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">SYNONYMs are loosely bound to the referenced objects. So you can delete a SYNONYM without getting any warning that it is being referenced by any other database object.<\/span><\/li>\n<li><span style=\"color: #000000;\">Chaining is not allowed. It means that you can not create SYNONYM of a SYNONYM.<\/span><\/li>\n<li><span style=\"color: #000000;\">Obviously consumes possible object names, as you can not create a table with the same name of a synonym<\/span><\/li>\n<li><span style=\"color: #000000;\">The object for which the SYNONYM is being created is checked at run time. It is not checked at creation time. So this means that if you make any related error e.g. spelling error, the synonym will created, but you will get an error while accessing the object.<\/span><\/li>\n<li><span style=\"color: #000000;\">SYNONYM can not be referenced in a DDL statement<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\">\u00a0<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Thanks for reading this article,<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Next steps :<\/strong><\/span><\/p>\n<ol>\n<li><span style=\"color: #000000;\"><b><strong>Add this article to your database toolkit<\/strong><\/b><\/span><\/li>\n<li><span style=\"color: #000000;\"><strong>Share this with your colleagues because Sharing is Learning<\/strong><\/span><\/li>\n<li><span style=\"color: #000000;\"><strong>Comment below if you need any assistance<\/strong><\/span><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Scenario I faced a situation where archived tables were created in the production database and now there was a requirement to move them as the database was growing. These archival tables were being used by several jobs and also in the application code. Moving them was demanding and also a very complicated process. I wanted [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[173,32,3],"tags":[168,169,13,12,167],"_links":{"self":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/314"}],"collection":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/comments?post=314"}],"version-history":[{"count":4,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/314\/revisions"}],"predecessor-version":[{"id":359,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/314\/revisions\/359"}],"wp:attachment":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/media?parent=314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/categories?post=314"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/tags?post=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}