开发者社区> 问答> 正文

Mysql的 auto_increment 使用问题

我建表时将id设为了 auto increment , 后来有我使用时发现id应该是从1开始的,但是结果却是从2开始递增,我是使用的C++,下面的是我部分代码:

        sql_->SetAutoCommit(0);

        if(sql_->InsertUserLoginTable(id,"") == -1)
        {
            result = "regist faild1";
        }
        else
        {
            std::string ID = sql_->GetLastID();
            std::cout << "ID : " << ID << std::endl;
            if (sql_->InsertLoginInfoTable(ID, "", "", "", IMEI, BTMAC) == -1)
            {
                result = "regist faild2";
            }
            else if (sql_->InsertUserInfoTable(ID, FaceID, NickName, "", "", "3") == -1)
            {
                result = "regist faild3";
            }
            else if (sql_->InsertUserRecordTable(ID, RegistIP) == -1)
            {
                result = "regist faild4";
            }
            else
            {
                result = "regist success";
            }
        }

        if(result == "regist success")
        {
            sql_->Commit(0);

        }
        else
        {
            sql_->Commit(1);
        }
        sql_->SetAutoCommit(1);

其中有一些是我自己封装的方法,部分如下:
void SetAutoCommit(int i)
{
sql->AutoCommit(i);
}
int Commit(int i)
{
sql->CommitSQL(i);
}
void AutoCommit(int i)
{
mysql_autocommit(connection_,i);
}
int CommitSQL(int i)
{
if(i == 1)
{
mysql_rollback(connection_);
return -1;
}
else
{
mysql_commit(connection_);
return 0;
}
}

展开
收起
a123456678 2016-03-06 18:29:34 2347 0
2 条回答
写回答
取消 提交回答
  • 自增ID肯定是按顺序自增的,但是如果你删除了其中的记录,再插入的话,自增ID是跳过的

    2021-10-12 13:44:00
    赞同 展开评论 打赏
  • 在使用MySQL中,经常会在表中建立一个自增的ID字段,利用自增ID能够快速建立索引,也是MySQL官方比较推荐的一种方式,但是,这种方式在大量数据且配置主从时,可能会出现由于自增ID导致同步失败的情况
    首先需要了解一点

    Mysql主从同步主要通过bin log来同步,而MySQl中bin log最大值为1G,

    2019-07-17 18:55:02
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像