我正在做一個基於網絡的小遊戲,需要確定將sql數據庫中某些基礎數據的完整性檢查的邏輯放在哪裏。db中用於維護點系統關係的邏輯?
每個用戶都記錄分配給他的點數,點數由各種任務授予。我會記錄每項任務交易以確保它們不會重複,並在完成時跟蹤任務的價值,因爲我的個人獎勵級別隨時間波動。
我的模式是這樣的,到目前爲止:
create table player (
player_ID serial primary key,
player_Points int not null default 0
);
create table task (
task_ID serial primary key,
task_PointsAwarded int not null
);
create table task_list (
player_ID int references player(player_ID),
task_ID int references task(task_ID),
when_completed timestamp default current_timestamp,
point_value int not null, --not fk because task value may change later
constraint pk_player_task_id primary key (player_ID, task_ID)
);
因此,player.player_Points
應該總在task_list
他的所有累積的任務點。
現在我在哪裏把邏輯強制執行?
我應該完全消除player.player_Points
,每次我想知道總分時都會查詢嗎?這看起來很浪費,因爲我會在遊戲過程中做很多的查詢。
或者,在task_list
中加上一個觸發器即自動更新player.player_Points
?數據庫中是否有過多的邏輯,應該在應用程序中維護這種關係?
謝謝。