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 00:44:45
Message-ID: CAE_gQfXTuW7qC2FZdMrtX+dnW8zsBj1kmneGn-CM1HMwfdPiKA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2016-05-03 00:45:38 Re: index question
Previous Message Sameer Kumar 2016-05-03 00:38:01 Re: index question