When should database synonyms be used?

后端 未结 6 1537
借酒劲吻你
借酒劲吻你 2021-02-03 20:59

I\'ve got the syntax down but I\'m wondering if somebody can provide an illustrative use case where database synonyms are very useful.

6条回答
  •  时光取名叫无心
    2021-02-03 21:57

    In general, it is bad practice to embed schema names in SQL or PL*SQL. So if you are writing some code that must refer to a table in another schema like: "select id from OtherSchema.OtherTable" you are best off defining a synonym for the table (create synonym OtherTable for OtherSchema.OtherTable) and writing "select id from OtherTable".

    This way, if OtherTable moves to a different schema name, or you have another another installation of the system that uses a different schema name you can just redefine the synonyms instead of changing the code.

    It can also be used to switch your code between two schemas with the same structure by redefining the synonyms.

提交回复
热议问题