본문 바로가기
  • AI (Artificial Intelligence)
Skills/Database

[MySQL] 8.1.1 Obtaining Auto-Increment Values

by 로샤스 2017. 7. 21.

Obtaining the value of column that uses AUTO_INCREMENT after an INSERT statement can be achieved in a number of different ways. To obtain the value immediately after an INSERT, use a SELECT query with the LAST_INSERT_ID() function.

For example, using Connector/ODBC you would execute two separate statements, the INSERT statement and the SELECT query to obtain the auto-increment value.

INSERT INTO tbl (auto,text) VALUES(NULL,'text');
SELECT LAST_INSERT_ID();

If you do not require the value within your application, but do require the value as part of another INSERT, the entire process can be handled by executing the following statements:

INSERT INTO tbl (auto,text) VALUES(NULL,'text');
INSERT INTO tbl2 (id,text) VALUES(LAST_INSERT_ID(),'text');

Certain ODBC applications (including Delphi and Access) may have trouble obtaining the auto-increment value using the previous examples. In this case, try the following statement as an alternative:

SELECT * FROM tbl WHERE auto IS NULL;

This alternative method requires that sql_auto_is_null variable is not set to 0. See Server System Variables.

See also How to Get the Unique ID for the Last Inserted Row.








Reference : https://dev.mysql.com/doc/connector-odbc/en/connector-odbc-usagenotes-functionality-last-insert-id.html








'Skills > Database' 카테고리의 다른 글

Installing MongoDB on a Mac  (0) 2019.08.09
Select Insert Example  (0) 2017.12.21
mac을 위핸 sql tool  (0) 2015.10.26
mysql 무료 툴 간단 벤치마킹  (0) 2015.10.26
[Mysql] Function 권한 설정  (0) 2015.10.13

댓글