[mysql] INSERT하는 동시에, INSERT된 record를 얻을 수 있을까

lacovnk의 이미지

insert하는 record의 id auto_increment primary key..) 를 가지고 곧바로 다른 작업을 해야 하는데

insert하고, select로 최신의 레코드의 id를 구하다 보면, 그 사이에 또 다른 record가 insert되어 있을지 모르니.. (굉장히 어려운 타이밍 문제이긴 하지만 그냥 가능성이;; )

한번에 해결하고 싶어서요 ㅎㅎ

insert하고 나면 result가 empty였던 것 같고;;

indie의 이미지

찾으시는게 이게 맞는지 몰라 링크를 올립니다.
http://dev.mysql.com/doc/mysql/en/mysql_insert_id.html

집에나 갈까?

ssik425의 이미지

insert를 하면서 lock을 걸어 버리면 안되나요?

개발자들의 궁극적 비전은 ?

sixmen의 이미지

걱정하지 않으셔도 됩니다. LAST_INSERT_ID() 도움말 보시면 아시겠지만 per-connection 입니다.
그러니까 그 사이에 다른 connection 을 열어서 insert를 한 녀석이 있어도 LAST_INSERT_ID()는 자기가 전에 실행한 값을 반환합니다.

jinoos의 이미지

sixmen wrote:
걱정하지 않으셔도 됩니다. LAST_INSERT_ID() 도움말 보시면 아시겠지만 per-connection 입니다.
그러니까 그 사이에 다른 connection 을 열어서 insert를 한 녀석이 있어도 LAST_INSERT_ID()는 자기가 전에 실행한 값을 반환합니다.

주의사항은 AUTO_INCREMENT 가 지정되어있는 테이블에서만 가능합니다.

목적을 찾아서... jiNoos

shilf의 이미지

제가
서버 프로그램을 c언어로 짜고있는데
거기서 mysql에 접속해서
정보를 저장하고 있었습니다.
auto_increment에 의해 늘어나는 키값을
잡아내기 위해서
fid = mysql_insert_id();
이런식으로 사용했습니다.

근데 다음과 같은 에러가 나오네요.
도움 부탁드립니다.
다른데 찾아봐도 인수같은거 안쓰던데
물론 바로 위에서 insert 하는 게 있습니다.

too few arguments to function `mysql_insert_id'

송효진의 이미지

c 라면 connection 정보가 담긴 변수를 생략 못할것 같은데요.

익명 사용자의 이미지

mysql *tmp

mysql_insert_id(tmp); 네요

답변 감사합니다.

익명 사용자의 이미지

insert직후 뭔가 작업을 데이터베이스내에서 수행해야 한다면,
trigger가 딱이겠지요.

* idu(insert, delete, update)에 trigger를 달아서 뭔가 할 수 있겠지요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.