Обсуждение: Unique Number KEY

Поиск
Список
Период
Сортировка

Unique Number KEY

От
"Patrice DUMAS"
Дата:
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



Re: [SQL] Unique Number KEY

От
CIMPOESU Teodor
Дата:
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).




Re: [SQL] Unique Number KEY

От
Christophe Labouisse
Дата:
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/