Friday, February 9, 2018

What are the pros/cons of using a synonym vs. a view?

They are different things. A synonym is an alias for the object directly, a view is a construct over one or more tables.

Some reasons to use a view:

May need to filter, join or otherwise frig with the structure and semantics of the result set

May need to provide legacy support for an underlying structure that has changed but has dependencies that you do not want to re-work.

May provide security where some of the contents of the table should be visible to a class of users but not all. This could involve removing columns with sensitive information or filtering out a subset of the records.

May wish to encapsulate some business logic in a form that is accessible to users for reporting purposes.

You may wish to unify data from more than one source.

Reasons to use a synonym:

You may wish to alias an object in another database, where you can't (or don't want to) hard code the reference to the specific database.

You may wish to redirect to a source that changes over time, such as an archive table.

You want to alias something in a way that does not affect the query optimiser.

syntax: -


Create View vwtablename

as 

Select *  from tablename  where CreatedOn>'2014-01-26'

union

Select *  from tablename  where CreatedOn<='2014-01-26'

go
select * from vwtablename
go
Drop  View vwtablename

Synonym is just a second name of table used for multiple link of database.

View can be created with many tables, and with virtual columns and with conditions. But synonym can be on view.

Alias is temporary and used with one query. Synonym is permanent and not used as alias.


With a view, I can

hide columns
add predicates (WHERE clause) to restrict rows
rename columns
give a column name to a SQL expression
With a synonym, I can:

reference objects in other schemas and databases without qualifying the name

No comments:

Change default Port on Next.js app

 If any other app or process is running on port 3000 , you will get this error in your terminal Port 3000 is already in use. error Command f...