[lug] (OT) postgresql
Bear Giles
bgiles at coyotesong.com
Sun Nov 10 17:56:25 MST 2002
> is there a postgresql select query i can enter to specify
> the last row of a table...
select max(inv_num) from customers;
to get the specific invoice number, or
select * from customers
where inv_num = (select max(inv_num) from customers);
to get the full row.
> i.e. i want to make a new invoice number based
> on the inv_num field of the last customer entry into a table.
so you do NOT want to generate a new invoice number from the existing
table. If you do, you must somehow guarantee that you will never
establish more than a single connection to the database, and never more
than one query at any time, otherwise your code may reuse the same
invoice number because of concurrent access.
What you really want to use is a "sequence" - it will guarantee
uniqueness. IIRC, it's something like
create sequence inv_num;
select next_val(inv_num);
You should also add 'unique' to the column constraints for your inv_num
field. This will ensure that you never have two invoices with the same
number.
Bear
More information about the LUG
mailing list