Synonyms Flashcards
What is a synonym?
It’s basically an alias for an object name. Much like you would alias a column name.
SQL Server 70-461 09-02
Syntax to create a synonym
USE TSQL2012 GO CREATE SYNONYM dbo.Categories FOR Production.Categories; GO
dbo.Categories synonym name
Production.Categories object you are creating a synonym for
SQL Server 70-461 09-02
Does the object the synonym will be an alias for need to exist when the synonym is created?
- No. Because of the late binding of synonyms
- When you actually use the synonym in a tsql statement, SQL Server will check for the objects existence.
SQL Server 70-461 09-02
What rules must be complied with when naming a synonym?
Rules for tsql identifiers
SQL Server 70-461 09-02
Do synonyms store any data or code?
No
SQL Server 70-461 09-02
If you don’t specify a schema when you create the synonym, what schema will it be assigned to?
The default schema associated with the user name.
SQL Server 70-461 09-02
What 5 types of objects can synonyms be used for?
- Tables, including temporary tables
- Views
- User-defined functions (Scalar, table-valued, inline)
- Stored procs (T-SQL, extended stored procedures and replication filter procedures)
- CLR Assemblies (stored procedures; table-valued, scalar, and aggregate functions)
SQL Server 70-461 09-02
Can synonyms refer to synonyms?
No! They can only refer to database objects.
SQL Server 70-461 09-02
Where can you use synonyms?
In TSQL statements that refer to the types of objects that synonyms stand for.
EXECUTE
SELECT
INSERT
UPDATE
DELETE
SQL Server 70-461 09-02
What statement can you not reference a synonym in?
ALTER. You must reference the base object instead.
SQL Server 70-461 09-02
How do you ALTER a synonym itself?
- You can’t. There is no ALTER SYNONYM statement.
- You have to drop it and recreate it.
SQL Server 70-461 09-02
Statement to drop synonym
DROP SYNONYM synonym_name
SQL Server 70-461 09-02
Does a synonym have to refer to an object in the same database it is created in?
No. It can refer to an object in another database, in addition to objects referenced by linked servers.
SQL Server 70-461 09-02
Can an object be dropped even if there is a synonym that refers to it?
- Yes, unfortunately
- There is nothing similar to SCHEMABINDING for synonyms
SQL Server 70-461 09-02
What is one advantage of being able to create a synonym for an object that doesn’t yet exist?
- You can use a single synonym for many different objects. Just recreate the synonym for each object as you need it.
- Or you could create the same synonym in multiple databases to reference the same object.
SQL Server 70-461 09-02