SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Synonyms


Synonyms

Author
Message
bkubicek
bkubicek
SSCrazy Eights
SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)

Group: General Forum Members
Points: 9658 Visits: 1092
Comments posted to this topic are about the item Synonyms
robert.sterbal 56890
robert.sterbal 56890
SSCrazy Eights
SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)

Group: General Forum Members
Points: 9557 Visits: 1722
How many times does the view for a table:

select * from table_name

get improved by using the synonyms?

Is this standards SQL?
Ivanova
Ivanova
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1138 Visits: 1027
In a previous role, I used synonyms to allow us to keep a fraud detection application running 24x7 with regular updates of 3rd party supplied data (known dodgy IP addresses, etc). There were two identical sets of tables, call them setA and setB with each table being referenced by a synonym. The application used the synonym at all times. When it was time to load the data, we cleared down the tables not currently in use, populated them with the latest supplied data, then redefined the synonyms to point to the newly-loaded set of tables. A metadata table kept track of which version of each table was current at any time and supported a completely automated refresh and switch process.

The main issue I've seen with synonyms is DBA suspicion; those who think synonyms are an unnecessary complication are not motivated to support a database which uses them, and they are apt to blame the synonyms as soon as anything goes wrong.
richardmgreen1
richardmgreen1
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8270 Visits: 1536
We use synonyms to point between our test and production 3-rd party databases.
It helps when we deploy code between our test and production SQL boxes.

We've found it also saves typing out the full 4-part table name (we use linked servers to access the base data).

When we get our databases sorted out properly, we can just repoint our synonyms and not have to change anything else.
Japie Botma
Japie Botma
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2523 Visits: 451
richardmgreen1 - Monday, January 7, 2019 3:10 AM
We use synonyms to point between our test and production 3-rd party databases.
It helps when we deploy code between our test and production SQL boxes.

We've found it also saves typing out the full 4-part table name (we use linked servers to access the base data).

When we get our databases sorted out properly, we can just repoint our synonyms and not have to change anything else.

We did about the same thing, but it does cause confusion when you look at code and do not realize that the table is actually on another server.


5ilverFox
Consulting DBA / Developer
South Africa
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)

Group: General Forum Members
Points: 907015 Visits: 48688
robert.sterbal 56890 - Sunday, January 6, 2019 9:47 PM
How many times does the view for a table:

select * from table_name

get improved by using the synonyms?

Is this standards SQL?


There's no performance difference between a Synonym and a Pass-Through-View (PTView for short).

The reason why I prefer Synonyms for inter-database code is because it keeps things more separate when it comes to views. I also don't have to update any views if someone makes the mistake of using SELECT * for the PTView.

As for the reason why I use Synonyms for such things instead of using 3 or 4 part naming is because there is no guarantee that database names and which server a database will be moved to will be the same. I also have multiple copies of certain databases in Dev and Staging and that means the database MUST have different names. When the code moves from one environment to another, I don't need to do a thing because the 2 part naming convention in the code recognizes the common named Synonyms in the database where the code is deployed to. There is no need to find the database name in any of the code and change it. The Synonyms take care of all of that.

The same goes for a restore from, say, Production to a lesser environment. Instead of having to change any code for database name changes due to being in a different environment, I only need to generate the code for the Synonyms, do a couple of Search'n'Replaces, drop all the Synonyms, and use the generated script to rebuild them to point them to the correct databases.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

When you put the right degree of spin on it, the number 318 is also a glyph that describes the nature of a DBAs job. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)

Group: General Forum Members
Points: 907015 Visits: 48688
Ivanova - Monday, January 7, 2019 1:24 AM
In a previous role, I used synonyms to allow us to keep a fraud detection application running 24x7 with regular updates of 3rd party supplied data (known dodgy IP addresses, etc). There were two identical sets of tables, call them setA and setB with each table being referenced by a synonym. The application used the synonym at all times. When it was time to load the data, we cleared down the tables not currently in use, populated them with the latest supplied data, then redefined the synonyms to point to the newly-loaded set of tables. A metadata table kept track of which version of each table was current at any time and supported a completely automated refresh and switch process.

The main issue I've seen with synonyms is DBA suspicion; those who think synonyms are an unnecessary complication are not motivated to support a database which uses them, and they are apt to blame the synonyms as soon as anything goes wrong.


Heh... no suspicion on the part of this DBA. In fact, I won't allow for anything more than 2 part naming in any of the databases on boxes that I'm responsible for.

And the use of "table flopping" as you've described is something I do all the time, as well, to keep tables online while a sister table is being loaded as a replacement. In fact, I'm the one that finally got people to start doing that when large sets of tables needed to be copied from external sources (IBM Power Systems, in this case). It's extremely effective and, if there's a failure during the load, you're still "in business" with the old data.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

When you put the right degree of spin on it, the number 318 is also a glyph that describes the nature of a DBAs job. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Rick-153145
Rick-153145
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2320 Visits: 750
I have used them for the same reason as Jeff. I actually found the developers were the ones who didn't much like them strangely, couldn't work out why and didn't get much help from them on why either, still made them use them. Smile
below86
below86
SSCrazy Eights
SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)

Group: General Forum Members
Points: 9570 Visits: 6002
At my prior job we used 3 part naming, our database names were the same on DEv, TEST, QA, PROD. My current job the database names start with the prefix of the environment, PROD_, TEST_, DEV_...
We uses synonyms a lot here, as a developer it doesn't bother me. The main issue is that all synonyms aren't named similar, some are just the table name, others have the suffix of '_syn'.

-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Stefan LG
Stefan LG
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1777 Visits: 280
Yes, we have used them before to make the transition from a development server to a production server easier.
In our case, some of the synonyms were referencing databases/tables via a Linked Server.
If I can remember correctly, is was not possible to run these CREATE scripts unless the Linked Server and the remote databases/tables where physically available? (i.e. some sort of pre-check).
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search