Documentation Home
MySQL 9.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 40.0Mb
PDF (A4) - 40.1Mb
Man Pages (TGZ) - 259.0Kb
Man Pages (Zip) - 366.2Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb


19.5.1.1 Replication and AUTO_INCREMENT

Statement-based replication ofAUTO_INCREMENT,LAST_INSERT_ID(), andTIMESTAMP values is carried out subject to the following exceptions:

  • A statement invoking a trigger or function that causes an update to anAUTO_INCREMENT column is not replicated correctly using statement-based replication. These statements are marked as unsafe. (Bug #45677)

  • AnINSERT into a table that has a composite primary key that includes anAUTO_INCREMENT column that is not the first column of this composite key is not safe for statement-based logging or replication. These statements are marked as unsafe. (Bug #11754117, Bug #45670)

    This issue does not affect tables using theInnoDB storage engine, since anInnoDB table with anAUTO_INCREMENT column requires at least one key where the auto-increment column is the only or leftmost column.

  • Adding anAUTO_INCREMENT column to a table withALTER TABLE might not produce the same ordering of the rows on the replica and the source. This occurs because the order in which the rows are numbered depends on the specific storage engine used for the table and the order in which the rows were inserted. If it is important to have the same order on the source and replica, the rows must be ordered before assigning anAUTO_INCREMENT number. Assuming that you want to add anAUTO_INCREMENT column to a tablet1 that has columnscol1 andcol2, the following statements produce a new tablet2 identical tot1 but with anAUTO_INCREMENT column:

    CREATE TABLE t2 LIKE t1;ALTER TABLE t2 ADD id INT AUTO_INCREMENT PRIMARY KEY;INSERT INTO t2 SELECT * FROM t1 ORDER BY col1, col2;
    Important

    To guarantee the same ordering on both source and replica, theORDER BY clause must nameall columns oft1.

    The instructions just given are subject to the limitations ofCREATE TABLE ... LIKE: Foreign key definitions are ignored, as are theDATA DIRECTORY andINDEX DIRECTORY table options. If a table definition includes any of those characteristics, createt2 using aCREATE TABLE statement that is identical to the one used to createt1, but with the addition of theAUTO_INCREMENT column.

    Regardless of the method used to create and populate the copy having theAUTO_INCREMENT column, the final step is to drop the original table and then rename the copy:

    DROP t1;ALTER TABLE t2 RENAME t1;

    See alsoSection B.3.6.1, “Problems with ALTER TABLE”.