Collection Contents Index Publication design for Adaptive Server Anywhere Sharing rows among several subscriptions pdf/chap7.pdf

Data Replication with SQL Remote
   PART 2. Replication Design for SQL Remote
     CHAPTER 7. SQL Remote Design for Adaptive Server Anywhere       

Partitioning tables that do not contain the subscription expression


In many cases, the rows of a table need to be partitioned even when the subscription expression does not exist in the table.

Top of page  The Contact example

The Contact database illustrates why and how to partition tables that do not contain the subscription expression.

Example 

Here is a simple database that illustrates the problem.

Each sales representative sells to several customers. At some customers there is a single contact, while other customers have several contacts.

The tables in the database 

The three tables are described in more detail as follows:

Table

Description

SalesRep

All sales representatives that work for the company. The SalesRep table has the following columns:

  • rep_key     An identifier for each sales representative. This is the primary key.

  • name     The name of each sales representative.

The SQL statement creating this table is as follows:

CREATE TABLE SalesRep (
   rep_key CHAR(12) NOT NULL,
   name CHAR(40) NOT NULL,
   PRIMARY KEY (rep_key)
)

Customer

All customers that do business with the company. The Customer table includes the following columns:

  • cust_key     An identifier for each customer. This is the primary key.

  • name     The name of each customer.

  • rep_key     An identifier for the sales representative in a sales relationship. This is a foreign key to the SalesRep table.

The SQL statement creating this table is as follows:

CREATE TABLE Customer (
   cust_key CHAR(12) NOT NULL,
   name CHAR(40) NOT NULL,
   rep_key CHAR(12) NOT NULL,
   FOREIGN KEY REFERENCES SalesRep,
   PRIMARY KEY (cust_key)
)

Contact

All individual contacts that do business with the company. Each contact belongs to a single customer. The Contact table includes the following columns:

  • contact_key     An identifier for each contact. This is the primary key.

  • name     The name of each contact.

  • cust_key     An identifier for the customer to which the contact belongs. This is a foreign key to the Customer table.

The SQL statement creating this table is:

CREATE TABLE Contact (
   contact_key  CHAR(12) NOT NULL,
   name  CHAR(40) NOT NULL,
   cust_key  CHAR(12) NOT NULL,
   FOREIGN KEY  REFERENCES Customer,
   PRIMARY KEY  (contact_key)
)

Replication goals 

The goals of the design are to provide each sales representative with the following information:

Top of page  Partitioning the Customer table in the Contact example

The Customer table can be partitioned using the rep_key value as a subscription expression. A publication that includes the SalesRep and Customer tables would be as follows:

CREATE PUBLICATION SalesRepData (
   TABLE SalesRep
   TABLE Customer SUBSCRIBE BY rep_key
)

Top of page  Partitioning the Contact table in the Contact example

The Contact table must also be partitioned among the sales representatives, but contains no reference to the sales representative rep_key value. How can the Message Agent match a subscription value against rows of this table, when rep_key is not present in the table?

To solve this problem, you can use a subquery in the Contact article that evaluates to the rep_key column of the Customer table. The publication then looks like this:

CREATE PUBLICATION SalesRepData (
   TABLE SalesRep
   TABLE Customer 
      SUBSCRIBE BY rep_key
   TABLE Contact 
      SUBSCRIBE BY (SELECT rep_key 
         FROM Customer
         WHERE Contact.cust_key = Customer.cust_key )
)

The WHERE clause in the subscription expression ensures that the subquery returns only a single value, as only one row in the Customer table has the cust_key value in the current row of the Contact table.

For Info     For an Adaptive Server Enterprise consolidated database, the solution is different. For more information, see Partitioning tables that do not contain the subscription column.

Top of page  Territory realignment in the Contact example

In territory realignment, rows are reassigned among subscribers. In the present case, territory realignment is the reassignment of rows in the Customer table, and by implication also the Contact table, among the Sales Reps.

When a customer is reassigned to a new sales rep, the Customer table is updated. The UPDATE is replicated as an INSERT or a or a DELETE to the old and new sales representatives, respectively, so that the customer row is properly transferred to the new sales representative.

For Info     For information on the way in which Adaptive Server Anywhere and SQL Remote work together to handle this situation, see Who gets what?.

When a customer is reassigned, the Contact table is unaffected. There are no changes to the Contact table, and consequently no entries in the transaction log pertaining to the Contact table. In the absence of this information, SQL Remote cannot reassign the rows of the Contact table along with the Customer.

This failure will cause referential integrity problems: the Contact table at the remote database of the old sales representative contains a cust_key value for which there is no longer a Customer.

Use triggers to maintain Contacts 

The solution is to use a trigger containing a special form of UPDATE statement, which does not make any change to the database tables, but which does make an entry in the transaction log. This log entry contains the before and after values of the subscription expression, and so is of the proper form for the Message Agent to replicate the rows properly.

The trigger must be fired BEFORE operations on the row. In this way, the BEFORE value can be evaluated and placed in the log. Also, the trigger must be fired FOR EACH ROW rather than for each statement, and the information provided by the trigger must be the new subscription expression. The Message Agent can use this information to determine which subscribers receive which rows.

Trigger definition 

The trigger definition is as follows:

CREATE TRIGGER UpdateCustomer

BEFORE UPDATE ON Customer
REFERENCING NEW AS NewRow
   OLD as OldRow
FOR EACH ROW
BEGIN
   // determine the new subscription expression 
   // for the Customer table
   UPDATE Contact
   PUBLICATION SalesRepData
   OLD SUBSCRIBE BY ( OldRow.rep_key )
   NEW SUBSCRIBE BY ( NewRow.rep_key )
   WHERE cust_key = NewRow.cust_key;
END;

A special UPDATE statement for publications 

The UPDATE statement in this trigger is of the following special form:

UPDATE table-name

PUBLICATION publication-name

{ SUBSCRIBE BY subscription-expression |

OLD SUBSCRIBE BY old-subscription-expression

NEW SUBSCRIBE BY new-subscription-expression }

WHERE search-condition

Notes on the trigger 

Information in the transaction log 

Here we describe the information placed in the transaction log. Understanding this helps in designing efficient publications.

Top of page  

Collection Contents Index Publication design for Adaptive Server Anywhere Sharing rows among several subscriptions pdf/chap7.pdf