LAST_INSERT_ID()
LAST_INSERT_ID() is a SQL function in the MySQL Specific category. Returns the AUTO_INCREMENT value generated by the most recent INSERT in the current connection. The syntax is LAST_INSERT_ID(). It accepts none. It returns integer ID. A typical example: INSERT INTO users (name) VALUES ('Alice'); SELECT LAST_INSERT_ID(); -- Returns the new user's ID A close relative is AUTO_INCREMENT, which mySQL auto-generates sequential integer values for this column. Commonly used with PRIMARY KEY. A close relative is ENGINE, which specifies the MySQL storage engine. InnoDB (default) supports transactions and foreign keys. MyISAM is faster for read-heavy. A close relative is ON DUPLICATE KEY UPDATE, which mySQL extension: if INSERT causes a duplicate key error, it performs an UPDATE instead. More about this category: MySQL-specific features — AUTO_INCREMENT, ENGINE, SHOW TABLES, LIMIT. Related MySQL Specific entries: AUTO_INCREMENT, ENGINE, ON DUPLICATE KEY UPDATE, LOAD DATA INFILE, LOCK TABLES / UNLOCK TABLES, FIND_IN_SET(), CHARACTER SET / COLLATION, SELECT ... FOR UPDATE (MySQL).