Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Sql > Re: Sequential ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 3380 of 3799
Post > Topic >>

Re: Sequential non unique IDs

by yazicivo@[EMAIL PROTECTED] (Volkan YAZICI) Apr 2, 2008 at 08:48 AM

On Tue, 1 Apr 2008, "John Reeve" <jreeve@[EMAIL PROTECTED]
> writes:

> I have the following scenario:
>  
> A 'task' table that has the fields:
>     id => primary key, updated on each insert using a sequence
>     customerid => integer
>     localid => integer
>  
> I need the localid to be sequential and unique per unique customerid.
The data needs to look like this:
>     1, 92, 1
>     2, 92, 2
>     3, 93, 1
>     4, 93, 2
>     5, 93, 3
>     6, 92, 3
>     and so on
>  
> I am presently doing this on the INSERT using an INNER SELECT, like
this:
>  
> INSERT INTO task (id, customerid, localid) VALUES (nextval('task_id'),
> 92, (SELECT MAX(localid) + 1 FROM task WHERE customerid = 92));

Why not creating a separate serial for localid field? It won't
contradict with your making localid to be sequential and unique per
unique customerid restriction.

CREATE TABLE task (
    id            serial    PRIMARY KEY,
    customerid    integer,
    localid       serial
);

CREATE UNIQUE INDEX task_customerid_localid_idx
    ON task (customerid, localid);

INSERT INTO task (customerid) VALUES (92);

If I didn't get you wrong, this should solve your problem.


Regards.

-- 
Sent via pgsql-sql mailing list (pgsql-sql@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
 




 4 Posts in Topic:
Sequential non unique IDs
jreeve@[EMAIL PROTECTED]   2008-04-01 15:49:46 
Re: Sequential non unique IDs
craig@[EMAIL PROTECTED]   2008-04-02 12:24:16 
Re: Sequential non unique IDs
jreeve@[EMAIL PROTECTED]   2008-04-06 20:18:24 
Re: Sequential non unique IDs
yazicivo@[EMAIL PROTECTED  2008-04-02 08:48:03 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Dec 1 22:03:40 CST 2008.