简体   繁体   中英

Auto increment column description in export mysql

I want to export Mysql database using PHP code as mysqldump is not available on my server. I am able to get most of the export file creation similar to phpmyadmin export file. I am stuck at how to add auto increment column name and value similar to phpmyadmin export.

    --
-- AUTO_INCREMENT for table `event`
--
ALTER TABLE `event`
  MODIFY `event_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;

One way is to find autoincrement column name, field type and it's value using different queries. Is there any other way. Let me know. Thanks

The INFORMATION_SCHEMA.COLUMNS table tells you if a column is auto_incrementing.

mysql> create table test.MyTable ( id int auto_increment primary key);

mysql> select * from information_schema.columns where table_schema='test'\G
*************************** 1. row ***************************
           TABLE_CATALOG: def
            TABLE_SCHEMA: test
              TABLE_NAME: MyTable
             COLUMN_NAME: id
        ORDINAL_POSITION: 1
          COLUMN_DEFAULT: NULL
             IS_NULLABLE: NO
               DATA_TYPE: int
CHARACTER_MAXIMUM_LENGTH: NULL
  CHARACTER_OCTET_LENGTH: NULL
       NUMERIC_PRECISION: 10
           NUMERIC_SCALE: 0
      DATETIME_PRECISION: NULL
      CHARACTER_SET_NAME: NULL
          COLLATION_NAME: NULL
             COLUMN_TYPE: int(11)
              COLUMN_KEY: PRI
                   EXTRA: auto_increment                    <-- here
              PRIVILEGES: select,insert,update,references
          COLUMN_COMMENT: 
   GENERATION_EXPRESSION: 
                  SRS_ID: NULL

The value of the table's auto_increment is in INFORMATION_SCHEMA.TABLES.

mysql> insert into test.MyTable () values ();

mysql> select * from information_schema.tables where table_schema='test'\G
*************************** 1. row ***************************
  TABLE_CATALOG: def
   TABLE_SCHEMA: test
     TABLE_NAME: MyTable
     TABLE_TYPE: BASE TABLE
         ENGINE: InnoDB
        VERSION: 10
     ROW_FORMAT: Dynamic
     TABLE_ROWS: 1
 AVG_ROW_LENGTH: 16384
    DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
   INDEX_LENGTH: 0
      DATA_FREE: 0
 AUTO_INCREMENT: 2                                          <-- here
    CREATE_TIME: 2018-09-09 15:31:45
    UPDATE_TIME: 2018-09-09 15:33:29
     CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
       CHECKSUM: NULL
 CREATE_OPTIONS: 
  TABLE_COMMENT: 

By the way, I once tried to do what you're doing, duplicate the behavior of mysqldump in pure PHP code. It's harder than you think. My effort is unfinished, and I'm not likely to work on because I don't use PHP these days. You can take a look: https://github.com/billkarwin/cats-and-dogs

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