
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Set AUTO_INCREMENT in a table while creating it in MySQL?
Let us first create a table. We have used AUTO_INCREMENT while creating the table to set auto increment for StudentId −
mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT, -> StudentFirstName varchar(100), -> StudentLastName varchar(100), -> StudentAge int, -> StudentCountryName varchar(100), -> PRIMARY KEY(StudentId) -> )AUTO_INCREMENT=30; Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentFirstName,StudentLastName,StudentAge,StudentCountryName) values('John','Smith',21,'US'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentFirstName,StudentLastName,StudentAge,StudentCountryName) values('Chris','Brown',20,'AUS'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+-----------+------------------+-----------------+------------+--------------------+ | StudentId | StudentFirstName | StudentLastName | StudentAge | StudentCountryName | +-----------+------------------+-----------------+------------+--------------------+ | 30 | John | Smith | 21 | US | | 31 | Chris | Brown | 20 | AUS | +-----------+------------------+-----------------+------------+--------------------+ 2 rows in set (0.00 sec)
- Related Articles
- Set DEFAULT values for columns while creating a table in MySQL
- Set auto increment initial value for MySQL table using ALTER command
- Truncate a MySQL table and then set a custom value to auto increment
- Set custom Auto Increment with ZEROFILL in MySQL
- What if I forgot to set Auto Increment? Can I set it later in MySQL?
- How to set initial value and auto increment in MySQL?
- How to make MySQL table primary key auto increment?
- Set options while creating a MySQL table. Display the same options as well
- How to create a table with auto-increment column in MySQL using JDBC?
- Can we use {} while creating a MySQL table?
- How to set auto-increment to an existing column in a table using JDBC API?
- Error occurs when table name “references” is set while creating a table
- What is MySQL GENERATED COLUMN and how to use it while creating a table?
- Change the Auto Increment counter in MySQL?
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?

Advertisements