SQL Server 2005 introduces the concept of a synonym.
A synonyms is a single-part name which can replace multi part name in SQL
Statement. Use of synonyms cuts down typing long multi part server name and can
replace it with one synonyms. It also provides an abstractions layer which will
protect SQL statement using synonyms from changes in underlying objects (tables
etc).
A synonym is an identifier that can
be used to refer an object from the data base that is having a very long name.
Thus instead of using the long name every time we can have the alias or
synonym. It gives us and extra layer of security as we have only the Abstract
of the object in the form of name which helps us if the underlying object is
changed or modified.
We can refer the synonym to perform
operations on the base object. We can also use a synonym to refer objects on a
linked server, thereby masking the server name.
CREATE SYNONYM
statement:
CREATE SYNONYM [schema_name.]synonym_name FOR object_name
To create a synonym,
USE UrDBName;
GO
CREATE SYNONYM MySyn
GO
CREATE SYNONYM MySyn
FOR UrDBName.ObjectNsame
GO
Use Synonyms :
Use Synonyms :
USE test;
GO
SELECT * FROM MySyn;
GO
To drop a synonym, all you need to
do is execute the DROP SYNONYM command followed by the synonym name or
right-click the synonym and select delete from the context menu.
DROP SYNONYM [schema].SynonymName
Synonyms
can be created on only following objects.
- Assembly (CLR) Stored Procedure
- Assembly (CLR) Table-valued Function
- Assembly (CLR) Scalar Function
- Assembly (CLR) Aggregate Functions
- Replication-filter-procedure
- Extended Stored Procedure
- SQL Scalar Function
- SQL Table-valued Function
- SQL Inline-table-valued Function
- SQL Stored Procedure
- View
- Table (User-defined)
Additionally
SYNONYMS can be used only to change data of object not the schema of the
object. SYNONYMS can be used with only SELECT, UPDATE, INSERT, DELETE, EXECUTE
commands.
Now when writing the stored procedure instead of
having to write out that entire alias every time you accessed the table you can
just use the alias Clients. Furthermore, if you ever change the location or the
name of the production database location all you need to do is modify one
synonym instead of having to modify all of the stored procedures which
reference the old server.
Benefits
- SYNONYMs provide a layer of abstraction over the referenced object
- Allow changes to complicated (multi part) and lengthy names with a simplified alias as a same server resident object.
- Provides flexibility for changing the location of objects without changing existing code.
- SYNONYMs can be created in the same database to provide backward compatibility for older applications in case of drop or rename of objects.
- SYNONYMs can be useful if you give the front-end query tools like spreadsheets and Access linked tables direct links in to the tables.
Limitations
- 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.
- Chaining is not allowed. It means that you can not create SYNONYM of a SYNONYM.
- Obviously consumes possible object names, as you can not create a table with the same name of a synonym
- 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.
- SYNONYM can not be referenced in a DDL statement
No comments:
Post a Comment