![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
where the second row should be offed. How to go about? |
#3
| |||
| |||
|
|
On Thu, 30 Sep 2010 22:44:35 +0200, Dorky Dork wrote: where the second row should be offed. How to go about? Check constraint, specifying that w_id must be < (strictly less than) I_id will solve your problem. |
#4
| |||
| |||
|
|
But if w=winner and l=loser, what happens if team 4 beat team 3? The problem needs to be rethought. And what about ties? |
#5
| |||
| |||
|
|
On Fri, 01 Oct 2010 10:41:00 +0000, HoneyMonster wrote: But if w=winner and l=loser, what happens if team 4 beat team 3? The problem needs to be rethought. And what about ties? Then, you would have to change your data model You would have columns t1,t2, and w. First team, second team and enumerated column "w" which would signify the winner. On a more subtle level, you're mixing categories: teams that played the game and the outcome of the game are not the same type of objects. You shouldn't pack the information about the game together with the information about the team. |
#6
| |||
| |||
|
|
On Fri, 01 Oct 2010 10:41:00 +0000, HoneyMonster wrote: But if w=winner and l=loser, what happens if team 4 beat team 3? The problem needs to be rethought. And what about ties? |

|
Then, you would have to change your data model You would have columns t1,t2, and w. First team, second team and enumerated column "w" which would signify the winner. |
|
On a more subtle level, you're mixing categories: teams that played the game and the outcome of the game are not the same type of objects. You shouldn't pack the information about the game together with the information about the team. |
#7
| |||
| |||
|
|
Mladen Gogala wrote: On Fri, 01 Oct 2010 10:41:00 +0000, HoneyMonster wrote: But if w=winner and l=loser, what happens if team 4 beat team 3? The problem needs to be rethought. And what about ties? No ties in tennis. ![]() Then, you would have to change your data model You would have columns t1,t2, and w. First team, second team and enumerated column "w" which would signify the winner. I don't see how this solves anything. Then again I'm a complete SQL dork so ... With the above, I would be left with the original problem. With an additional winner column, I still wouldn't want a (t2, t1, w) inserted if a (t1, t2, w) existed (given the same dates). Back to square one. I'm not sure how orthodox this is, but I did this: create table test ( w_id char(4), l_id char(4), date date, unique(w_id, l_id, date), check(bla('test', w_id, l_id,date)), check(w_id <> l_id) ); where bla() is a little plpgsql hack which essentially does an EXISTS (SELECT 1 FROM test WHERE w_id= 'l_id' AND l_id = 'w_id' AND date = 'date'). Seems to worky work. So far at least. On a more subtle level, you're mixing categories: teams that played the game and the outcome of the game are not the same type of objects. You shouldn't pack the information about the game together with the information about the team. |
|
I don't think this is the problem. The problem is that a match between two opponents is a reflexive relation, (p1,p2) = (p2, p1). Tables in SQL are not, unless enforced. See the above caveat though. |
#8
| |||||
| |||||
|
|
Mladen Gogala wrote: On Fri, 01 Oct 2010 10:41:00 +0000, HoneyMonster wrote: But if w=winner and l=loser, what happens if team 4 beat team 3? The problem needs to be rethought. And what about ties? No ties in tennis. ![]() Then, you would have to change your data model You would have columns t1,t2, and w. First team, second team and enumerated column "w" which would signify the winner. I don't see how this solves anything. Then again I'm a complete SQL dork so ... |
|
With the above, I would be left with the original problem. With an additional winner column, I still wouldn't want a (t2, t1, w) inserted if a (t1, t2, w) existed (given the same dates). Back to square one. |
|
I'm not sure how orthodox this is, but I did this: create table test ( w_id char(4), l_id char(4), date date, unique(w_id, l_id, date), check(bla('test', w_id, l_id,date)), check(w_id <> l_id) ); where bla() is a little plpgsql hack which essentially does an EXISTS (SELECT 1 FROM test WHERE w_id= 'l_id' AND l_id = 'w_id' AND date = 'date'). |
|
Seems to worky work. So far at least. |
|
On a more subtle level, you're mixing categories: teams that played the game and the outcome of the game are not the same type of objects. You shouldn't pack the information about the game together with the information about the team. I don't think this is the problem. The problem is that a match between two opponents is a reflexive relation, (p1,p2) = (p2, p1). Tables in SQL are not, unless enforced. See the above caveat though. |
#9
| |||||
| |||||
|
|
On 06.11.2010 07:55, Dorky Dork wrote: Mladen Gogala wrote: On Fri, 01 Oct 2010 10:41:00 +0000, HoneyMonster wrote: But if w=winner and l=loser, what happens if team 4 beat team 3? The problem needs to be rethought. And what about ties? No ties in tennis. ![]() Then, you would have to change your data model You would have columns t1,t2, and w. First team, second team and enumerated column "w" which would signify the winner. I don't see how this solves anything. Then again I'm a complete SQL dork so ... If you enforce that t1_id < t2_id then only (1,2,2001-01-01) is valid in the DB and not (2,1,2001-01-01). Make the combination of all three unique and you avoid two teams with the same date in the DB. |
|
With the above, I would be left with the original problem. With an additional winner column, I still wouldn't want a (t2, t1, w) inserted if a (t1, t2, w) existed (given the same dates). Back to square one. No, w/l column would not be part of the uniqueness enforcing columns. For those you only need the three mentioned above. I'm not sure how orthodox this is, but I did this: create table test ( w_id char(4), l_id char(4), date date, unique(w_id, l_id, date), check(bla('test', w_id, l_id,date)), check(w_id <> l_id) ); where bla() is a little plpgsql hack which essentially does an EXISTS (SELECT 1 FROM test WHERE w_id= 'l_id' AND l_id = 'w_id' AND date = 'date'). You are not really comparing those column values against column names, are you? |
|
Seems to worky work. So far at least. But it is way inefficient - especially since your table will grow over time. Even with an index you have the callout to plpgsql which isn't necessary. |
|
On a more subtle level, you're mixing categories: teams that played the game and the outcome of the game are not the same type of objects. You shouldn't pack the information about the game together with the information about the team. I don't think this is the problem. The problem is that a match between two opponents is a reflexive relation, (p1,p2) = (p2, p1). Tables in SQL are not, unless enforced. See the above caveat though. Enforcement is easy, as Mladen has pointed out. I do not know Postgres well enough but I guess you can even create a before insert trigger which will exchange columns if id ordering is wrong. So you can do insert into foo values (1,2,2001-01-01) and insert into foo values (2,1,2001-01-01) and in both cases the record would read (1,2,2001-01-01). |
|
And I do also think that you assign semantic to the column ordering. Note especially that the distribution of ids across the two columns is dependent on the outcome of the game. If you pick Mladen's approach you can create records beforehand and later update the record with the winning team info. |
![]() |
| Thread Tools | |
| Display Modes | |
| |