Re: Function PostgreSQL 9.2

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: "drum(dot)lucas(at)gmail(dot)com" <drum(dot)lucas(at)gmail(dot)com>, Melvin Davidson <melvin6925(at)gmail(dot)com>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Function PostgreSQL 9.2
Date: 2016-05-03 21:46:45
Message-ID: 57291C45.1060406@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 05/03/2016 02:27 PM, drum(dot)lucas(at)gmail(dot)com wrote:
>
>
> On 4 May 2016 at 01:18, Melvin Davidson <melvin6925(at)gmail(dot)com
> <mailto:melvin6925(at)gmail(dot)com>> wrote:
>
>
>
> On Tue, May 3, 2016 at 1:21 AM, David G. Johnston
> <david(dot)g(dot)johnston(at)gmail(dot)com <mailto:david(dot)g(dot)johnston(at)gmail(dot)com>> wrote:
>
>
> Well.. I don't need to add a constraint if I already have a
> default value, that's right...
>
>
> Wrong
> David J.
>
>
> What you need is a TRIGGER function & TRIGGER that will select and
> assign the next users_code based on company_id.
> I'm not going to write the whole thing for you, but here is part of
> the trigger function logic.
>
> eg: IF NEW.company_id = 1 THEN
> NEW.users_code = NEXTVAL(c1_users_code_seq);
> ELSEIF NEW.company.id <http://NEW.company.id> = 2 THEN
> NEW.users_code = NEXTVAL(c2_users_code_seq);
> ELSEIF NEW.company.id <http://NEW.company.id> = 3 THEN
> NEW.users_code = NEXTVAL(c3_users_code_seq);
> ...
> ...
> ELSE
> < something bad happened because NEW.company_id was
> not valid ?
> END IF;
>
>
>
>
> Do I have to have one sequence peer company_id ? There will be
> thousands.. isn't there a better way to do that?
>
> Seriously, get yourself the books I have recommended and study them
> BEFORE you continue attempting to design your database.
> You need a firm understanding of logical design & flow, otherwise
> you will be wasting your time.
>
>
> That's what I'm doing.. Studying.. asking for some help to get a better
> understand.... isn't this the purpose of this mail list?

I think the intent being expressed was to: study --> sketch out
design/schema --> ask list whether previous is correct --> make
corrections or not --> implement.

Instead what seems to have happened is: implement --> get painted into
corner --> ask how to escape corner. Hence the frustration on the part
of list members.

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2016-05-03 21:50:19 Re: pg_upgrade with an extension name change
Previous Message David G. Johnston 2016-05-03 21:46:02 Re: Function PostgreSQL 9.2