473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Get the generated value of serial type field (primary key)

66
Expand|Select|Wrap|Line Numbers
  1. -- Table: public.customers
  2. CREATE TABLE public.customers (
  3.   customercd   serial NOT NULL,
  4.   pass         char(32) NOT NULL,
  5.   lnamek       varchar(20) NOT NULL,
  6.   fnamek       varchar(20) NOT NULL,
  7.   status       boolean NOT NULL DEFAULT true,
  8.   /* Keys */
  9.   CONSTRAINT customers_pkey
  10.     PRIMARY KEY (customercd)
  11. ) WITHOUT OIDS;
  12.  
  13. CREATE INDEX customers_index01
  14.   ON public.customers
  15.   (customercd, status);
Expand|Select|Wrap|Line Numbers
  1. -- Table: public.customercontacts
  2. CREATE TABLE public.customercontacts (
  3.   customercd  integer NOT NULL,
  4.   contact     varchar(50) NOT NULL,
  5.   "type"      varchar(3),
  6.   /* Keys */
  7.   CONSTRAINT customercontacts_pkey
  8.     PRIMARY KEY (contact),
  9.   /* Foreign keys */
  10.   CONSTRAINT foreign_key01
  11.     FOREIGN KEY (customercd)
  12.     REFERENCES public.customers(customercd)
  13.     ON DELETE RESTRICT
  14.     ON UPDATE CASCADE
  15. ) WITHOUT OIDS;
1) insertCustomer(pass, fnamek, lnamek)
-- no need to provide the customercd and status because auto-increment and with default value respectively.

2) insertCustomerContact(customercd, contact, type)
-- need to provide the all fields.

Problem: How can I determine the previously used customercd that generated by the serial field upon insertCustomer in order to pass the customercd as argument to insertCustomerContact.
Aug 10 '07 #1
6 6240
eros
66
I found something that may be the solution..but please confirmed by the expert...

This is safe to use? That the value of last_value field from customers_customercd_seq is always the previously used/added upon insert?
Expand|Select|Wrap|Line Numbers
  1. insert into customercontacts (customercd, contact, 'type')
  2. values (select last_value from customers_customercd_seq,'09084789545', 'MOB');
BUT error occurred Error:
ERROR: syntax error at or near "'type'";
Error while executing the query (State:42601, Native Code: 7)



