Sunteți pe pagina 1din 16

C H A P T E R

Schema Manager for Views, Synonyms, and Sequences

8
3 3 3 3

In This Chapter
Using Schema Manager for creating views Creating private and public Synonyms Setting up sequences

T
See Reference Section

his chapter explores further ways to use Schema Manager. Here you learn how to create and modify views, Synonyms, and sequences. The Schema Manager substitutes for handwritten SQL code. You can view the commands generated by the Schema Manager and even record them for later use. If a task described here has an equivalent SQL command, the Command Reference section of this book contains a crossreference to the command. Watch for the See Reference Section icon in the margin. Chapter 13 contains complete instructions for views, Synonyms, and sequences using the SQL commands.

CrossReference

Views
Views have one primary purpose: to rearrange the way you see a Table, a portion of a Table, or a group of Tables without actually creating any copies of the underlying data. Views enable you to use the data in more convenient packaging. For example, a certain Table contains all valid orders for every customer. You could create a view that shows you the valid orders for a single customer and reuse this view when displaying data to your customer on your Web site.

188

Chapter 8 3 Schema Manager for Views, Synonyms, and Sequences

CrossReference

Chapter 24 shows how to create views based on Objects (Object views). You can use a view anywhere you can use a Table. You can use views in the FROM clause of your SQL query or even in an INSERT, UPDATE, or DELETE command.

Modifying data using views


When you update data using a view, you actually update the underlying Tables data. This condition also applies when you insert or delete from a view. You cannot use some kinds of views for modifying data: 3 Views with set operators such as INTERSECT, UNION, and MINUS 3 Views with GROUP BY, connect BY, or START WITH clauses 3 Views with group functions such as AVG, SUM, or MAX 3 Views using the DISTINCT function 3 Views which join Tables (with some exceptions) You can create views that join several Tables and still allow the update of one Table. The updateable Table must have a unique Index on at least one Column in the joined Table. Even then, all Columns in the view are not updateable. To determine which columns, if any, are updateable, query the Data Dictionary view called USER_UPDATABLE_COLUMNS. This view displays the updateable Columns. For example, if you create a view named TANK_ANIMALS_VIEW that joins two Tables, the following SQL SELECT command shows which Columns you can use for updating:
SELECT COLUMN_NAME, UPDATABLE FROM USER_UPDATABLE_COLUMNS WHERE TABLE_NAME = TANK_ANIMALS_VIEW;

Use views to restrict the kind of data inserted or updated into the underlying Table with the WITH CHECK OPTION parameter. The WITH CHECK OPTION states all data modified through the view must fit within the view.

Creating a new view


See Reference Section

CREATE View

This section describes how to create a view using the Schema Manager.

Chapter 8 3 Views

189

To illustrate the following steps, lets create a view in the SEAPARK Schema: 1. Start the Schema Manager and log in as the DBA. 2. Click Object Create. A small pop-up menu lists Objects that you can create. 3. Click View. The Create View window appears as shown in Figure 8-1.

Figure 8-1: Schema Manager creates views here.

4. Type in the name of the view in the Name box. 5. Select the Schema in the Schema box. 6. Type in a query in the Query textbox. At this point, you must write a valid SQL query. As an example, a new view called CARETAKER_ANIMAL_VIEW is created. It merges the two Tables: TANK and AQUATIC_ANIMAL. (See Figure 7-1 in Chapter 7 for a database diagram showing the relationship of these Tables.) The query assumes the User logged in to Oracle8 is the name of the CARETAKER. The query follows:
SELECT TANK_NAME, ANIMAL_NAME, BIRTH_DATE FROM TANK T , AQUATIC_ANIMAL A WHERE CHIEF_CARETAKER_NAME = USER AND T.TANK_NO = A.TANK_NO

Figure 8-2 shows the appearance of the Create View window.

190

Chapter 8 3 Schema Manager for Views, Synonyms, and Sequences

Figure 8-2: Type in a valid query for use in the view.

7. Click the Advanced tab (Optional). The Advanced tab reveals the following view options (leave each option as default or modify as needed): Replace if Exists. Check this option to allow the new view to replace an existing view of the same name. Force. Check this option if you need to create the view even though the creator does not have SELECT privileges on the underlying Tables. You can also use this option to create a view that references a nonexistent Table. This type of view is invalid until the underlying Table exists and the User executing the view has appropriate privileges on the underlying Table. Read Only. Check this option to prevent all but query access to the view. Constraint. Check this option to add Constraints on the Columns in the view. Use this option for views with UPDATEs and INSERTs. With Check. Check this option if you want to restrict the User to inserting and updating rows that fit within the view. (This option may not work if the view contains a subquery.) Use this option for views with UPDATEs and INSERTs. Figure 8-3 shows the sample view with the Replace if Exists and With Check options selected.

