Http web server - 634 CHAPTER 28 FROM DATABASES TO DATATYPES
Monday, April 21st, 2008634 CHAPTER 28 FROM DATABASES TO DATATYPES Sequence Functions The primary interaction with sequences is handled through several sequence-manipulation functions. The functions allow you to retrieve and manipulate the values within a sequence. nextval The nextval function is used to generate the next value of a sequence. We ll discuss functions more in Chapter 33, but for now the syntax should be straightforward enough: SELECT nextval(’sequence_name’); currval The currval function is used to determine the most recently obtained value of a sequence on the given connection. This assumes you have called nextval at least once during the session; otherwise, currval will fail. The syntax follows that of nextval: SELECT currval (’sequence_name’); lastval The lastval function, new in PostgreSQL 8.1, operates similarly to currval, except that instead of explicitly stating the sequence to be called against, lastval automatically returns the value of the last sequence nextval was called against: SELECT lastval(); This makes it a little easier to manipulate tables, because you can insert into a table and retrieve the generated serial key value without having to know the name of the sequence. Like currval, calling lastval in a session where nextvalhas not been called will generate an error. setval The last of the sequence-manipulation functions, setval is used to set a sequence s value to a specified number. The setvalfunction actually offers two different syntaxes, the first of which follows: SELECT setval(’sequence_name’,value); This version of setval is fairly straightforward, setting the named sequence s value to value. Once setval has been executed in this way, subsequent nextval calls will begin returning the next value based on the sequence definition. For example, if you call setval on a sequence and give it a value of 2112, calling nextval on the sequence will return 2113, and then increase from there. Optionally, you can pass in a third value to setval to control this behavior, using the following syntax: SELECT setval(’sequence_name’, value, is_called); In this form, the value determines if the sequence will treat the number passed in as having been called before. By setting is_called as TRUE, you achieve the same behavior as the two-parameter form of setval; however, by setting is_called as FALSE, the sequence will start
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.