16.2 Column Groups with DBMS_REPCATColumn groups provide a mechanism for guaranteeing data consistency across one or more columns in a replicated table. Every replicated table has at least one column group, called the default column group, which Oracle creates automatically. You can also create your own column groups (and you probably should) in which you group logically related fields. 16.2.1 About Column GroupsSuppose that you have a customer table that contains address information, such as street address, city, state, and postal code, plus personal information such as last name, marital status, birth date, and phone number. SQL> desc customer Name Null? Type --------------- -------- ------ CUSTOMER_ID NUMBER(6) NAME VARCHAR2(30) MARITAL_STATUS VARCHAR2(1) PHONE_NUMBER VARCHAR2(16) STREET_ADDR VARCHAR2(30) CITY VARCHAR2(30) STATE VARCHAR2(30) POSTAL_CODE VARCHAR2(12) TIMESTAMP DATE GLOBAL_NAME VARCHAR2(30) In this table, the fields pertaining to the customer's address (i.e., STREET_ADDR, CITY, STATE, and POSTAL_CODE) are logically related. You would not want to allow an update at one site to set the CITY to "San Francisco" and an update at another site to set the STATE to "Mississippi" since (as of this writing) there is no such municipality as San Francisco, Mississippi. Oracle's answer to this potential catastrophe is the column group. A column group is a logical grouping of columns whose collective values are treated as a unit. If we create a column group and add the address-related fields STREET_ADDR, CITY, STATE, and POSTAL_CODE, we can be sure that rows in this table will always contain consistent values for these columns. We can also make a second column group consisting of the fields NAME, MARITAL_STATUS, and PHONE_NUMBER. Note that a row in this table could contain address information that was entered at one site, and name information that was entered at another site. As we shall see in the later section "Built-in Resolution Techniques," every column group needs to have a "governing" column that determines which data is to be considered correct. For example, if you want to use the Latest Timestamp resolution method for a given column group, then your table should include a DATE field, and your application should update this field with the current time whenever it performs inserts or updates on the table.
The procedures you'll use to create and maintain column groups follow:
16.2.2 Creating and Dropping Column GroupsThe DEFINE_COLUMN_GROUP, DROP_COLUMN_GROUP, and MAKE_COLUMN_GROUP procedures are used to create and drop column groups. The difference between DEFINE_COLUMN_GROUP and MAKE_COLUMN_GROUP is that the former creates a column group with no member columns, and the latter both creates the group and adds columns to it. 16.2.2.1 The DBMS_REPCAT.DEFINE_COLUMN_GROUP procedureThe DEFINE_COLUMN_GROUP procedure creates a column group with no member columns. Here's the specification: PROCEDURE DBMS_REPCAT.DEFINE_COLUMN_GROUP (sname IN VARCHAR2, oname IN VARCHAR2, column_group IN VARCHAR2, comment IN VARCHAR@ := NULL); Parameters are summarized in the following table.
16.2.2.1.1 ExceptionsThe DEFINE_COLUMN_GROUP procedure may raise the following exceptions:
16.2.2.1.2 RestrictionsNote the following restrictions on calling the DEFINE_COLUMN_GROUP:
16.2.2.1.3 ExampleThe DEEFINE_COLUMN_GROUP creates an empty column group -- that is, one with no members. After creating the column group, you can add columns to it with the DBMS_REPCAT.ADD_GROUPED_COLUMN procedure described later in this chapter. The following example creates an empty column group for table SPROCKET.PRODUCTS: BEGIN DBMS_REPCAT.DEFINE_COLUMN_GROUP(sname=> 'SPROCKET', oname => 'PRODUCTS ', column_group => 'CG_PRODUCTS_PRICE_COLS', comment => 'Comment added on '||sysdate|| ' by ' ||user); END; 16.2.2.2 The DBMS_REPCAT.DROP_COLUMN_GROUP procedureThe DROP_COLUMN_GROUP procedure drops a column group that you've previously created. Here's the specification: PROCEDURE DBMS_REPCAT.DROP_COLUMN_GROUP (sname IN VARCHAR2, oname IN VARCHAR2, column_group IN VARCHAR2); Parameters are summarized in the following table.
16.2.2.2.1 ExceptionsThe DROP_COLUMN_GROUP procedure may raise the following exceptions:
16.2.2.2.2 RestrictionsNote the following restrictions on calling DROP_COLUMN_GROUP:
16.2.2.2.3 ExampleThis example drops the column group CG_PRODUCTS_PRICE_COLS that was created in the CREATE_COLUMN_GROUP example: BEGIN DBMS_REPCAT.DROP_COLUMN_GROUP(sname > 'SPROCKET', oname => 'PRODUCTS ', column_group => 'CG_PRODUCTS_PRICE_COLS'); END; 16.2.2.3 The DBMS_REPCAT.MAKE_COLUMN_GROUP procedureThe MAKE_COLUMN_GROUP procedure creates a column group and adds member columns to it. Here's the specification: PROCEDURE DBMS_REPCAT.MAKE_COLUMN_GROUP (sname IN VARCHAR2, oname IN VARCHAR2, column_group IN VARCHAR2, {list_of_column_names IN VARCHAR2 | list_of_column_names IN dbms_repcat.varchar2s} ); Note that you must specify only one of the list_of_column_names parameters. Parameters are summarized in the following table.
16.2.2.3.1 ExceptionsThe MAKE_COLUMN_GROUP procedure may raise the following exceptions:
16.2.2.3.2 RestrictionsNote the following restrictions on calling MAKE_COLUMN_GROUP:
16.2.2.3.3 ExampleBy passing "*" to the list_of_column_names parameter in MAKE_COLUMN_GROUP, you can create a column group consisting of all columns in the table. BEGIN DBMS_REPCAT.MAKE_COLUMN_GROUP(sname=> 'SPROCKET', oname => 'PRODUCTS ', column_group => 'CG_PRODUCTS_ALL_COLS', list_of_column_names => '*'); END; You can also use MAKE_COLUMN_GROUP to create a column group containing whatever subset of columns you want. BEGIN DBMS_REPCAT.MAKE_COLUMN_GROUP( sname => 'SPROCKET', oname => 'PRODUCTS ', column_group => 'CG_PRODUCTS_MFG_COLS', list_of_column_names=> 'REV_LEVEL, PRODUCTION_DATE, PRODUCTION_STATUS'); END; 16.2.3 Modifying Existing Column GroupsOnce you have created a column group, you can add and remove member columns (with the ADD_GROUPED_COLUMN and DROP_GROUPED_COLUMN procedures), and you can add or change the comment associated with the group (with the COMMENT_ON_COLUMN_GROUP procedure). 16.2.3.1 The DBMS_REPCAT.ADD_GROUPED_COLUMN procedureThe ADD_GROUPED_COLUMN procedure adds a member column to a column group. You can call this procedure after you have created a new, empty column group with DBMS_REPCAT.DEFINE_COLUMN_GROUP, or if your schema or conflict resolution requirements change. Here's the specification: PROCEDURE DBMS_REPCAT.ADD_GROUPED_COLUMN (sname IN VARCHAR2, oname IN VARCHAR2, column_group IN VARCHAR2, {list_of_column_names IN VARCHAR2 | list_of_column_names IN dbms_repcat.varchar2s}); Note that you must specify only one of the list_of_column_names parameters. Parameters are summarized in the following table.
16.2.3.1.1 ExceptionsADD_GROUPED_COLUMN may raise the following exceptions:
16.2.3.1.2 RestrictionsNote the following restrictions on calling ADD_GROUPED_COLUMN:
16.2.3.1.3 ExampleIn this example, we add the columns CATALOG_ID and DESCRIPTION to the column group CG_PRODUCT_MFG_COLS that we created in the MAKE_COLUMN_GROUP example: DECLARE cg_list DBMS_REPCAT.VARCHAR2(s); BEGIN cg_list(1) := 'CATALOG_ID'; cg_list(1) := 'DESCRIPTION'; DBMS_REPCAT.ADD_GROUPED_COLUMN(sname=> 'SPROCKET', oname => 'PRODUCTS ', column_group => 'CG_PRODUCT_MFG_COLS', list_of_column_names => cg_list); END; 16.2.3.2 The DBMS_REPCAT.DROP_GROUPED_COLUMN procedureThe DROP_GROUPED_COLUMN procedure allows you to drop a column from a column group. Dropping a column from a column group is quite similar to adding one. Make sure, however, that none of your conflict resolution methods reference the column(s) that you are dropping. And as with the other procedures with a "list_of_column_names" parameter, you can pass "*" to the parameter to indicate all fields in table oname. Here's the specification: PROCEDURE DBMS_REPCAT.DROP_GROUPED_COLUMN (sname IN VARCHAR2, oname IN VARCHAR2, column_group IN VARCHAR2, {list_of_column_names IN VARCHAR2 | list_of_column_names IN dbms_repcat.varchar2s}); Note that you must specify only one of the list_of_column_names parameters. Parameters are summarized in the following table.
16.2.3.2.1 ExceptionsThe DROP_GROUPED_COLUMN procedure may raise the following exceptions:
16.2.3.2.2 RestrictionsNote the following restrictions on calling DROP_GROUPED_COLUMN:
16.2.3.2.3 ExampleThe following example shows how to drop a column from an existing column group: BEGIN DBMS_REPCAT.DROP_GROUPED_COLUMN( sname => 'SPROCKET', oname => 'PRODUCTS', column_group => 'CG_PRODUCT_MFG_COLS', list_of_column_names => 'CATALOG_ID, DESCRIPTION'); END; 16.2.3.3 The DBMS_REPCAT.COMMENT_ON_COLUMN_GROUP procedureThe COMMENT_ON_COLUMN_GROUP procedure adds or changes the comment associated with a column group. Here's the specification: PROCEDURE DBMS_REPCAT.COMMENT_ON_COLUMN_GROUP (sname IN VARCHAR2, oname IN VARCHAR2, column_group IN VARCHAR2, comment IN VARCHAR2); Parameters are summarized in the following table.
16.2.3.3.1 ExceptionsThe COMMENT_ON_COLUMN_GROUP procedure may raise the following exceptions:
16.2.3.3.2 RestrictionsThe COMMENT_ON_COLUMN_GROUP procedure must be called from the master definition site. 16.2.3.3.3 ExampleYou can create or change the comment field in DBA_REPCOLUMN_GROUP with the COMMENT_ON_COLUMN_GROUP procedure, as the following example illustrates: BEGIN DBMS_REPCAT.COMMENT_ON_COLUMN_GROUP( sname => 'SPROCKET', oname => 'PRODUCTS', column_group => 'CG_PRODUCT_MFG_COLS', comment => 'Added catalog_id + desc on '||sysdate); Copyright (c) 2000 O'Reilly & Associates. All rights reserved. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|