Monday, October 25, 2010

Messages On New Arrival

PostgreSQL database: Sequence Control

In PostgreSQL you can create tables with "primary key" autoincremented, as I observed in the following example.

create table students (id SERIAL not null
name varchar (255) NULL, constraint
pk_beneficios primary key (id));

autoincremented This will create a sequence for the "primary key" as shown below.

id integer not null default nextval ('alumnos_id_seq':: regclass)

Some times it is necessary to observe or modify the sequence to perform these operations can be Use the following sentences.

  • "SELECT FROM last_value nombre_secuencia," returns the last value in the sequence.
  • "SELECT nextval ('nombre_secuencia');": Returns the value of the last sequence number and increments by 1.
  • "SELECT setval ('nombre_secuencia', value)": Set the value to the stream, forcing nextval to return (value + 1).
  • "SELECT setval ('nombre_secuencia', value, true);" It works the same way as the previous sentence.
  • "SELECT setval ('nombre_secuencia', value, false);": Set the value to the stream, forcing a return nextval (value).
  • "SELECT currval ('nombre_secuencia');": Returns the value of the last number in the sequence.
The nombre_secuencia can be replaced by "alumno_id_seq" the first example, while the value is an integer greater than 0 (zero).

"Thank you for sharing your knowledge"


Post a Comment