ShowCreateTableemployees\G************************ ***1.row******************************Table:employeesCreateTable:CREATETABLE`employees`(`Id`int(11)NOTNULLAUTO_INCREMENT ,`Name`varch"/> ShowCreateTableemployees\G************************ ***1.row******************************Table:employeesCreateTable:CREATETABLE`employees`(`Id`int(11)NOTNULLAUTO_INCREMENT ,`Name`varch">

Home  >  Article  >  Database  >  How do we apply AUTO_INCREMENT to a column?

How do we apply AUTO_INCREMENT to a column?

WBOY
WBOYforward
2023-08-24 10:21:04565browse

How do we apply AUTO_INCREMENT to a column?

AUTO_INCREMENT means that the column will automatically get the value. To illustrate this, we create a table called "employees" as shown below:

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)

From the above result set, we can see that the id column has been given the auto-increment option. Now when we insert a value in the Name field, the id field will automatically get the value −

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)

The above is the detailed content of How do we apply AUTO_INCREMENT to a column?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete