Re: Should I add a Index Key in this case ?

Steve Midgley <[email protected]> Wed, 16 Oct 2019 11:26:19 -0400
Newsgroups gmane.comp.db.postgresql.sql
Message-ID <CAJexoSJ-08_uaDwm+5GNieYqEJ-QayMwQGzR1u1-2CEcQ+fsHw@mail.gmail.com>
--0000000000008fac52059508b9aa
Content-Type: text/plain; charset="UTF-8"

On Wed, Oct 16, 2019 at 7:36 AM Karen Goh <[email protected]> wrote:

> Hi Experts,
>
> I have a use case as follows :
>
>  add constraints to the database so that no two reservations for the same
> viewing may refer to the same seat.
>
> So, say I have a primary key like this in the table A:
>
>
>     SEAT_id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1
> START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
>     CONSTRAINT A_pkey PRIMARY KEY (SEAT_id))
> WITH (
>     OIDS = FALSE
> )
> TABLESPACE pg_default;
>
> So, basically I would like to create a SEAT_Viewing_Id.
>
> In this case, do I create a Index Key or ?
>
> How should I construct or rather alter my table A to accomodate this
> SEAT_Viewing_Id ?
>
> Hope someone can tell me how.
>
> Furthermore, whenever an insertion is done via WebApp, I would have to
> insert the Index key as well or does PostgreSQL will have a way to
> increment the Index key which is the SEAT_Viewing_Id at the same time?
>
> Thanks & regards,
> Karen
>
>
> I'm not sure I understand exactly what you want to do, but it sounds like
you are want to create a second field/column in your table named
"SEAT_viewing_id" and you want that field to auto-increment independently
from the primary key? If so, you can consider the "serial" datatype as
possiblye meeting your needs:
https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL

So just `alter table`, to add your new field, and make its datatype
`serial`.

Apologies if I misunderstood your question,
Steve

--0000000000008fac52059508b9aa
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><br></div><br><div class=3D"gmail_quote">=
<div dir=3D"ltr" class=3D"gmail_attr">On Wed, Oct 16, 2019 at 7:36 AM Karen=
 Goh &lt;<a href=3D"mailto:[email protected]">[email protected]</a>&g=
t; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0p=
x 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Exp=
erts,<br>
<br>
I have a use case as follows : <br>
<br>
=C2=A0add constraints to the database so that no two reservations for the s=
ame viewing may refer to the same seat.<br>
<br>
So, say I have a primary key like this in the table A:<br>
<br>
<br>
=C2=A0 =C2=A0 SEAT_id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCRE=
MENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),<br>
=C2=A0 =C2=A0 CONSTRAINT A_pkey PRIMARY KEY (SEAT_id))<br>
WITH (<br>
=C2=A0 =C2=A0 OIDS =3D FALSE<br>
)<br>
TABLESPACE pg_default;<br>
<br>
So, basically I would like to create a SEAT_Viewing_Id.<br>
<br>
In this case, do I create a Index Key or ?<br>
<br>
How should I construct or rather alter my table A to accomodate this SEAT_V=
iewing_Id ?<br>
<br>
Hope someone can tell me how.<br>
<br>
Furthermore, whenever an insertion is done via WebApp, I would have to inse=
rt the Index key as well or does PostgreSQL will have a way to increment th=
e Index key which is the SEAT_Viewing_Id at the same time?<br>
<br>
Thanks &amp; regards,<br>
Karen<br>
<br>
<br></blockquote><div>I&#39;m not sure I understand exactly what you want t=
o do, but it sounds like you are want to create a second field/column in yo=
ur table named &quot;SEAT_viewing_id&quot; and you want that field to auto-=
increment independently from the primary key? If so, you can consider the &=
quot;serial&quot; datatype as possiblye meeting your needs: <a href=3D"http=
s://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL">=
https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERI=
AL</a></div><div><br></div><div>So just `alter table`, to add your new fiel=
d, and make its datatype `serial`.<br></div><div><br></div><div>Apologies i=
f I misunderstood your question,</div><div>Steve</div><div><br></div></div>=
</div>

--0000000000008fac52059508b9aa--