![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
You can do that. But first you have to do some optimisations, like: add a column id(bigserial) to the departamens table, after which you will replace the column department with id_department in the projects table. It is an optimisation, as you are dealing with integer, not text. |
|
After that, what do you want to achieve? When you are inserting a department, should the server insert 2 to 8 blank records in the projects table which contain the inserted department? Or do you want not to be able to insert a department if there aren't already 2 to 8 projects containing that department in the projects table? |
#3
| |||
| |||
|
|
I'd like to ensure that the creation of a department also implies the creation of two to eight projects; no more, no less: CREATE TABLE departments ( department text primary key not null ); CREATE TABLE projects ( project text primary key not null, department text not null references departments(department) on delete cascade on update cascade ); So it'd be nice to have an INSERT trigger that's fired on departments at the end of the transaction to ensure that between two and eight projects exist for the newly created department. Is there no way to achieve the above stated goal in the server? Must I rely on the application to enforce consistency? |
#4
| |||
| |||
|
|
On Tue, 26 Oct 2004, Mike Mascari wrote: I'd like to ensure that the creation of a department also implies the creation of two to eight projects; no more, no less: Is there no way to achieve the above stated goal in the server? Must I rely on the application to enforce consistency? Well. It's not exactly meant to be a user facing feature, but check out CREATE CONSTRAINT TRIGGER. |
#5
| |||
| |||
|
|
Stephan Szabo wrote: On Tue, 26 Oct 2004, Mike Mascari wrote: I'd like to ensure that the creation of a department also implies the creation of two to eight projects; no more, no less: Is there no way to achieve the above stated goal in the server? Must I rely on the application to enforce consistency? Well. It's not exactly meant to be a user facing feature, but check out CREATE CONSTRAINT TRIGGER. Thanks, Stephan! I read the disclaimer "It is not intended for general use" but am curious as to why it isn't a user-facing feature? Is it a function of just exposing a cleaner SQL interface, or is it a function of the trigger queue having been written after user-defined triggers, or is there some philosophical argument against allowing user-definable triggers to be deferred? |
#6
| |||
| |||
|
|
Stephan Szabo wrote: On Tue, 26 Oct 2004, Mike Mascari wrote: I'd like to ensure that the creation of a department also implies the creation of two to eight projects; no more, no less: Is there no way to achieve the above stated goal in the server? Must I rely on the application to enforce consistency? Well. It's not exactly meant to be a user facing feature, but check out CREATE CONSTRAINT TRIGGER. Thanks, Stephan! I read the disclaimer "It is not intended for general use" but am curious as to why it isn't a user-facing feature? Is it a function of just exposing a cleaner SQL interface, or is it a function of the trigger queue having been written after user-defined triggers, or is there some philosophical argument against allowing user-definable triggers to be deferred? |
![]() |
| Thread Tools | |
| Display Modes | |
| |