database trigger 질문입니다

pjs3224의 이미지

안녕하세요~ 데이터 베이스 트리거 작성중에 질문 올립니다~

현재 사용하는건 MYSQL 5.2 Workbench 버전을 쓰고 있구요

A라는 테이블이 있고 A 테이블 안에는

rowid(PK, AI, 자료형 INT), id(int), fid(int) 이렇게 2가지가 있습니다

제가 원하는건

insert into A ( id, fid ) values ( 7, 8 ); 명령이 수행 될 경우

실제 A 테이블에 저장되는 값은
rowid id fid
1 7 8
2 8 7

이렇게 저장되는 것입니다

아는지식을 총동원해서 trigger를 짜보긴 했는데

트리거 적용 후 insert문 실행 시 매번

insert into test.A(id, fid) values (7, 8) Error Code: 1146. Table 'test.A' doesn't exist

이런 에러만 볼 수 있었습니다

적용한 트리거는
------------------------------------------------------------
CREATE TRIGGER `A_BUPD` BEFORE UPDATE ON A FOR EACH ROW

BEGIN
DECLARE ID int;
DECLARE FID int;
SET PID = NEW.id;
SET FPID = NEW.fid;
INSERT INTO A (id, fid) values (FID, ID);
END
------------------------------------------------------------
이렇게 해 보았었습니다.

chanik의 이미지

트리거가 걸려있는 테이블을 트리거 안에서 변경하는 sql문은 허용되지 않습니다.

http://dev.mysql.com/doc/refman/5.5/en/faqs-triggers.html#qandaitem-B-5-1-9

B.5.9: Can triggers access tables?
 
A trigger can access both old and new data in its own table. 
A trigger can also affect other tables, but it is not permitted to modify a table 
that is already being used (for reading or writing) by the statement 
that invoked the function or trigger.

해당 트랜잭션과 관계없는 제3의 테이블을 변경한다거나, 아래의 페이지에 나오는 식으로
BEFORE INSERT 트리거에서 new 데이터를 변경하여 해당 row에 영향을 주는 것은 허용되고요.
(위에 인용한 FAQ도 여기서 따 온 것입니다)

http://www.dbforums.com/mysql/1676515-trigger-problem-inserting-updating-same-table.html

제가 보기엔, 지금 원하시는 작업은 트리거로 해결할 문제가 아니고
stored procedure를 만들어 다룰 문제인 것 같습니다.
(id, fid)를 받아서 두 개의 row를 insert하는 프로시저를 만들면 되겠죠.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.