Chapter 8. SQL Remote Design forAdaptive Ser verEnter prise
Ensuring unique primary keys
Usersat physically distinct sites can each INSERT new rows to a table, so
thereis an obvious problem ensuring that primary key values are kept
unique.
Iftwo users INSERT a row using the same primary key values, the second
INSERTto reach a given database in the replication system will fail. As
SQLRemote is a replication system for occasionally-connected users, there
canbe no locking mechanism across all databases in the installation. Itis
necessaryto design your SQL Remote installation so that primary key errors
donot occur.
Forprimary key errors to be designed out of SQL Remote installations; the
primarykeys of tables that may be modiļ¬ed at more than one site must be
guaranteedunique. Thereare several ways of achieving this goal. This
chapterdescribes a general, economical and reliable method that uses a pool
ofprimary key values for each site in the installation.
Overviewof primar y key
pools Theprimary key pool is a table that holds a set of primary key values for
eachdatabase in the SQL Remote installation. Eachremote user receives
theirown set of primary key values. When a remote user inserts a new row
intoa table, they use a stored procedure to select a valid primary key from
thepool. Thepool is maintained by periodically running a procedure at the
consolidateddatabase that replenishes the supply.
Themethod is described using a simple exampledatabase consisting of sales
representativesand their customers. Thetables are much simpler than you
woulduse in a real database; this allows us to focus just on those issues
importantfor replication.

The primary key pool

Thepool of primary keys is held in a separate table. Thefollowing CREATE
TABLEstatement creates a primary key pool table:
CREATE TABLE KeyPool (
table_name VARCHAR(40) NOT NULL,
value INTEGER NOT NULL,
location VARCHAR(6) NOT NULL,
PRIMARY KEY (table_name, value),
)
go
Thecolumns of this table have the following meanings:
175