I have a table showtime
and a table ticket
.
CREATE TABLE showtime (
showtime_id SERIAL PRIMARY KEY,
theatre_id INT REFERENCES theatre(theatre_id),
showroom_id INT REFERENCES showroom(showroom_id),
movie_id INT REFERENCES movie(movie_id),
start_time TIME,
end_time TIME,
show_date DATE
);
CREATE TABLE ticket (
showtime_id INT REFERENCES showtime(showtime_id),
seat_number SERIAL UNIQUE,
price NUMERIC(1000, 2) NOT NULL,
time_bought TIME,
date_bought DATE,
wasUsed BOOLEAN,
PRIMARY KEY (showtime_id, seat_number)
);
As you can see, each row in showtime
will have a unique showtime_id
. What I want to happen is when a new ticket
is added, the seat_number
should auto-increment from the last value tied with showtime_id
:
ticket
showtime_id | seat_number | instead_of |
1 | 1 | 1 |
1 | 2 | 2 |
1 | 3 | 3 |
1 | 4 | 4 |
2 | 1 | 5 |
2 | 2 | 6 |
3 | 1 | 7 |
4 | 1 | 8 |
Is this possible in PostgreSQL?