Syntax error How to work with auto incrementing column in MySQL?

How to work with auto incrementing column in MySQL?



To work with auto incrementing column, you can set it as AUTO_INCREMENT while creating the table.

Let us first create a table. Here, we have set the Id field as column since that would be our auto increment column −

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> FirstName varchar(20),
   -> LastName varchar(20)
   -> );
Query OK, 0 rows affected (0.71 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(FirstName,LastName) values('John','Smith');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable(FirstName,LastName) values('Chris','Brown');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable(FirstName,LastName) values('Carol','Taylor');
Query OK, 1 row affected (0.21 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+----+-----------+----------+
| Id | FirstName | LastName |
+----+-----------+----------+
| 1  | John      | Smith    |
| 2  | Chris     | Brown    |
| 3  | Carol     | Taylor   |
+----+-----------+----------+
3 rows in set (0.00 sec)
Updated on: 2019-07-30T22:30:26+05:30

126 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements