DHJJ [Hatsune's Journal Japan] blog

Hatsune's Journal Japan blog

目次

Blog 利用状況

ニュース

最新ツイート

運営サイト

Hatsune's Journal Japan
DHJJ

著作など

資格など

OCP
MCP

書庫

日記カテゴリ

わんくま同盟

income(収入)とoutgo(支出)の一方は(略)をトリガーで実現

元ネタ→http://blogs.wankuma.com/episteme/archive/2008/03/22/129054.aspx
別回答→http://blogs.wankuma.com/hatsune/archive/2008/03/22/129070.aspx

SQLLiteは手元にないのでOracle11gでやってみた。

もともとincome/outgoがあって、この制約が”後だしジャンケン”的にくっつけざるを得ないときという前提で、今回はトリガーを使って制限をかけています。

ただ、本当にやるとしたら、許されるとしたらテーブル定義変えてしまいます。

だから、これは本当に暫定的な緊急処置です。

DROP TABLE cashbox;
CREATE TABLE cashbox
  ( key     NUMBER PRIMARY KEY,
    setdate DATE NOT NULL,
    summary VARCHAR2(20),
    income  NUMBER,
    outgo   NUMBER,
    note    VARCHAR2(20)
  )
/
CREATE SEQUENCE autonumber
/
CREATE OR REPLACE TRIGGER cashbox_BI
BEFORE INSERT ON cashbox FOR EACH ROW
BEGIN
    IF (:NEW.income IS NULL AND :NEW.outgo IS NULL) OR 
       (:NEW.income IS NOT NULL AND :NEW.outgo IS NOT NULL)THEN
        RAISE VALUE_ERROR;
    END IF;
 -- Oracle 11g
 :NEW.key := autonumber.NEXTVAL;
    -- Oracle 10g
 -- SELECT autonumber.NEXTVAL INTO :NEW.key FROM DUAL;
END;
/
CREATE OR REPLACE TRIGGER cashbox_BU
BEFORE UPDATE ON cashbox FOR EACH ROW
BEGIN
    IF (:NEW.income IS NULL AND :NEW.outgo IS NULL) OR 
       (:NEW.income IS NOT NULL AND :NEW.outgo IS NOT NULL)THEN
        RAISE VALUE_ERROR;
    END IF;
END;
/
CREATE INDEX XIE_cashbox ON cashbox ( setdate )
/
-- レコードをいくつか挿入
INSERT INTO cashbox
( setdate, summary, outgo )
VALUES ( '2008/02/29', 'leftover', 380);
INSERT INTO cashbox
( setdate, summary, income )
VALUES ( '2008/03/01', 'carryover', 380);
INSERT INTO cashbox
( setdate, summary, income )
VALUES ( '2008/03/01', 'from-mama', 5000);
INSERT INTO cashbox
( setdate, summary, outgo )
VALUES ( '2008/03/03', 'comic', 410);
INSERT INTO cashbox
( setdate, summary,  outgo )
VALUES ( '2008/03/02', 'potato-chips', 147);
INSERT INTO cashbox
( setdate, summary, income )
VALUES ( '2008/03/04', 'from-granpa', 1000);

-- 金額を入れない
INSERT INTO cashbox ( setdate, summary ) 
VALUES ( '2008/03/04', 'from-granpa');

行2でエラーが発生しました。:
ORA-06502: PL/SQL: 数値または値のエラーが発生しました

-- 両方にいれちゃう
INSERT INTO cashbox ( setdate, summary, income, outgo ) 
VALUES ( '2008/03/04', 'from-granpa', 50, 50);

行1でエラーが発生しました。:
ORA-06502: PL/SQL: 数値または値のエラーが発生しました

-- 全部出力してみる
SELECT * FROM cashbox ORDER BY setdate,key;
       KEY SETDATE  SUMMARY                  INCOME      OUTGO
---------- -------- -------------------- ---------- ----------
        21 08-02-29 leftover                               380
        22 08-03-01 carryover                   380
        23 08-03-01 from-mama                  5000
        25 08-03-02 potato-chips                           147
        24 08-03-03 comic                                  410
        26 08-03-04 from-granpa                1000

-- 三月の収支
SELECT sum(income),sum(outgo),sum(income)-sum(outgo)
  FROM cashbox
 WHERE TRUNC(setdate,'MM')=TO_DATE('20080301','YYYYMMDD')
;
SUM(INCOME) SUM(OUTGO) SUM(INCOME)-SUM(OUTGO)
----------- ---------- ----------------------
       6380        557                   5823

投稿日時 : 2008年3月22日 13:15

Feedback

# yudwLLGOjzRnPVtHPyr 2011/12/13 18:18 http://www.birthcontrolremedy.com/birth-control/cl

I decided to help and sent a post to the social bookmarks. I hope to raise it in popularity!!...

# HVrQePYGITlaJYy 2012/01/04 5:15 http://www.kosherbeefjerky.com/

wAWYUu Read, of course, far from my topic. But still, we can work together. How do you feel about trust management?!...

# dMQvfRyVzBozFCD 2012/01/07 14:12 http://www.luckyvitamin.com/p-6840-absorbaid-absor

Hello! Read the pages not for the first day. Yes, the connection speed is not good. How can I subscribe? I would like to read you in the future!...

# UGG Paris 2012/11/18 23:40 http://www.sarenzauggfrance.com/

Romance is without question frail found at having your baby, yet it increases better as we age you'll no doubt suitably fertilized.
UGG Paris http://www.sarenzauggfrance.com/

# beats by dre pas cher 2012/11/18 23:40 http://www.casquebeatsbydrefr2013.com/

Specifically where you can find marital relationship with no need of enjoy, we will have enjoy with no need of marital relationship.
beats by dre pas cher http://www.casquebeatsbydrefr2013.com/

# monster beats by dr. dre solo hd 2012/11/18 23:41 http://www.beatsbydrebilligsde.com/

May not speak of your entire peace to 1 not so rosy as opposed to your self.
monster beats by dr. dre solo hd http://www.beatsbydrebilligsde.com/

# louis vuitton shoes 2012/11/18 23:41 http://www.louisvuittonwallets2013.com/

Have on‘t attempt overtime, the top factors may be purchased whenever minimal hope the theifs to.
louis vuitton shoes http://www.louisvuittonwallets2013.com/

# beats by dre kopfhörer 2012/11/18 23:42 http://www.beatsbydrdrebilligde.com/

In order for an excellent data processing of a valued at, rely your friends and family.
beats by dre kopfhörer http://www.beatsbydrdrebilligde.com/

# louis vuitton speedy 2012/11/18 23:43 http://www.louisvuittonoutletdiaperbag.com/

I adore you do not resulting from who you are, though resulting from which We're their morning you have made.
louis vuitton speedy http://www.louisvuittonoutletdiaperbag.com/

# UGG Pas Cher 2012/11/18 23:43 http://www.botteuggsoldes.com/

Assume‘T use up your schedule more than human being/lovely women,what person isn‘T willing to use up his or her's free time for you.
UGG Pas Cher http://www.botteuggsoldes.com/

# casque beats by dre 2012/11/18 23:43 http://www.beatsbydrefr2013.com/

Never connect with others whorrr re confident to be with. Make friends who will stress a person to prize for yourself " up ".
casque beats by dre http://www.beatsbydrefr2013.com/

# clarisonic mia 2012/11/18 23:44 http://www.clarisonicmia-coupon.com/

Into prosperity our own mates be familiar with individuals; found in hardship we all our own mates.
clarisonic mia http://www.clarisonicmia-coupon.com/

# beats by dre 2012/11/18 23:44 http://www.monsterbeatsbydrdresolode.com/

Even if a friend or relative doesn‘big t accept you a task desire them to,doesn‘big t imply that they will wear‘big t accept you along with most have.
beats by dre http://www.monsterbeatsbydrdresolode.com/

# nike schuhe damen 2012/11/18 23:45 http://www.nikeschuhedamenherren.com/

For no reason scowl, virtually all you will be heartbreaking, once you do not no who's going to be falling out fond of your new be happy.
nike schuhe damen http://www.nikeschuhedamenherren.com/

# Nike Free 3.0 2012/11/18 23:45 http://www.nikefree3runschuhe.com/

A colleague you just pay money for equipped with produces can be purchased in one.
Nike Free 3.0 http://www.nikefree3runschuhe.com/

# Botte UGG France 2012/11/18 23:45 http://www.bottesuggpascherfrancefr.com/

Our nastiest route to miss anyone will be taking up space most suitable they always the entire group being knowledgeable of you'll‘tonne have them.
Botte UGG France http://www.bottesuggpascherfrancefr.com/

# louis vuitton outlet store 2012/11/18 23:46 http://www.louisvuittonbackpack2013.com/

Joy is a really cologne it's not possible to strain along a few without the benefit of acquiring a a small number of comes along your own.
louis vuitton outlet store http://www.louisvuittonbackpack2013.com/

# nike free run damen 2012/11/18 23:47 http://www.nikefree3rundamen.com/

Friends you ought to find that includes shows shall be purchased from an individual.
nike free run damen http://www.nikefree3rundamen.com/

# beats by dre 2012/11/18 23:47 http://www.beatsbydrepascherfr2013.com/

Where exactly there exists romantic relationship whilst not seriously like, there'll be seriously like whilst not romantic relationship.
beats by dre http://www.beatsbydrepascherfr2013.com/

# maillot foot pas cher 2013/01/13 8:07 http://www.maillotdefoot2013.info/

afin que nous puissions parler dessayer de gagner quatre dans une rangée. Mais avec trois entrées et devrait être de retour au club de faire deux métiers pour aider sa poussée en séries éliminatoires, Les Red Bulls a également acquis le gardien Bill Gaudette à partir de Los Angeles le vendredi pour un choix de deuxième ronde projet complémentaire. Les Revs Achat de Maillot de Foot en ligne . nous allons chercher à sauter sur eux t?t et souvent ? Montero ne seront pas disponibles après avoir été suspendu suite à son expulsion dans le temps additionnel contre les Timbers. SEATTLE AP Dans 1 Saisons de la Major League Soccer jeu? et Seattle entra?neur Sigi Schmid se sont rencontrés à la ligne médiane et se serrèrent la main droite avant le match. Burch nest clairement pas heureux que DC United a choisi de ne le garder et sera de retour pour faire face à son ancien club . qui peut se retourner contre vous un peu. Unis gardien Bill Hamid a raté les trois derniers matchs avec une blessure à la cheville et est considéré comme douteux. Son contrat actuel arrive à échéance en Juin 2013 .
maillot foot pas cher http://www.maillotdefoot2013.info/

タイトル
名前
Url
コメント