pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedUnique Number KEY
4+ messages / 4 participants
[nested] [flat]
* Unique Number KEY
@ 1999-02-26 17:42 Patrice DUMAS <pdumas@inforoutes-ardeche.fr>
1999-02-26 18:25 ` Re: [SQL] Unique Number KEY CIMPOESU Teodor <teo@digiro.net>
0 siblings, 1 reply; 4+ messages in thread
From: Patrice DUMAS @ 1999-02-26 17:42 UTC (permalink / raw)
To: pgsql-sql
Hello,
What is the best way, with PostGres, to create a Unique Number Key
Generator?
For example, I have a STUDENTS table:
{
StudentNumber integer;
StudentName char[40];
...
}
Beginner in SQL, I think I must use Primary key or something like that, to
be sure StudentNumber is unique.
But, I want StudentNumber to be generated automatically, the user don't
worry in assigning a StudentNumber.
What is the best way to do that with PostGres-SQL ?
Thank you for your answers and sorry for my poor english,
Patrice DUMAS
Ardèche, FRANCE.
pdumas@inforoutes-ardeche.fr
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [SQL] Unique Number KEY
1999-02-26 17:42 Unique Number KEY Patrice DUMAS <pdumas@inforoutes-ardeche.fr>
@ 1999-02-26 18:25 ` CIMPOESU Teodor <teo@digiro.net>
1999-02-26 22:45 ` Re: [SQL] Unique Number KEY Christophe Labouisse <christophe.labouisse@dial.oleane.com>
0 siblings, 1 reply; 4+ messages in thread
From: CIMPOESU Teodor @ 1999-02-26 18:25 UTC (permalink / raw)
To: Patrice DUMAS <pdumas@inforoutes-ardeche.fr>; +Cc: pgsql-sql
Patrice DUMAS wrote:
> Hello,
>
> What is the best way, with PostGres, to create a Unique Number Key
> Generator?
>
> For example, I have a STUDENTS table:
> {
> StudentNumber integer;
> StudentName char[40];
> ...
> }
you have 2 ways to do it:
1. try something like
CREATE TABLE STUDENTS(
studentID int4 DEFAULT 'int(now())',
studentName text );
then if you say:
INSERT INTO STUDENTS (studentName) VALUES ('Patrice DUMAS jr.') ;
you will get an unique ID generated from a timestamp returned by now() .
2. by using a sequence.See \h create sequence and nextval() function.
This is a better way to get an ordered ID ( with an increment specified
by you).
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [SQL] Unique Number KEY
1999-02-26 17:42 Unique Number KEY Patrice DUMAS <pdumas@inforoutes-ardeche.fr>
1999-02-26 18:25 ` Re: [SQL] Unique Number KEY CIMPOESU Teodor <teo@digiro.net>
@ 1999-02-26 22:45 ` Christophe Labouisse <christophe.labouisse@dial.oleane.com>
0 siblings, 0 replies; 4+ messages in thread
From: Christophe Labouisse @ 1999-02-26 22:45 UTC (permalink / raw)
To: pgsql-sql
CIMPOESU Teodor <teo@digiro.net> writes:
> you have 2 ways to do it:
> 1. try something like
> CREATE TABLE STUDENTS(
> studentID int4 DEFAULT 'int(now())',
> studentName text );
Is suspect int(now()) to have a resolution of one second. Therefore
this will not work if you insert more than one record in a second.
Christophe Labouisse : Cinéma, typographie, Unix
christophe.labouisse@dial.oleane.com http://www.multimania.com/gabuzo/
Le cinéma en Lumière : http://www.lumiere.org/
^ permalink raw reply [nested|flat] 4+ messages in thread
* RE: [SQL] Unique Number KEY
@ 1999-02-26 18:34 Jackson, DeJuan <djackson@cpsgroup.com>
0 siblings, 0 replies; 4+ messages in thread
From: Jackson, DeJuan @ 1999-02-26 18:34 UTC (permalink / raw)
To: Patrice DUMAS <pdumas@inforoutes-ardeche.fr>; pgsql-sql
CREATE TABLE students (
Student_Number SERIAL PRIMARY KEY,
Student_Name VARCHAR(40)
);
Then:
INSERT INTO students(student_name) VALUES('Fred Strauss');
-- Student_Number should be 1
SERIAL will auto-create a sequence for you.
Look up SEQUENCES in the docs for more info on how to access and manage
the auto generated number.
> -----Original Message-----
> From: Patrice DUMAS [mailto:pdumas@inforoutes-ardeche.fr]
> Sent: Friday, February 26, 1999 11:43 AM
> To: pgsql-sql@postgreSQL.org
> Subject: [SQL] Unique Number KEY
>
>
> Hello,
>
> What is the best way, with PostGres, to create a Unique Number Key
> Generator?
>
>
> For example, I have a STUDENTS table:
> {
> StudentNumber integer;
> StudentName char[40];
> ...
> }
>
> Beginner in SQL, I think I must use Primary key or something
> like that, to
> be sure StudentNumber is unique.
> But, I want StudentNumber to be generated automatically, the
> user don't
> worry in assigning a StudentNumber.
>
> What is the best way to do that with PostGres-SQL ?
>
> Thank you for your answers and sorry for my poor english,
>
>
>
>
> Patrice DUMAS
> Ardèche, FRANCE.
> pdumas@inforoutes-ardeche.fr
>
>
>
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~1999-02-26 22:45 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-02-26 17:42 Unique Number KEY Patrice DUMAS <pdumas@inforoutes-ardeche.fr>
1999-02-26 18:25 ` CIMPOESU Teodor <teo@digiro.net>
1999-02-26 22:45 ` Christophe Labouisse <christophe.labouisse@dial.oleane.com>
1999-02-26 18:34 RE: [SQL] Unique Number KEY Jackson, DeJuan <djackson@cpsgroup.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox