|
10 | 10 | alink="#0000ff">
|
11 | 11 | <H1>Frequently Asked Questions (FAQ) for PostgreSQL</H1>
|
12 | 12 |
|
13 |
| - <P>Last updated: Mon Oct 8 23:19:46 EDT 2007</P> |
| 13 | + <P>Last updated: Tue Oct 9 15:52:10 EDT 2007</P> |
14 | 14 |
|
15 | 15 | <P>Current maintainer: Bruce Momjian (<A href=
|
16 | 16 | "mailto:bruce@momjian.us">bruce@momjian.us</A>)
|
@@ -916,38 +916,28 @@ <H3 id="item4.11.1">4.11.1) How do I create a
|
916 | 916 | );
|
917 | 917 | </PRE>
|
918 | 918 |
|
919 |
| - See the <I>create_sequence</I> manual page for more information |
920 |
| - about sequences. |
| 919 | + <P>Automatically created sequence are named |
| 920 | + <<I>table</I>>_<<I>serialcolumn</I>>_<I>seq</I>, where |
| 921 | + <I>table</I> and <I>serialcolumn</I> are the names of the table and |
| 922 | + <SMALL>SERIAL</SMALL> column, respectively. See the |
| 923 | + <I>create_sequence</I> manual page for more information about |
| 924 | + sequences.</P> |
921 | 925 |
|
922 | 926 | <H3 id="item4.11.2">4.11.2) How do I get the value of a
|
923 | 927 | <SMALL>SERIAL</SMALL> insert?</H3>
|
924 | 928 |
|
925 |
| - <P>One approach is to retrieve the next <SMALL>SERIAL</SMALL> value |
926 |
| - from the sequence object with the <I>nextval()</I> function |
927 |
| - <I>before</I> inserting and then insert it explicitly. Using the |
928 |
| - example table in <A href="#item4.11.1">4.11.1</A>, an example in a |
929 |
| - pseudo-language would look like this:</P> |
930 |
| -<PRE> |
931 |
| - new_id = execute("SELECT nextval('person_id_seq')"); |
932 |
| - execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')"); |
933 |
| -</PRE> |
| 929 | + <P>The simplest way is to retrieve the assigned <SMALL>SERIAL</SMALL> |
| 930 | + value with <SMALL>RETURNING</SMALL>. Using the example table in <A |
| 931 | + href="#item4.11.1">4.11.1</A>, it would look like this:</P> |
934 | 932 |
|
935 |
| - You would then also have the new value stored in <CODE>new_id</CODE> |
936 |
| - for use in other queries (e.g., as a foreign key to the <CODE>person |
937 |
| - </CODE> table). Note that the name of the automatically created |
938 |
| - <SMALL>SEQUENCE</SMALL> object will be named <<I>table</I>>_<<I> |
939 |
| - serialcolumn</I>>_<I>seq</I>, where <I>table</I> and <I>serialcolumn</I> |
940 |
| - are the names of your table and your <SMALL>SERIAL</SMALL> column, |
941 |
| - respectively. |
942 |
| - |
943 |
| - <P>Alternatively, you could retrieve the assigned <SMALL>SERIAL</SMALL> |
944 |
| - value with the <I>currval()</I> function <I>after</I> it was inserted by |
945 |
| - default, e.g.,</P> |
946 | 933 | <PRE>
|
947 |
| - execute("INSERT INTO person (name) VALUES ('Blaise Pascal')"); |
948 |
| - new_id = execute("SELECT currval('person_id_seq')"); |
| 934 | + INSERT INTO person (name) VALUES ('Blaise Pascal') RETURNING id; |
949 | 935 | </PRE>
|
950 | 936 |
|
| 937 | + You can also call <I>nextval()</I> and use that value in the |
| 938 | + <SMALL>INSERT</SMALL>, or call <I>currval()</I> <I>after</I> the |
| 939 | + <SMALL>INSERT</SMALL>. |
| 940 | + |
951 | 941 | <H3 id="item4.11.3">4.11.3) Doesn't <I>currval()</I>
|
952 | 942 | lead to a race condition with other users?</H3>
|
953 | 943 |
|
|
0 commit comments