Is there a need to insert auto_increment column values in MySQL while using INSERT statement?

MySQLMySQLi Database

No, there’s no need to insert auto_increment column values, since it begins from 1 and inserts on its own. This is because we have set it as auto increment. Let us first create a table −

mysql> create table DemoTable
(
   EmployeeId int NOT NULL AUTO_INCREMENT,
   EmployeeName varchar(30),
   EmployeeSalary int,
   PRIMARY KEY(EmployeeId)
);
Query OK, 0 rows affected (0.49 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('Chris',56789);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('David',78909);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('Mike',100056);
Query OK, 1 row affected (0.39 sec)
mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('Bob',150000);
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output. Here, you can see the id value gets added on its own since we have set it as auto_increment −

+------------+--------------+----------------+
| EmployeeId | EmployeeName | EmployeeSalary |
+------------+--------------+----------------+
|          1 | Chris        |          56789 |
|          2 | David        |          78909 |
|          3 | Mike         |         100056 |
|          4 | Bob          |         150000 |
+------------+--------------+----------------+
4 rows in set (0.00 sec)
raja
Published on 07-Oct-2019 15:02:11
Advertisements