
- 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
The equivalent of SQL Server function SCOPE_IDENTITY() in MySQL?
The equivalent of SQL Server function SCOPE_IDENTITY() is equal to LAST_INSERT_ID() in MySQL. The syntax is as follows:
SELECT LAST_INSERT_ID().
This returns the id of last inserted record.
Here, I am going to create a table with primary key column. The following is the demo of last_insert_id().
First, let us create two tables. The query to create the first table table is as follows:
<TestOnLastInsertIdDemo>
mysql> create table TestOnLastInsertIdDemo -> ( -> StudentId int NOT NULL AUTO_INCREMENT, -> PRIMARY KEY(StudentId) -> ); Query OK, 0 rows affected (0.95 sec)
Now creating the second table. The query is as follows:
<TestOnLastInsertIdDemo2>
mysql> create table TestOnLastInsertIdDemo2 -> ( -> Id int NOT NULL AUTO_INCREMENT, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (2.79 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into TestOnLastInsertIdDemo2 values(),(),(),(),(),(),(),(); Query OK, 8 rows affected (0.21 sec) Records: 8 Duplicates: 0 Warnings: 0
Now create a trigger on table ‘TestOnLastInsertIdDemo2’. The query to create a table is as follows:
mysql> delimiter // mysql> create trigger insertingTrigger after insert on TestOnLastInsertIdDemo -> for each row begin -> insert into TestOnLastInsertIdDemo2 values(); -> end; -> // Query OK, 0 rows affected (0.19 sec) mysql> delimiter ;
If you will insert the record in the TestOnLastInsertIdDemo table, the last_insert_id() returns 1. The query to insert record is as follows:
mysql> insert into TestOnLastInsertIdDemo values(); Query OK, 1 row affected (0.31 sec)
Use the function last_insert_id(). The query is as follows:
mysql> select last_insert_id();
The following is the output:
+------------------+ | last_insert_id() | +------------------+ | 1 | +------------------+ 1 row in set (0.00 sec)
In the above sample output, it gives 1 because the last_insert_id() uses the original table only, not inside trigger table.
- Related Articles
- Equivalent of SQL Server IDENTITY Column in MySQL?
- MySQL LIMIT clause equivalent for SQL SERVER?
- What is the C# Equivalent of SQL Server DataTypes?
- Does SQL Server have an equivalent to MySQL's ENUM data type?
- Difference between MySQL and SQL Server
- How to write a MySQL “LIMIT” in SQL Server?
- Database Wars: MSSQL Server, Oracle PL/SQL and MySQL
- What is the equivalent of SQL “like” in MongoDB?
- Mean and Mode in SQL Server
- Check Constraint in MS SQL Server
- Order by in MS SQL Server
- Offset-Fetch in MS SQL Server
- SQL Query to Convert Rows to Columns in SQL Server
- Generate table DDL via a query on MySQL and SQL Server?
- MS SQL Server - Type Conversion
