Re: Function PostgreSQL 9.2

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

On Tue, May 3, 2016 at 1:21 AM, David G. Johnston <
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 = 2 THEN
NEW.users_code = NEXTVAL(c2_users_code_seq);
ELSEIF 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;

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.

--
*Melvin Davidson*
I reserve the right to fantasize. Whether or not you
wish to share my fantasy is entirely up to you.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2016-05-03 13:22:55 Re: Vacuum full of parent without partitions possible?
Previous Message uğur Karabin 2016-05-03 12:39:32 Re: Does the initial postgres user have a password?