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

How do we apply AUTO_INCREMENT to a column?

WBOY
Release: 2023-08-24 10:21:04
forward
584 people have browsed it

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template