php中mysql自增 MySQL的自增欄位

2021-10-17 21:56:09 字數 1523 閱讀 8655

1.關鍵字 auto_increment 2.自增用法 例: create table animals ( id mediumint not null auto_increment, name char(30) not nu

1.關鍵字

auto_increment

2.自增用法

例:create table animals ( id mediumint not null auto_increment,

name char(30) not null,

primary key (id));

3.關於自增

q:怎麼獲得當前的自增的最大值?

a:select @@identity

q:怎麼獲得table的當前自增最大值?

a:select max(id) from table

q:對自增的理解?

a: 一般情況下獲取剛插入的資料的id,使用select max(id) from table 是可以的。last_insert_id 是與table無關的,如果向表a插入資料後,再向表b插入資料,last_insert_id會改變。

使用單insert語句插入多條記錄, last_insert_id返回乙個列表。

@@identity是表示的是最近一次向具有identity屬性(即自增列)的表插入資料時對應的自增列的值,是系統定義的全域性變數。比如有個表a,它的自增列是id,當向a表插入一行資料後,如果插入資料後自增列的值自動增加至101,則通過select@@identity得到的值就是101。

注:last_insert_id是乙個函式.

用法:last_insert_id()

q:mysql中的last_insert_id()和mssql中的@@identity

a:按照應用需要,常常要取得剛剛插入資料庫表裡的記錄的id值。

在mysql中可以使用last_insert_id()函式,,在mssql中使用@@identity。挺方便的乙個函式。

但是,這裡需要注意的是,當使用insert語句插入多條記錄的時候,使用last_insert_id()返回的還是第一條的id值,而@@identity返回最後一條。

q:mysql_insert_id()與last_insert_id()

a:mysql_insert_id() 將 mysql 內部的 c api 函式 mysql_insert_id() 的返回值轉換成 long(php中命名為int)。如果 auto_increment 的列的型別是 bigint,則 mysql_insert_id() 返回的值將不正確。可以在 sql查詢中用 mysql 內部的 sql 函式 last_insert_id() 來替代。

mysql的last_insert_id()的介紹 mysql_insert_id()就是呼叫last_insert_id()來實現的。

在mysql中用last_insert_id()....在程式中用mysql_insert_id().

PHP自增自減

寫出一下php段的輸出結果 count 5 function get count echo count count echo get count echo get count 答案為 501 主要涉及到兩個知識點 1.php變數的作用域 2.自增 自減變數 面試人對 php變數的作用域 理解挺到位,...

mysql 實現id自增序列 mysql自增id列

如果希望在每次插入新記錄時,自動地建立主鍵欄位的值。可以在表中建立乙個 auto increment 字段。mysql 使用 auto increment 關鍵字來執行 auto increment 任務。預設地auto increment 的開始值是 1,每條新記錄遞增 1。主鍵又稱主關鍵字,主關...

mysql 主鍵自增語句 MySQL 自增主鍵

以下僅考慮 innodb 儲存引擎。自增主鍵有兩個性質需要考慮 單調性每次插入一條資料,其 id 都是比上一條插入的資料的 id 大,就算上一條資料被刪除。連續性插入成功時,其資料的 id 和前一次插入成功時資料的 id 相鄰。自增主鍵的單調性 為何會有單調性的問題?這主要跟自增主鍵最大值的獲取方式...