Ressources documentaires pour Mandriva Linux et les Logiciels Libres

Billets dans la catégorie Trucs & Astuces

PostgreSQL tips : quickly add a surrogate key column

Suppose that you want to add a surrogate key to a table in PostgreSQL to help you quickly identify each rows in a unique way. You can do this easily and quickly by just adding a SERIAL type column. When adding a SERIAL type column to an already filled table, PostgreSQL will automatically fill the column with some values.
To ensure your surrogate key uniqueness, don’t forget to add the UNIQUE constraint : on top of that this will create an index, thus speeding up your join queries on this column.

[sql light= »true »]ALTER TABLE mytable ADD COLUMN id SERIAL UNIQUE;[/sql]



My Tweets