获取任何表的当前AUTO_INCREMENT值

时间:2013-04-04 20:55:28

标签: mysql auto-increment

如何获取MySQL中表的当前AUTO_INCREMENT值?

8 个答案:

答案 0 :(得分:502)

您可以使用此查询获取所有表数据:

SHOW TABLE STATUS FROM `DatabaseName` WHERE `name` LIKE 'TableName' ;

您可以使用此查询获取完整的信息:

SELECT `AUTO_INCREMENT`
FROM  INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND   TABLE_NAME   = 'TableName';

答案 1 :(得分:18)

我相信你正在寻找MySQL的LAST_INSERT_ID()功能。如果在命令行中,只需运行以下命令:

LAST_INSERT_ID();

您还可以通过SELECT查询获取此值:

SELECT LAST_INSERT_ID();

答案 2 :(得分:13)

如果您只是想知道数字,而不是在查询中获取它,那么您可以使用:

SHOW CREATE TABLE tablename;

你应该在底部看到auto_increment

答案 3 :(得分:4)

mysqli可执行示例代码:

<?php
    $db = new mysqli("localhost", "user", "password", "YourDatabaseName");
    if ($db->connect_errno) die ($db->connect_error);

    $table=$db->prepare("SHOW TABLE STATUS FROM YourDatabaseName");
    $table->execute();
    $sonuc = $table->get_result();
    while ($satir=$sonuc->fetch_assoc()){
        if ($satir["Name"]== "YourTableName"){
            $ai[$satir["Name"]]=$satir["Auto_increment"];
        }
    }
    $LastAutoIncrement=$ai["YourTableName"];
    echo $LastAutoIncrement;
?>  

答案 4 :(得分:4)

如果手动运行查询, methai 的答案是正确的,那么当2个并发事务/连接在生产中的运行时实际执行此查询时会出现问题(例如)。

在MySQL工作台中手动尝试同时打开2个连接:

CREATE TABLE translation (
  id BIGINT PRIMARY KEY AUTO_INCREMENT
);
# Suppose we have already 20 entries, we execute 2 new inserts:

交易1:

21 = SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES
     WHERE TABLE_SCHEMA = 'DatabaseName' AND TABLE_NAME = 'translation';

insert into translation (id) values (21);

交易2:

21 = SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES
     WHERE TABLE_SCHEMA = 'DatabaseName' AND TABLE_NAME = 'translation';

insert into translation (id) values (21);

# commit transaction 1;
# commit transaction 2;

插入事务1是可以的: 事务2的插入出错:错误代码:1062。重复输入&#39; 21&#39;关键&#39; PRIMARY&#39;。

一个好的解决方案是 jvdub 的答案,因为每个事务/连接,2个插入将是:

交易1:

insert into translation (id) values (null);
21 = SELECT LAST_INSERT_ID();

交易2:

insert into translation (id) values (null);
22 = SELECT LAST_INSERT_ID();

# commit transaction 1;
# commit transaction 2;

但是我们必须在插入之后执行 last_insert_id()! 我们可以重用该id以插入其他需要外键的表中!

此外,我们无法执行以下2个查询:

insert into translation (id) values ((SELECT AUTO_INCREMENT FROM
    information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE()
    AND TABLE_NAME='translation'));

因为我们实际上有兴趣在其他表中获取/重用该ID或返回!

答案 5 :(得分:2)

查询以检查一种给定模式的所有表的AUTO_INCREMENT百分比“使用情况”(类型为 bigint unsigned 的列除外):

SELECT 
  c.TABLE_NAME,
  c.COLUMN_TYPE,
  c.MAX_VALUE,
  t.AUTO_INCREMENT,
  IF (c.MAX_VALUE > 0, ROUND(100 * t.AUTO_INCREMENT / c.MAX_VALUE, 2), -1) AS "Usage (%)" 
FROM 
  (SELECT 
     TABLE_SCHEMA,
     TABLE_NAME,
     COLUMN_TYPE,
     CASE 
        WHEN COLUMN_TYPE LIKE 'tinyint(1)' THEN 127
        WHEN COLUMN_TYPE LIKE 'tinyint(1) unsigned' THEN 255
        WHEN COLUMN_TYPE LIKE 'smallint(%)' THEN 32767
        WHEN COLUMN_TYPE LIKE 'smallint(%) unsigned' THEN 65535
        WHEN COLUMN_TYPE LIKE 'mediumint(%)' THEN 8388607
        WHEN COLUMN_TYPE LIKE 'mediumint(%) unsigned' THEN 16777215
        WHEN COLUMN_TYPE LIKE 'int(%)' THEN 2147483647
        WHEN COLUMN_TYPE LIKE 'int(%) unsigned' THEN 4294967295
        WHEN COLUMN_TYPE LIKE 'bigint(%)' THEN 9223372036854775807
        WHEN COLUMN_TYPE LIKE 'bigint(%) unsigned' THEN 0
        ELSE 0
     END AS "MAX_VALUE" 
   FROM 
     INFORMATION_SCHEMA.COLUMNS
     WHERE EXTRA LIKE '%auto_increment%'
   ) c

   JOIN INFORMATION_SCHEMA.TABLES t ON (t.TABLE_SCHEMA = c.TABLE_SCHEMA AND t.TABLE_NAME = c.TABLE_NAME)

WHERE
 c.TABLE_SCHEMA = 'YOUR_SCHEMA' 
ORDER BY
 `Usage (%)` DESC;

答案 6 :(得分:1)

如果在sql server中自动增加列,则查看当前自动增量值,如果要编辑该列的值,请使用以下查询。

-- to get current value
select ident_current('Table_Name')

-- to update current value
dbcc checkident ('[Table_Name]',reseed,"Your Value")

答案 7 :(得分:0)

我一直在寻找相同的东西,最后在Helper类中创建了一个静态方法(在我的情况下,我将其命名为App \ Helpers \ Database)。

方法

/**
 * Method to get the autoincrement value from a database table
 *
 * @access public
 *
 * @param string $database The database name or configuration in the .env file
 * @param string $table    The table name
 *
 * @return mixed
 */
public static function getAutoIncrementValue($database, $table)
{
    $database ?? env('DB_DATABASE');

    return \DB::select("
        SELECT AUTO_INCREMENT 
        FROM information_schema.TABLES 
        WHERE TABLE_SCHEMA = '" . env('DB_DATABASE') . "' 
        AND TABLE_NAME = '" . $table . "'"
    )[0]->AUTO_INCREMENT;
}

要调用该方法并获取MySql AUTO_INCREMENT,只需使用以下命令:

$auto_increment = \App\Helpers\Database::getAutoIncrementValue(env('DB_DATABASE'), 'your_table_name');

希望有帮助。