AUTO_INCREMENT表示該欄位將自動取得值。為了說明這一點,我們建立了一個名為「employees」的表,如下所示:
mysql> Show Create Table employees\G *************************** 1. row *************************** Table: employees Create Table: CREATE TABLE `employees` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(35) DEFAULT NULL, PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec)
從上面的結果集中,我們可以看到id列被賦予了自動遞增選項。現在,當我們在Name欄位中插入值時,id欄位會自動取得值−
mysql> Insert Into employees(Name) Values('Ram'); Query OK, 1 row affected (0.09 sec) mysql> Insert Into employees(Name) Values('Shyam'); Query OK, 1 row affected (0.03 sec) mysql> Insert Into employees(Name) Values('Mohan'); Query OK, 1 row affected (0.04 sec) mysql> Insert Into employees(Name) Values('Sohan'); Query OK, 1 row affected (0.04 sec) mysql> Select * from employees; +----+-------+ | Id | Name | +----+-------+ | 1 | Ram | | 2 | Shyam | | 3 | Mohan | | 4 | Sohan | +----+-------+ 4 rows in set (0.00 sec)
以上是我們如何將AUTO_INCREMENT應用到一列上?的詳細內容。更多資訊請關注PHP中文網其他相關文章!