插入到标识属性,产生意外结果


Inserts to identity attribute producing unexpected results

>我有一个表格

MovieInfo
M_ID int idenity
title varchar
yearReleased int    
Primary key ( title, yearReleased)

我的php文件将值插入表中:

try{
     $host = new PDO('sqlsrv:server=localhost;Database='.$database, $username, $password);
        $movieTitle = $_POST['title'];
        $movieYear = $_POST['year'];

        $stmt = "INSERT INTO MovieInfo (title, yearReleased) VALUES (:title, :year)";
        $q = $host->prepare($stmt);
        $q->execute(array(':title'=>$movieTitle, ':year'=>$movieYear));
        $host = null;
    }
catch(PDOException $e)
{
    die('Connect not connect: '.$e->getMessage());
}

我的表填充正常,但自动更新的M_ID不像 1,2,3,4,5 那样增量更新。相反,我对该列的值是 1,3,4,6,7,10,11。我很好奇这是意料之中的还是有什么不同。如果我尝试插入重复项,它不会运行,这是预期的。似乎每次M_ID都在增加,即使在失败的插入中也是如此。

是的,这就是 IDENTITY 的工作方式。 无论 INSERT 语句是成功还是失败,该值都会递增。