Sunteți pe pagina 1din 4

Exam Tip Synonyms cannot refer to other synonyms.

They can only refer to database objects such as tables, views, stored procedures, and functions. In other words, synonym chainin g is not allowed. You can use synonyms in the T-SQL statements that refer to the types of objects that synonyms can stand for. In addition to EXECUTE for stored procedures, you can us e the statements for data manipulation: SELECT, INSERT, UPDATE, and DELETE. Note Using alter with synonyms You cannot reference a synonym in a DDL statement such as ALTER. Such statements require that you reference the base object instead. Lesson 2: Using Synonyms Chapter 9 317 Dropping a Synonym You can drop a synonym by using the DROP SYNONYM statement. DROP SYNONYM dbo.Categories Because there is no ALTER SYNONYM, to change a synonym, you must drop and recrea te it. Abstraction Layer Synonyms can refer to objects in other databases, in addition to objects referen ced by linked servers. That makes it possible to dramatically simplify queries in your databas e and potentially remove the need for three-part and four-part references. For example, suppose the database DB01 has a view called Sales.Reports, and it i s on the same server as TSQL2012. To query it from TSQL2012, you must write something lik e the following. SELECT report_id, report_name FROM ReportsDB.Sales.Reports Now suppose you add a synonym, called simply Sales.Reports. CREATE SYNONYM Sales.Reports FOR ReportsDB.Sales.Reports The query is now simplified to the following. SELECT report_id, report_name FROM Sales.Reports The user no longer has to remember the other database name. This turns out to be even more useful if you are using linked servers. Then you can reduce a four-part nam e down to two parts and even one part, making life much easier for the end user. Synonyms and References to Nonexisting Objects You can create a synonym even if the object referenced does not exist. An advant age is that you can use a single synonym for many different objects, just recreating the syn onym for each object as you need it. Or you can create the same synonym in many databases that refer to a single object, and not have to use a three-part reference. The disadvantage is that there is no such thing as WITH SCHEMABINDING: If you dr op an object in a database, it will be dropped whether or not a synonym references it. Any synonyms referencing the object are effectively orphans; they fail to work when someone t ries to use them. Synonym Permissions To create a synonym, you must have the CREATE SYNONYM permission, which inherits from the CONTROL SERVER permission. After you've created a synonym, you can grant oth

er users permissions such as EXECUTE or SELECT to the synonym, depending on the type of o bject the synonym stands for. 318 Chapter 9 Designing and Creating Views, Inline Functions, and Synonyms Comparing Synonyms with Other Database Objects Synonyms are unusual in that, although they are technically database objects bel onging to the same namespace as other database objects, they don't contain any data or any code. It s interesting to compare synonyms with the other database objects. Some advantages of synonyms over views are as follows: Unlike views, synonyms can stand in for many other kinds of objects, not just tab les. Just as with views, synonyms can provide an abstraction layer, allowing you to pr esent a logical view of a system without having to expose the physical names of the da tabase objects to the end user. If the underlying object is altered, the synonym will n ot break. Some disadvantages of synonyms are: Unlike views, synonyms cannot simplify complex logic like a view can simplify com plex joins. Synonyms are really just names. A view can refer to many tables but a synonym can only ever refer to just one obj ect. A view can reference another view, but a synonym cannot reference another synonym ; synonym chaining is not allowed. When a view stands in for a table, a user can see the columns and data types of the view. But a synonym does not expose the metadata of the underlying table or view that it stands for. This could be seen as either an advantage or a disadvantage depending on th e context: If you do not want to expose metadata to the user, this could be an advantage. In SSMS, when the user opens the tree to look at a synonym, the user will not see a ny columns or data types if the synonym refers to a table or view, nor will the use r see any parameters if the synonym refers to a procedure or function. If you do want to expose metadata to users as part of user education, then a syno nym could be a disadvantage. For example, the user might need external documentation to find out what columns are available. In most respects, synonyms behave just like other database objects such as table s, views, and T-SQL code objects: For example, you can use the synonym in SELECT statement s in place of table names, view names, and inline function names, and you can assign the sa me sets of permissions to synonyms that you can for tables and views. Quick Check 1. Does a synonym store T-SQL or any data? 2. Can synonyms be altered? Quick Check Answer 1. No, a synonym is just a name. All that is stored with a synonym is the object it refers to.

2. No, to change a synonym, you must drop and recreate it. Lesson 2: Using Synonyms Chapter 9 319 Practice Using Synonyms In this practice, you use what you ve learned about synonyms to create a user inte rface and to run reports. If you encounter a problem completing an exercise, you can install the completed projects from the Solution folder that is provided with the companion content for this ch apter and lesson. Exercise 1 Use Synonyms to Provide More Descriptive Names for Reporting In this exercise, you create a user interface for reporting in the TSQL database . Assume the following scenario: The TSQL2012 system has been in production for so me time now, and you have been asked to provide access for a new reporting applicat ion to the database. However, the current view names are not as descriptive as the repo rting users would like, so you will use synonyms to make them more descriptive. 1. Start in the TSQL2012 database. USE TSQL2012; GO 2. Create a special schema for reports. CREATE SCHEMA Reports AUTHORIZATION dbo; GO 3. Create a synonym for the Sales.CustOrders view in the TSQL2012 database. Look first at the data. SELECT custid, ordermonth, qty FROM Sales.CustOrders; You have determined that the data actually shows the customer ID, then the total of the qty column, by month. Therefore, create the TotalCustQtyByMonth synonym and test it. CREATE SYNONYM Reports.TotalCustQtyByMonth FOR Sales.CustOrders; SELECT custid, ordermonth, qty FROM Reports.TotalCustQtyByMonth; 4. Create a synonym for the Sales.EmpOrders view by inspecting the data first. SELECT empid, ordermonth, qty, val, numorders FROM Sales.EmpOrders; The data shows employee ID, then the total of the qty and val columns, by month. Therefore, create the TotalEmpQtyValOrdersByMonth synonym and test it. CREATE SYNONYM Reports.TotalEmpQtyValOrdersByMonth FOR Sales.EmpOrders; SELECT empid, ordermonth, qty, val, numorders FROM Reports.TotalEmpQtyValOrdersByMonth; 5. Inspect the data for Sales.OrderTotalsByYear. SELECT orderyear, qty FROM Sales.OrderTotalsByYear; 320 Chapter 9 Designing and Creating Views, Inline Functions, and Synonyms This view shows the total of the qty value by year, so name the synonym TotalQtyByYear. CREATE SYNONYM Reports.TotalQtyByYear FOR Sales.OrderTotalsByYear; SELECT orderyear, qty FROM Reports.TotalQtyByYear; 6. Inspect the data for Sales.OrderValues. SELECT orderid, custid, empid, shipperid, orderdate, requireddate, shippeddate, qty, val FROM Sales.OrderValues; This view shows the total of val and qty for each order, so name the synonym TotalQtyValOrders. CREATE SYNONYM Reports.TotalQtyValOrders FOR Sales.OrderValues; SELECT orderid, custid, empid, shipperid, orderdate, requireddate, shippeddate, qty, val

FROM Reports.TotalQtyValOrders; Note that there is no unique key on the combination of columns in the GROUP BY o f the Sales.OrderValues view. Right now, the number of rows grouped is also the nu mber of orders, but that is not guaranteed. Your feedback to the development team sho uld be that if this set of columns does define a unique row in the table, they shoul d create a unique constraint (or a unique index) on the table to enforce it. 7. Now inspect the metadata for the syno

S-ar putea să vă placă și