Chapter 8 3 Views

191

Figure 8-3: Choose appropriate options for the view here.

8. Click Show SQL (Optional). This option expands the window to display the generated Create View command. 9. Click Apply to create the view. Assuming no error messages pop up, your view is created and you return to the main Schema Manager window.
CrossReference

Any valid SQL query can be used in a view. The query can use Column functions, pseudocolumns, group functions, and so forth. To learn more about constructing queries, see Chapter 9. Creating a view with Schema Manager is similar to creating a view with SQL*Plus. Using the Schema Manager to change an existing view is easier than using SQL*Plus, however. The next section shows how to change a view.

Changing a view with Schema Manager


The Schema Manager simplifies the modification of a views underlying SQL. Use the following steps to change the view: 1. Start the Schema Manager and log in as the DBA. 2. Double-click the View folder. A list of Schemas containing views appears. 3. Double-click the Schema that contains the view to be changed. 4. Click the view to be changed. The View Properties window appears in the right frame, as shown in Figure 8-4.

192

Chapter 8 3 Schema Manager for Views, Synonyms, and Sequences

Figure 8-4: Modify the view by typing in the query box.

5. Modify the query as needed. Simply type the corrections in the window containing the query. 6. Click the Advanced tab (Optional). Make changes if needed to the View parameters. See the preceding section for an explanation of each option in the Advanced tab. 7. Click Apply to save the changes. The next section shows how to remove a view.

Deleting (dropping) a view


Deleting a view with the Schema Manager is similar to deleting other Objects. Use the following steps to drop a view. 1. Start the Schema Manager and log in as the DBA. 2. Double-click the View folder. A list of Schemas containing views appears. 3. Double-click the Schema that contains the view to be dropped. 4. Click the view to be dropped. 5. Click the red X. 6. The Schema Manager verifies you want to drop the view, as shown in Figure 8-5.

Chapter 8 3 Synonyms

193

Figure 8-5: Schema Manager verifies the dropping of a view.

7. Click Yes to drop the view. You can use views for several tasks. Oracle8s Optimizer handles views as it handles any SQL query. Therefore, unlike SQL queries, you do not lose performance with views. The Synonym is another useful Object. The next section shows how to create and manage Synonyms using the Schema Manager.

Synonyms
Primarily, Synonyms enable multiple Users to reference an Object without adding the Schema as a prefix to the Object. You can make a Synonym for a Table, a view, or even another Synonym. In addition, you can create Synonyms for functions, Packages, Procedures, sequences, and database links to remote databases. After creation, you can use a Synonym as though it were the underlying Object. Like views, Synonyms do not contain their own data they are only a different way of referencing the underlying data. A Synonym can be private or public. Private Synonyms are created by a User for her own use and sole access. Public Synonyms are created by the DBA and are shared among all Users.

194

Chapter 8 3 Schema Manager for Views, Synonyms, and Sequences

Oracle8 comes with a set of Public Synonyms that reference the Data Dictionary views. Data Dictionary views are convenient reference views which enable you to use SQL queries to extract Table names Column attributes, space usage and other important database details. Table 8-1 lists some of the available Data Dictionary views. Schema Manager uses these same Data Dictionary views to prepare its display of Objects.

Table 8-1 Useful Data Dictionary Views


View Name Description Every Table, view, and Synonym available to the current User. The privileges that the current User retains in any database Table. Includes grants to public. The privileges received by current User or public. Does not include grants to Roles to which the current User belongs. The names and creation dates for all Users in the database. The remaining free space in each Tablespace. All installed products (such as SQL*Plus and PL/SQL) and their complete version numbers. All Tables, views, and Synonyms available to the current User. All Indexes created by the current User. Current Users Table statistics. Current Users Column statistics. Privileges that the current User has granted to others. Columns that the current User may update. Views owned by the current User. All Object-oriented Tables in the database. All Object Types. All Object types that are methods. All method results. Similar to Columns.

ALL_CATALOG ALL_TAB_GRANTS ALL_TAB_GRANTS_RECD

ALL_USERS DBA_FREE_SPACE PRODUCT_COMPONENT_VERSION USER_CATALOG USER_INDEXES USER_TABLES USER_TAB_COLUMNS USER_TAB_GRANTS_MADE USER_UPDATABLE_COLUMNS USER_VIEWS ALL_OBJECT_TABLES ALL_TYPES ALL_TYPE_METHODS ALL_METHOD_RESULTS

Chapter 8 3 Synonyms

195

