简体   繁体   中英

MYSQL: Insert one table into the beginning of another and set the auto_increment

I have php-script, which every 30 sec post the

  • current date_time (starts from the 2016-01-27, see below)
  • and corresponding some real-time data (Cian)

to the MYSQL-table A (about 100K columns). The table A looks as follows (sql_id is the AUTO_INCREMENT column number):

在此处输入图片说明

Also, I have the table B (about 100K columns), which consist of the same data as A , but for the previous period (before the 2016-01-27). So, A starts exactly from the moment, then B was finishing.

Finally I need to have one table, with joined data and:

  • name A (it used by php-script and I couldn't change the name inside php)
  • B-data should be before A-data (so, B table should be insert into the benining of A and data will be in chronological sequence)
  • AUTO_INCREMENT sql_id should be continuous and set to the [sql_id (B) + sql_id (A) + 1]. So, the next sql_id for the php-script will automatically be correct

What will be the best way to do that during 30 sec (before script post the next data)?

The solution is. It could be done, all via sql commands. So its very fast. Every step could be executed independently. But if you have a live system with traffic STEP 4 and STEP 5 should be executed in a transaction.

  1. Create a Table with named C with the same structure as A and B
  2. Insert the data of B in C but with primary_key = NULL for auto_inc

    INSERT INTO C (sql_id,date_time,cian) SELECT NULL, date_time, cian FROM B;

  3. Insert the data of A in C but with primary_key = NULL for auto_inc

    INSERT INTO C (sql_id,date_time,cian) SELECT NULL, date_time, cian FROM A;

  4. Delete Table A

    DROP TABLE A;

  5. Rename the Table C to A

    ALTER TABLE C RENAME TO A;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM