
- 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
Get the new record key ID from MySQL insert query?
We can get new record key with the help of LAST_INSERT_ID() function from MySQL. First, we will create a table and for inserting record, we will use LAST_INSERT_ID().
Let us create a table with the help of create command.
The query is as follows −
mysql> create table LastInsertRecordIdDemo -> ( -> id int auto_increment, -> value varchar(100), -> primary key(id) -> ); Query OK, 0 rows affected (0.52 sec)
After creating a table, we will insert records and set it using LAST_INSERT_ID() function.
mysql> insert into LastInsertRecordIdDemo values(1,'Low'); Query OK, 1 row affected (0.10 sec) mysql> insert into LastInsertRecordIdDemo values(LAST_INSERT_ID(),'High'); Query OK, 1 row affected (0.11 sec)
Now we can display all the records with the help of select statement.
The query is as follows −
mysql> select *from LastInsertRecordIdDemo;
The following is the output.
+----+-------+ | id | value | +----+-------+ | 1 | Low | | 2 | High | +----+-------+ 2 rows in set (0.00 sec)
Now to insert records, we need to add 1 in the function LAST_INSERT_ID (). The query is as follows −
mysql> insert into LastInsertRecordIdDemo values(LAST_INSERT_ID()+1,'Medium'); Query OK, 1 row affected (0.08 sec)
Now we can display all records with the help of select statement.
mysql> select *From LastInsertRecordIdDemo;
The following is the output.
+----+--------+ | id | value | +----+--------+ | 1 | Low | | 2 | High | | 3 | Medium | +----+--------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to delete a record with the lowest ID?
- How to get a specific column record from SELECT query in MySQL?
- MySQL query to get max id from varchar type and the values in numeric?
- How to return the nth record from MySQL query?
- Insert record using MySQL SELECT?
- How to get the id after INSERT into MySQL database using Python?
- Get digits from a record in MySQL?
- MySQL query to fecth the domain name from email-id?
- How do I get the id after INSERT into MySQL database in Python?
- MySQL query to find expiry date (record) from the next 2 days?
- MySQL query to select records beginning from a specific id
- Insert record in a MySQL table with Java
- MongoDB query to get record beginning with specific element from an array?
- Getting the next primary key without adding a new record is impossible, isn't it in MYSQL
- MySQL query for INSERT INTO using values from another table?

Advertisements