When an SQL command contains a Table name without the Schema prefix, Oracle8 searches for a match for the Table name using the following hierarchy: 1. Table owned by the Current User. 2. Private Synonym owned by the current User. 3. Public Synonym. Application developers typically use private Synonyms as shorthand for Tables they need to reference often during SQL code development. This approach avoids the retyping of long Table names. Public Synonyms simplify the migration of applications. If the Schema names are different but the Tables are the same, an application can migrate easily from database to database. Typically, Synonyms are used to simplify the migration of applications from a development or test platform to a production platform. Sometimes, the DBA uses public Synonyms for common lookup Tables needed across multiple Schemas. Public Synonyms also enable two User groups to refer to a single Table with two different names. Views typically perform this task, but if the underlying Table is identical and each group wants a business-rule appropriate Name, a Synonym may be a good solution. For example, the accounting department may refer to the customer Table as CUSTOMER while the sales department insists the same Table must be named CLIENT. Creating two Synonyms for the same Table satisfies both departments.
Caution

When creating public Synonyms, Oracle8 Users often mistakenly assume they can immediately share their Tables with other Oracle8 Users. Creating a public Synonym for a Table does not automatically enable other Users to view or change the data in your Table. You must still assign the appropriate privileges using the Security Manager or SQL GRANT command. The next section shows how to use the Schema Manager to create a new Synonym.

Creating a new Synonym


Use the Schema Manager to create both private and public Synonyms. Follow these steps: 1. Start the Schema Manager and log in as the DBA. 2. Click Object Create. A small pop-up menu lists the Objects that you can create. 3. Click Synonym. The Create Synonym window appears as shown in Figure 8-6.

196

Chapter 8 3 Schema Manager for Views, Synonyms, and Sequences

Figure 8-6: Schema Manager creates Synonyms here.

4. Type in the name of the Synonym in the Name box. 5. Select the Schema in the Schema box. For public Synonyms, choose Public. For private Synonyms, choose the Schema that will own the Synonym. 6. Select Local or Remote Database radio button. 7. Select the type of Object the Synonym references. In the sample, select Table. If you select Remote Database, the only type available will be Database Link. 8. Select the Schema owning the referenced Object. 9. Select the Object to be referenced. If you selected Remote Database, you will see a list of existing database links from which to select. Figure 8-7 shows the selections for creating a public Synonym named
CREATURE for the SEAPARK.AQUATIC_ANIMAL Table in the local database.

Figure 8-7: Schema Managers Create Synonym is ready to complete.

Chapter 8 3 Sequences

197

10. Click Create to complete the definition. You cannot modify or alter Synonyms. The next section shows how to delete Synonyms.

Deleting (dropping) a Synonym


Deleting a Synonym with the Schema Manager is similar to deleting other Objects. Use the following steps to drop a Synonym. 1. Start the Schema Manager and log in as the DBA. 2. Double-click the Synonym folder. A list of Schemas containing Synonyms appears. 3. Double-click the Schema that contains the Synonym to be dropped. If you are dropping a public Synonym, double-click Public. 4. Click the Synonym to be dropped. 5. Click the red X. 6. The Schema Manager verifies you want to drop the Synonym. 7. Click Yes to drop the view. The next section shows how to create and manage sequences for use when assigning sequential numbers to accommodate concurrent Users.

Sequences
A sequence is an Oracle8 Object that delivers a unique number, incremented by one or some specified amount, every time it is requested. Sequences are typically used to generate a Primary Key for a Table or for a set of Tables. The sequence generates a new number each time it is called. The number increments every time, regardless of the state of the transaction, thus guaranteeing unique numbers. For example, User A starts an application that begins building a row to insert in a Table. The application calls a sequence and retrieves the next value from the sequence, 109. User A has not committed the transaction. At the same time, User B creates a new row for the same Table. User B calls the same sequence and receives the number 110. User B commits his record. User A rolls back her transaction. User C inserts a row, calling the same sequence and receiving the number 111. User C commits the row. The Table now has two rows with Primary Keys of 110 and 111. The next time a User calls the sequence, it will send number 112 and so forth.

198
Tip

Chapter 8 3 Schema Manager for Views, Synonyms, and Sequences

A sequence is not directly connected with any Table; instead, sequences simply deliver sequence numbers. Any number of Tables can share the same sequence to retrieve numbers. The Tables only become connected with the sequence indirectly through a Trigger or the use of the NEXTVAL function in the INSERT command.

Creating a new sequence


See Reference Section

CREATE SEQUENCE

