Add contraint that checks sum

I'm trying to add a constraint to a table by checking that the sum of a value is < 100.

This is my schema:

  CREATE TABLE Works  (
     eid      INTEGER,
     did      INTEGER,
     pct_time INTEGER,
     PRIMARY KEY (eid,did),
     FOREIGN KEY (eid) REFERENCES Employee(eid),
     FOREIGN KEY (did) REFERENCES Dept(did)
  );

I need to check that the sum of pct_time for each eid is <= 100.

For example

eid --- did ---- pct_time
0 ----- a ------- 50
0 ----- d ------- 40
0 ----- c ------- 20
1 ----- a ------- 90
1 ----- b ------- 10
2 ----- d ------- 40
2 ----- a ------- 20

Here, it should have errored when I added the 3rd entry for eid 0 as the sum > 100.
Это было бы хорошо для Eid 1 и 2, так как pct_time <= 100
Как это можно сделать?

Пока что все, что я сделал, это

ALTER TABLE Works
ADD CONSTRAINT c1
CHECK SUM(pct_time) < 100
5
задан a_horse_with_no_name 15 May 2011 в 14:20
поделиться