Sequence

From: Alex Cheshev <alex(dot)cheshev(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Sequence
Date: 2008-07-26 01:21:10
Message-ID: 488A7C06.8000202@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello.

Each user has a lot of guests. Each guest only has one user.

1. I create a table users:

CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
name varchar(256)
);

2. I create a table guests:

CREATE TABLE guests (
user_id integer,
guest_id SERIAL,
PRIMARY KEY (user_id, guest_id),
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
)

3. I add two new users:

insert into users (name) values ('alex2008');
insert into users (name) values ('jack2008');

select * from users;
user_id | name
---------+----------
1 | alex2008
2 | jack2008
(2 rows)

4. I add two new guests to the user alex2008 and one new guest to the
user jack2008:

insert into guests (user_id, name) values (1, 'Mark Twain');
insert into guests (user_id, name) values (1, 'Anna Black');
insert into guests (user_id, name) values (2, 'John Black');

select * from guests;
user_id | guest_id | name
---------+----------+------------
1 | 1 | Mark Twain
1 | 2 | Anna Black
2 | 3 | John Black
(3 rows)

I want to have a different result of insert command:

user_id | guest_id | name
---------+----------+------------
1 | 1 | Mark Twain
1 | 2 | Anna Black
2 | 1 | John Black

Sequence guests_guest_id_seq is not connected with the field user_id. It
increases in spite of values of user_id. How can I solve this problem?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John DeSoi 2008-07-26 01:36:13 Re: A couple of newbie questions ...
Previous Message Danyelle Gragsone 2008-07-25 22:22:42 Re: Help Me !!!