auto_increment key를 가진 테이블에 하나의 Row를 삽입했을 때,
방금 삽입한 Row의 key를 알고 싶을 때가 있다.
이 때는
select * from table where autoincrement_col is null;
이렇게 하면 된다.
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html auto_increment 를 검색해보라.
thread-safe 한지를 알고 싶을텐데.. 이 테스트를 해 보면 된다.
두 개의 mysql 클라이언트를 띄우고,
아래 스키마의 테이블을 만든다.
create table user (
user_no int not null auto_increment,
user_id char(12),
primary key (user_no)
);
Client 1:
insert into user (user_id) values('abc');
Client 2:
insert into user (user_id) values('def');
Client 1:
select * from user where user_no is null;
Client 2:
select * from user where user_no is null;
그럼 결과는?
+---------+---------+
| user_no | user_id |
+---------+---------+
| 1 | abc |
+---------+---------+
Client 2:
+---------+---------+
| user_no | user_id |
+---------+---------+
| 2 | def |
+---------+---------+
얼쑤 좋구나..