What is A Synonym in SQL?
The synonym in SQL provides us with an alternative name for a database object. This alternative name will refer to the respective original object. We cannot have synonyms with the same name, which means when defining a synonym we have to make sure that the name of that particular synonym is unique. Moreover, the alternative name that is provided by using a synonym is permanent.
The synonym cannot be accessed by other databases, that is why we can say that the synonym is database-independent. The synonym provides a level of abstraction that will allow users to work with these objects without knowing the details. Backward compatibility can also be provided when we are using the synonym. The lengthy and confusing object names of a database can be simplified with the help of the synonym. To define the synonym in SQL, the keyword SYNONYM is used.
To learn more about SQL SYNONYM, click here.
Syntax:
CREATE SYNONYM [synonym_name]
FOR [base_object_name];
Example
CREATE SYNONYM GFG
FOR dbo.w3wiki;
Now after defining the synonym, we can access it by using the synonym name.
SELECT * FROM GFG;
Difference Between Alias and Synonym in SQL
In SQL, two keywords are used to create an alternative name which are alias and synonym. Although both of these have similar functions, each of these keywords has its purpose. This means that the Alias has different usage from Synonym and vice versa.
This article aims to clarify the differences between the alias and the synonym and guide on when and how to use each effectively.