2011-01-28 6 views
1

次の問題があります。私のアプリケーションでは、特殊なシステム(強さ、知覚など1〜10の値を持つ)を使用してゲームキャラクターを作成します。保存または後(プロシージャを呼び出すことによって)私はSPECIALパラメータ値のベースで文字の統計をカウントする必要があります。これどうやってするの ?これは、関係スキームである:トリガ/プロシージャを使用してエンティティの値に基づいてパラメータを評価してください

create table Player (
    id_player numeric, 
    player_name varchar2(50) not null, 
    age decimal not null, 
    strength decimal not null, 
    perception decimal not null, 
    endurance decimal not null, 
    charisma decimal not null, 
    inteligence decimal not null, 
    agility decimal not null, 
    luck decimal not null, 
    caps decimal not null, 
    statistics numeric, 
    CONSTRAINT chk_s check (strength <= 10), 
    CONSTRAINT chk_p check (perception <= 10), 
    CONSTRAINT chk_e check (endurance <= 10), 
    CONSTRAINT chk_c check (charisma <= 10), 
    CONSTRAINT chk_i check (inteligence <= 10), 
    CONSTRAINT chk_a check (agility <= 10), 
    CONSTRAINT chk_l check (luck <= 10), 
    CONSTRAINT unique_name UNIQUE (player_name), 

    CONSTRAINT PLAYER_PK primary key (id_player) 
); 

create table Player_derived_statistics(
    id_statistics numeric, 
    carry_weight decimal, 
    hit_points decimal, 
    radiation_resistance decimal, 

    CONSTRAINT DERIVED_STATISTICS_PK primary key (id_statistics) 
); 

alter table Player add constraint PLAYER_DERIVED_STATISTICS_FK1 foreign key (statistics) references Player_derived_statistics (id_statistics); 

とクエリーすべてのパラメータを返す:

enter image description here

、ここでは、SQLコードです

SELECT p.strength, p.perception, p.endurance, p.charisma, p.inteligence, p.agility, p.luck 
from player p inner join player_derived_statistics s on s.id_statistics = p.statistics; 

だから、最後に私はなりたいです各プレイヤーのcarry_weight、hit_points、radiation_resistanceを数えることができます。すべての式が(player_parameter * 10) + 150であるとします。トリガーまたはプロシージャーを使用する方が良いでしょうか?


EDIT

私は答えからコードを使用しようとしているが、私はエラーEncountered the symbol "INNER" when expecting one of the following: (...を取得しています。

CREATE OR REPLACE PACKAGE pkg_player_stats AS 
    FUNCTION get_derived_stats(p_id_player IN player.id_player%TYPE) 
    RETURN derived_stats_rec 
    IS 
    l_stats_rec derived_stats_rec; 
    BEGIN 
    SELECT (p.strength*10)+150, 
      (p.endurance*20)+150, 
      ((p.endurance-1)*2)/100 
     INTO l_stats_rec.carry_weight, 
      l_stats_rec.hit_points, 
      l_stats_rec.radiation_resistance 
     FROM (
     SELECT p.strength, 
       p.endurance 
      from player p inner join player_derived_statistics s on s.id_statistics = p.statistics); 
    RETURN l_stats_rec; 
    END get_derived_stats; 
END; 

答えて

0

私は間違いなく、このようなことのためにトリガを使用しません。 ID_PLAYERパラメータを受け取り、値または値のレコードのいずれかを返す関数が必要なように思えます。このような何か(私はあなたが記述している数式を理解していることはよく分からないことに注意してくださいので、私は少し推測している

CREATE OR REPLACE PACKAGE pkg_player_stats 
AS 
    TYPE derived_stats_rec IS RECORD (
    carry_weight NUMBER, 
    hit_points NUMBER, 
    radiation_resistance NUMBER); 

    FUNCTION get_derived_stats(p_id_player IN player.id_player%TYPE) 
    RETURN derived_stats_rec; 
END; 

CREATE OR REPLACE PACKAGE pkg_player_stats 
AS 
    FUNCTION get_derived_stats(p_id_player IN player.id_player%TYPE) 
    RETURN derived_stats_rec 
    IS 
    l_stats_rec derived_stats_rec; 
    BEGIN 
    SELECT carry_weight   * multiplier + 150, 
      hit_points   * multiplier + 150, 
      radiation_resistance * multiplier + 150 
     INTO l_stats_rec.carry_weight, 
      l_stats_rec.hit_points, 
      l_stats_rec.radiation_resistance 
     FROM (
     SELECT p.strength + 
       p.perception + 
       p.endurance + 
       p.charisma + 
       p.inteligence + 
       p.agility + 
       p.luck multiplier, 
       s.carry_weight, 
       s.hit_points, 
       s.radiation_resistance 
      from player p 
       inner join player_derived_statistics s on s.id_statistics = p.statistics); 
    RETURN l_stats_rec; 
    END get_derived_stats; 
END; 
0

なぜあなたは2つのテーブルが必要なのでしょうか?私は

    のいずれかとなるだろう
  • すべてSPECIAL統計情報を持つ単一のテーブルのみと派生統計(アプリケーション内でビューを照会します)を計算ビュー:

    CREATE VIEW player_v AS 
    SELECT p.strength, ..., /* all attributes */ 
         p.strength * 10 + 150 as carry_weight, 
         p.endurance * 20 + 150 as hit_points, 
         (p.endurance - 1) * 2/100 as radiation_resistance 
        FROM player p 
    
  • ドを持つ単一のテーブルをあなたは、トリガーで更新rived列:あなたは、Oracle 11を使用している場合は、あなたも使用することができ

    CREATE OR REPLACE TRIGGER player_ins_up_trg 
        BEFORE UPDATE OR INSERT ON player 
        FOR EACH ROW 
    BEGIN 
        :new.carry_weight := :new.strength * 10 + 150; 
        :new.hit_points := :new.endurance * 20 + 150; 
        :new.radiation_resistance := (:new.endurance - 1) * 2/100; 
    END; 
    
  • virtual column

    仮想列は、ディスク上に格納されていません。むしろ、データベースは、式または関数の集合を計算することによって、必要に応じて仮想列の値を導き出します。例えば

    ALTER TABLE player ADD (carry_weight AS (strength * 10 + 150)); 
    
+0

Iすでに全体デシベルカントーので、私はそれに固執したいと思い作成しました。 –

+0

良いモデルがあれば、長期的には助けになります。 –

関連する問題