Documente Academic
Documente Profesional
Documente Cultură
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
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.
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.
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.
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
190
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
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.
192
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.
Chapter 8 3 Synonyms
193
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
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.
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.
196
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.
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.
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
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.
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.
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
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.
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
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.