我有以下问题。在我的应用程序中,用户使用特殊系统(强度、感知等,值从 1 到 10)创建游戏角色。在保存或之后(通过调用过程),我需要根据特殊参数值计算字符统计信息。我怎样才能做到这一点 ?这是关系方案:
这是SQL代码:
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);
并查询返回所有参数:
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
. 使用什么会更好:触发器或过程?
编辑
我正在尝试使用答案中的代码,但出现错误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;