A new sequence can easily be created using Schema Manager. Like other Objects, a sequence is owned by a Schema and can be referenced with a Synonym. Only the SELECT Privilege can be granted for a sequence. Granting SELECT on a sequence enables the User to retrieve sequence numbers from the sequence. Follow these steps to create a new sequence using Schema Manager. 1. Start the Schema Manager and log in as the DBA. 2. Click Object Create. A small pop-up menu lists the Objects that you can create. 3. Click Sequence. The Create Sequence window appears as shown in Figure 8-8.

Figure 8-8: Schema Manager creates sequences here.

Chapter 8 3 Sequences

199

4. Type in the name of the sequence in the Name box. 5. Select the Schema in the Schema box. 6. Choose Ascending or Descending order. 7. Type in a minimum value (Optional). The default for ascending sequences is 1. The default for descending sequences is -1026. 8. Type in a maximum value (Optional). Ascending sequences receive a default value of 1027. Descending sequences receive a default value of -1. 9. Type in an increment or decrement value (optional). When you choose Ascending, this field is labeled Increment. If left blank, ascending sequences receive a default value of 1, meaning the sequence increases from the minimum value to the maximum value in increments of 1. Leave this field blank or type in a number. When you choose Descending, this field is labeled Decrement. If left blank, descending sequences receive a default value of -1, meaning the sequence decreases from the maximum value to the minimum value in increments of -1. Leave this field blank or type in a number. 10. Choose Cycle or No Cycle. Check the Cycle Values box in the options area if you want the sequence to start back at the beginning when it reaches the maximum (or minimum for descending) value. Leave the Cycle Values box unchecked to make the sequence stop when it reaches the end. 11. Choose Order or No Order. Check the Order Values box in the Options area to instruct Oracle8 to generate sequence numbers in the requested order. 12. Choose a Cache option. Default. Oracle8 caches 20 numbers. No Cache. Oracle8 does not Cache any sequence numbers. Size. Fill in the number of sequence numbers to be cached. This number cannot exceed the number of sequence numbers spanning the life of the sequence. For example, a sequence that increments by 5 and begins at 5 and ends at 50 cannot cache more than 10 numbers. Figure 8-9 shows a sample sequence called ANIMAL_SEQ.

200

Chapter 8 3 Schema Manager for Views, Synonyms, and Sequences

Figure 8-9: Schema Manager creates sequences here.

13. Click Create to create the sequence. You will return to the main Schema Manager window. Once a sequence is created, you can use it to generate sequence numbers. The next section shows how to modify some parameters of the sequence.

Changing (altering) a sequence


See Reference Section

ALTER SEQUENCE

When changing a sequence, take care to not specify changes that will compromise the uniqueness of the Primary Keys depending on the sequence. Follow these steps to modify an existing sequence using Schema Manager: 1. Start the Schema Manager and log in as the DBA. 2. Double-click the Sequence folder. 3. Double-click the Schema that owns the sequence you want to change. 4. Click the sequence to be changed. The Properties window of the sequence appears in the right frame. 5. Make changes as needed. 6. Click Apply to modify the sequence. You return to the main Schema Manager window. If you need to reset a sequence to a starting number higher than the current value of the sequence, you must drop the sequence and re-create it. The next section shows how to drop a sequence.

Chapter 8 3 Summary

201

Deleting (dropping) a sequence


Deleting a sequence with the Schema Manager is similar to deleting other Objects. Follow these steps to drop a sequence: 1. Start the Schema Manager and log in as the DBA. 2. Double-click the sequence folder. A list of Schemas containing sequences appears. 3. Double-click the Schema that contains the sequence to be dropped. 4. Click the sequence to be dropped. 5. Click the red X. 6. The Schema Manager verifies you want to drop the sequence. 7. Click Yes to drop the sequence. Remember: dropping a sequence causes Oracle8 to remove any privileges granted on the sequence. Synonyms remain but are invalid. Triggers using the dropped sequence remain but issue an error message when used.

Summary
Views help streamline access to the data by hiding complexities of the underlying Tables. Views can also help to separate data for different Users as a security measure. Views can be referenced almost anywhere you reference a Table. Views are stored in the database as an SQL SELECT command and have no data of their own. Synonyms enable developers to reference Tables with shorthand names. Synonyms also allow different applications to reference the same Object with different names. Synonyms can help simplify migration of applications from one Schema or database instance to another. Sequences can quickly and easily assign unique numbers for Primary Keys. With sequences, you do not need to query a Table to find the highest Primary Key. In addition, sequences are safer than other application-generated methods because they handle multiple concurrent Users seamlessly. The next chapter, the first of six chapters covering SQL commands, shows you how to build a query using SQL. The basic SQL concepts used in queries also are used in other SQL commands such as INSERT and UPDATE.

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