[+/-]
Starting with MySQL 5.1.21, source and target tables for replication do not have to be identical. A table on the master can have more or fewer columns than the slave's copy of the table. In addition — subject to certain conditions — corresponding table columns on the master and the slave can use different data types.
In all cases where the source and target tables do not have identical definitions, the following must be true in order for replication to work:
You must be using row-based replication. (Using
MIXED
for the binary logging format
does not work.)
The database and table names must be the same on both the master and the slave.
Additional conditions are discussed (and examples provided) in the following two sections.
Starting with MySQL 5.1.21, you can replicate a table from the master to the slave such that the master's copy of the table and the slave's copy of the table do not have the same number of columns, subject to the following conditions:
Each “extra” column in the version of the table having more columns must have a default value.
A column's default value is determined by a
number of factors, including its type, whether it is
defined with a DEFAULT
option,
whether it is declared as NULL
, and
the server SQL mode in effect at the time of its
creation; see Section 10.1.4, “Data Type Default Values”),
for more information.
Matching columns must be defined in the same order on both the master and the slave.
Any additional columns must be defined following the matching columns.
In addition, when the slave's copy of the table has more columns than the master's copy, then each matching column must use the same data type.
Examples. The following examples illustrate some valid and invalid table definitions:
More columns on the master. The following table definitions are valid:
master>CREATE TABLE t1 (c1 INT, c2 INT, c3 INT);
slave>CREATE TABLE t1 (c1 INT, c2 INT);
The following table definitions would raise Error 1532 (ER_BINLOG_ROW_RBR_TO_SBR) because the definitions of the columns common to both versions of the table are in a different order on the slave than they are on the master:
master>CREATE TABLE t1 (c1 INT, c2 INT, c3 INT);
slave>CREATE TABLE t1 (c2 INT, c1 INT);
The following table definitions would also raise Error 1532, because the definition of the extra column on the master appears before the definitions of the columns common to both versions of the table:
master>CREATE TABLE t1 (c3 INT, c1 INT, c2 INT);
slave>CREATE TABLE t1 (c1 INT, c2 INT);
More columns on the slave. The following definitions replicate correctly:
master>CREATE TABLE t1 (c1 INT, c2 INT);
slave>CREATE TABLE t1 (c1 INT, c2 INT, c3 INT);
The following definitions raise Error 1532 because the columns common to both versions of the table are not defined in the same order on both the master and the slave:
master>CREATE TABLE t1 (c1 INT, c2 INT);
slave>CREATE TABLE t1 (c2 INT, c1 INT, c3 INT);
The following table definitions also raise Error 1532 because the definition for the extra column in the slave's version of the table appears before the definitions for the columns which are common to both versions of the table:
master>CREATE TABLE t1 (c1 INT, c2 INT);
slave>CREATE TABLE t1 (c3 INT, c1 INT, c2 INT);
The following table definitions fail, because the
slave's version of the table has additional
columns compared to the master's version, and
the two versions of the table define column
c2
as a different data type.
master>CREATE TABLE t1 (c1 INT, c2 BIGINT);
slave>CREATE TABLE t1 (c1 INT, c2 INT, c3 INT);
Corresponding columns on the master's and the slave's copies of the same table ideally should have the same data type. However, beginning with MySQL 5.1.21, this is not always strictly enforced, as long as certain conditions are met. These conditions are listed here:
The slave's copy of the table cannot contain more columns than the master's copy.
For columns holding numeric data types, the sizes may differ, as long as the size of the slave's version of the column is equal or greater to the size of the master's version of the column. This is sometimes referred to as attribute promotion, because the data type of the master's version of the column is promoted to a type that is the same size or larger on the slave.
Data type conversions currently supported by attribute
promotion are shown in the following table, in which
X
and
N
both represent positive
integers.
Original Data Type | Promoted Data Type(s) |
---|---|
CHAR( |
CHAR( |
VARCHAR( |
VARCHAR( |
CHAR( |
VARCHAR( |
BINARY( |
BINARY( |
VARBINARY( |
VARBINARY( |
BINARY( |
VARBINARY( |
BIT( |
BIT( |
TINYINT |
SMALLINT ,
MEDIUMINT ,
INT , or
BIGINT
|
SMALLINT |
MEDIUMINT ,
INT , or
BIGINT
|
MEDIUMINT |
INT or
BIGINT
|
INT |
BIGINT |
Unsigned integer columns can be promoted to larger
unsigned types; for example, a column declared as
TINYINT UNSIGNED
can be restored to a
column declared as SMALLINT UNSIGNED
,
MEDIUMINT UNSIGNED
, INT
UNSIGNED
, or BIGINT
UNSIGNED
. You cannot promote a signed column
to an unsigned type, or an unsigned column to a signed
type.
For columns holding numeric data types the sizes may differ, as long as the size of the slave's version of the column is equal or greater to the size of the master's version of the column. For example, the following table definitions are allowed:
master>CREATE TABLE t1 (c1 TINYINT, c2 INT);
slave>CREATE TABLE t1 (c1 INT, c2 INT);
The slave's versions of both columns
c1
and c2
are the
same size as or larger than the master's versions
of these columns. However, the following definitions
would fail:
master>CREATE TABLE t1 (c1 INT, c2 FLOAT(8,3));
slave>CREATE TABLE t1 (c1 INT, c2 FLOAT(7,3));
In this case, Error 1532 would be raised because the
master's copy of column c2
is
larger than its counterpart on the slave — that
is, the master's copy of c2
on
the master can hold more digits than the slave's
copy of the column.
There is no conversion between integer
(TINYINT
,
SMALLINT
,
MEDIUMINT
, and so on) and
noninteger (FLOAT
,
DOUBLE
,
DECIMAL
, and so on)
numeric data types, and so the following definitions
would fail with Error 1532:
master>CREATE TABLE t1 (c1 INT, c2 FLOAT(8,3));
slave>CREATE TABLE t1 (c1 INT, c2 BIGINT);
A column using a noninteger numeric data type must always have the same definition on both the master and the slave.
For columns storing CHAR
and BINARY
data, the size
of the slave's copy of the column must be equal to
or greater than the size of the master's copy. For
example, the following table definitions would replicate
successfully:
master>CREATE TABLE t1 (c1 INT, c2 CHAR(30));
slave>CREATE TABLE t1 (c1 INT, c2 CHAR(50));
If the size of the master's version of the column is greater than that of the slave's version of the column, replication fails with Error 1532.
The replication process can convert freely between
BINARY
,
VARBINARY
,
CHAR
and
VARCHAR
columns, as long
as the slave's version of the column is the same
size as or larger than the master's version. For
example, the following table definitions can be used
successfully:
master>CREATE TABLE t1 (c1 INT, c2 VARBINARY(30));
slave>CREATE TABLE t1 (c1 INT, c2 CHAR(30));
Since replication is currently not supported between different character sets, it is sufficient when comparing sizes of columns containing character data to count the number of characters rather than the number of bytes.
Attribute promotion can be used with both statement-based and row-based replication, and is not dependent on the storage engine used by either the master or the slave.
User Comments
Add your own comment.