Re: Function PostgreSQL 9.2

From: "drum(dot)lucas(at)gmail(dot)com" <drum(dot)lucas(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Function PostgreSQL 9.2
Date: 2016-05-03 04:00:41
Message-ID: CAE_gQfU5syUhyQFZZob5z4bEMem06QUdYFrAh+ZWGYi3+bxVMA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 3 May 2016 at 12:44, drum(dot)lucas(at)gmail(dot)com <drum(dot)lucas(at)gmail(dot)com> wrote:

> This is what I've done:
>
>
> -- 1 - Creating the Sequence:
>
> CREATE SEQUENCE users_code_seq
>> INCREMENT 1
>> MINVALUE 1
>> MAXVALUE 9223372036854775807
>> START 1000;
>> CACHE 1;
>
>
> -- 2 - Setting the DEFAULT
>
> ALTER TABLE public.users ALTER COLUMN code SET DEFAULT
>> NEXTVAL('users_code_seq');
>
>
> -- 3 - Setting the column as NOT NULL;
>
>>
>> ALTER TABLE public.users ALTER COLUMN code SET NOT NULL;
>
>
> -- 4 - Setting the trigger
>
> CREATE TRIGGER public.update_code_column
>> BEFORE UPDATE OR INSERT
>> ON public.users
>> FOR EACH ROW
>> EXECUTE PROCEDURE public.users_code_seq;
>
>
> -- 5 - Creating a CONSTRAINT UNIQUE
>
>> ALTER TABLE public.users
>> ADD CONSTRAINT uc_users_code UNIQUE("code");
>
>
>
> Is that right?
> Am I missing something?
>
> Cheers
> Lucas
>

Well.. I don't need to add a constraint if I already have a default value,
that's right...

Anyway...

hmm.. actually.. it's a little bit different what I've done and what I need
=(

1 - each user on the public.users table, is part of a company. Each company
has a unique company_id

2 - Remember the default 1000 value? That value is per company.

*Example:*

Company Test1 - Company_id = 1
- user john01 = users.code: 1000
- user john02 = users.code: Nz
- user john03 = users.code: 1001
- user john04 = users.code: Nz

Company Test2 - Company_id = 2
- user matt01 = users.code: Text1
- user matt02 = users.code: 1000
- user matt03 = users.code: 1001
- user matt04 = users.code: 1002

Company Test3 - Company_id = 3
- user luke01 = users.code: 1000
- user luke02 = users.code: 1001
- user luke03 = users.code: Text2
- user luke04 = users.code: 1002

So, the default value is 1000 for EACH company. And the users must get the
nextval value from there.

How can I do that?
Or at least if you guys can give me a direction...

Cheers

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2016-05-03 05:21:28 Re: Function PostgreSQL 9.2
Previous Message Sameer Kumar 2016-05-03 01:21:13 Re: index question