본문 바로가기
IT/DB

MySql 의 last_insert_id()

by 뷰IGHT 2020. 6. 23.

https://cirius.tistory.com/1139

 

[MySQL] last_insert_id() 사용시 주의

많은 개발자들의 Database 관련 코드를 보면 심심치 않게 위험한 요소들을 가지고 있다. MySQL 를 다루면서 많은 실수를 하는 부분이 바로  Last_insert_id() 사용과 관련된 부분이다. 오늘은 잘못된 auto

cirius.tistory.com

- 하나의 insert 문에 대량의 정보를 insert 할 경우 

INSERT INTO some_table ( contents, member_id ) VALUES  ('이건 테스트1', 'bgjo'), ('이건 테스트2', 'bgjo'),('이건 테스트3', 'bgjo') 

이면 last_insert_id()는 1 이라는. 

따로따로 insert 하면 

INSERT INTO some_table ( contents, member_id ) VALUES  ('이건 테스트1', 'bgjo') 

INSERT INTO some_table ( contents, member_id ) VALUES  ('이건 테스트2', 'bgjo') 

INSERT INTO some_table ( contents, member_id ) VALUES  ('이건 테스트3', 'bgjo') 

last_insert_id()는 2

 

트랜잭션도 주의

'IT > DB' 카테고리의 다른 글

[MySql] errno: 121 "Duplicate key on write or update"  (0) 2020.06.21
[mysql] select delete 동시에 하기  (0) 2018.10.18