When I removed the single quote ( ' ) :
Expand|Select|Wrap|Line Numbers
  1. insert into customercontacts (customercd, contact, type)
  2. values (select last_value from customers_customercd_seq,'09084789545', 'MOB');
also error occurred:
Error: ERROR: syntax error at or near "select";
Error while executing the query (State:42601, Native Code: 7)


I am using WinSQL via ODBC
Aug 10 '07 #2
michaelb
534 Expert 512MB
Try this:
Expand|Select|Wrap|Line Numbers
  1. insert into customercontacts (customercd, contact, type)
  2. values 
  3. (select last_value, '09084789545', 'MOB' from customers_customercd_seq,)
  4.  
Aug 10 '07 #3
michaelb
534 Expert 512MB
As far as getting the last auto number getting it off the last_value field is not the best option. This field shows the last value allocated by any session, meaning that if you have concurrent or nearly concurrent processes you may get the wrong value. I would recommend using the currval function instead.

Generally speaking having to obtain the previously auto generated number is a red flag.
If you are doing some class project it's ok, but if you are working on a real life application we may need to consider other options.
Aug 10 '07 #4
eros
66
As far as getting the last auto number getting it off the last_value field is not the best option. This field shows the last value allocated by any session, meaning that if you have concurrent or nearly concurrent processes you may get the wrong value. I would recommend using the currval function instead.

Generally speaking having to obtain the previously auto generated number is a red flag.
If you are doing some class project it's ok, but if you are working on a real life application we may need to consider other options.
What's the other options? It is currval function?
Aug 13 '07 #5
michaelb
534 Expert 512MB
What's the other options? It is currval function?
No, currval is the first option - getting value directly from the last_value field is simply a bad and unsafe approach.
The alternative would be to create the sequence table manually, not associating it with your key field on the data table.
In this case you will be responsible for getting the next value from the sequence and using it to insert the row in your table.
This is a trade-off, and you would need to weight pros and cons.
With what you have now everything is automated - you don't have to worry about getting the next key, or maintaining the sequence - if you drop the table the sequence gets dropped, if you create the table the sequence gets created.
But you have to do something to obtain the key of your record, which is from the purist point of view pretty bad.
If you manage the keys manually you don't have to do anything to find its value - you have it; but on the other hand you are responsible for the whole business of maintaining the sequence table and getting the values from there.
I had experience with both of this techniques, there's no "better" way, but there is a way that could be better for a particular situation.
Follow these links for more information:
http://www.postgresql.org/docs/8.2/s...esequence.html
http://www.postgresql.org/docs/8.2/s...-sequence.html
Aug 14 '07 #6
eros
66
No, currval is the first option - getting value directly from the last_value field is simply a bad and unsafe approach.
The alternative would be to create the sequence table manually, not associating it with your key field on the data table.
In this case you will be responsible for getting the next value from the sequence and using it to insert the row in your table.
This is a trade-off, and you would need to weight pros and cons.
With what you have now everything is automated - you don't have to worry about getting the next key, or maintaining the sequence - if you drop the table the sequence gets dropped, if you create the table the sequence gets created.
But you have to do something to obtain the key of your record, which is from the purist point of view pretty bad.
If you manage the keys manually you don't have to do anything to find its value - you have it; but on the other hand you are responsible for the whole business of maintaining the sequence table and getting the values from there.
I had experience with both of this techniques, there's no "better" way, but there is a way that could be better for a particular situation.
Follow these links for more information:
http://www.postgresql.org/docs/8.2/s...esequence.html
http://www.postgresql.org/docs/8.2/s...-sequence.html
Yes, I realized that you are strongly correct. I need to have a table that maintain all the keys.

On the case of manually maintain keys in the table, I can also automate the generation of keys based on the table.

Thank you very much.
Aug 15 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Paul Lamonby | last post by:
Hi, i want to create a unique serial number to my Db entries. I thought the best way would be to add the auto_increment primary key value to a string, then insert it into a table field...
5
by: Marek Lewczuk | last post by:
I'm curious if the default scheme for sequence name (which is created with SERIAL data type) can be changed -- currently all sequences are named like this: <table_name>_<field_name>_seq -- can it...
3
by: Poul Møller Hansen | last post by:
Hi, I need an auto incrementing field that will contain values like N000001, N000002, N000003 etc. I think the way is to use the value from an identity field in a stored procedure that is...
12
by: Prabu Subroto | last post by:
Dear my friends... I am using postgres 7.4 and SuSE 9.1. I want to use auto_increment as on MySQL. I look up the documentation on www.postgres.com and I found "serial" . But I don't know...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
33
by: bill | last post by:
In an application I am writing the user can define a series of steps to be followed. I save them in a sql database using the field "order" (a smallint) as the primary key. (there are in the range...
6
by: Andrus | last post by:
I need to create generic table field level cache. Table primary key (PrimaryKeyStructType) can be int, string or struct containing int and string fields. FieldName contains table field name to be...
8
by: Elfae | last post by:
I have searched high and low for a sample for this, and I just can't find any. Sorry for the length! Background Information The issue revolves around setting up a system-generated increase in...
1
by: Frank Swarbrick | last post by:
We're trying to take advantage of the new ROW CHANGE TIMESTAMP option. Here is a simple table: CREATE TABLE "ACCTASGN"."NUMBER_STATUS" ( "STATUS_CODE" CHAR(1) NOT NULL , "STATUS_DESCRIPTION"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.