473,386 Members | 1,741 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,386 software developers and data experts.

Please help

CY
Hello list

I hope someone can gave me a hint / advice on this.

I wanted a auto-increasement function, similar to sequence, to increase
a field. I cannot use CREATE SEQUENCE because it cd_line_no will start
again with a new coursedetail.

TQ in advance.

TABLE
=====
CREATE TABLE coursedetail
(
cd_cf_id char(30),
cd_line_no smallint default auto_increment by 1,
cd_name char(40),
cd_status char(2),

CONSTRAINT cd_pkey PRIMARY KEY (cd_cf_id, cd_line_no),
FOREIGN KEY (cd_cf_id) REFERENCES course (cf_id)
);


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #1
2 1658
CY wrote:
I wanted a auto-increasement function, similar to sequence, to increase
a field. I cannot use CREATE SEQUENCE because it cd_line_no will start
again with a new coursedetail.

CREATE TABLE coursedetail
(
cd_cf_id char(30),
cd_line_no smallint default auto_increment by 1,
cd_name char(40),
cd_status char(2),

CONSTRAINT cd_pkey PRIMARY KEY (cd_cf_id, cd_line_no),
FOREIGN KEY (cd_cf_id) REFERENCES course (cf_id)
);


Don't really understand why you can't use CREATE SEQUENCE... are you
saying you need a unique, unused smallint for cd_line_no, for a given
cd_cf_id (noting that (cd_cf_id, cd_line_no) is the primary key)?

This is probably a bad design, but you could try
CREATE FUNCTION cd_nextval(CHAR(30)) RETURNS smallint LANGUAGE sql AS
'SELECT coalesce(max(cd_line_no), -1) + 1 FROM coursedetail WHERE
cd_cf_id = $1';

Need to change some of your application code to handle it, and your
coursedetail table definition a bit, but, hey, can't do all your
homework for you ;)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/pfBgNYbTUIgzwfARAvusAJ9nMvlpEIIqXwm57p1YhINcQqBSnw CeOgAq
JCNEeCrY3BbdMR1AbY2SVWY=
=ef8G
-----END PGP SIGNATURE-----

Nov 12 '05 #2
Reposting this back to the pgsql-general list, others might have better
insights into this.

CY wrote:
Dear Ang

Hi - thanks for your reply.

I have master/detail record where the detail records each lineitem of
each master
(similar to order / order entry type of structure). The SEQUENCE is not
suitable
being that I have many master records and won't want whole lots of
SEQUENCE table
at the backend. Thus, I wanted is a small function that is similar to
sequence in Postgresql
to do an "auto-itemising" lineitem.
Oh okay, I think I've got what you wanted to do:

Given that master contains one or more details, you'd have a table
"details" like

primary key = (master_id, detail_id);
master_id | detail_id | data...
----------+-----------+---------
1 | 1 | ....
1 | 2 |
2 | 1 |

And that when you want to insert another detail with master_id = 3, you
want it to have detail_id = 1. Or when inserting a new detail with
master_id = 1, you want detail_id = 3.
Whether it is bad design - I do not really know. Your email DID make
me think a harder
from that angle.
Okay, the function I gave before would work for single users, but it
might return the same key to different concurrent users -> bad thing.
Can't really avoid the problem if you insist on detail_id to be
sequential from 1 for every master_id.

Another way would be to use a SEQUENCE on detail_id, and bear with the
fact that it doesn't start from 1... just use

SELECT * FROM detail WHERE master_id = $foo ORDER BY detail_id;

and number them as you receive them in your application.

To get the $n-th detail, use:

SELECT * FROM detail WHERE master_id = $foo ORDER BY detail_id LIMIT $n, 1;

But that'll pose performance problems when $n is large.
Hmmmm... there're problems either way. Anyone out there with better ideas?
As I am new to Postgresql and SQL, I depend examples from guidebooks and
help from peple
like you.

BTW, how do you use COALESCE - I know it is keyword but the Postgresql
manuals had no
record of it. Could you recommend where I can find good examples of
Postgresql
- something like Postgresql Cookbook - if any.


Section 6.12.2 of the 7.3 docs:
http://www.postgresql.org/docs/7.3/i...l.html#AEN9753

The better examples are in the mailing list and the archives. Read
through them, and bookmark/save the interesting ones.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/p0w+NYbTUIgzwfARAv2xAJ0eAr/QT0SGI8I58PsBFz4VovvLOgCfYzEE
qZVG59GFYtdl2Mf2uGVTCAs=
=Jr5k
-----END PGP SIGNATURE-----

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kurt Watson | last post by:
I’m having a different kind of problem with Hotmail when I sign in it says, "Web Browser Software Limitations Your Current Software Will Limit Your Ability to Use Hotmail You are using a web...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
13
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
1
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and...
0
by: 2Barter.net | last post by:
newsmail@reuters.uk.ed10.net Fwd: Money for New Orleans, AL & GA Inbox Reply Reply to all Forward Print Add 2Barter.net to Contacts list Delete this message Report phishing Show original
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
5
by: tabani | last post by:
I wrote the program and its not giving me correct answer can any one help me with that please and specify my mistake please it will be highly appreciable... The error arrives from option 'a' it asks...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.