10

I'm trying to create a table as follows:

CREATE TABLE SCHEDULE (
 SESSIONID                  SERIAL,
 MODULECODE                 VARCHAR(10),
 CONSTRAINT SCHEDULE_FOREIGN_KEY FOREIGN KEY (MODULECODE) REFERENCES MODULES (MODULECODE),
 CONSTRAINT SCHEDULE_PRIMARY_KEY PRIMARY KEY (SESSIONID, MODULECODE));

The idea being that SESSION ID would auto increment with each new row but only local to MODULECODE, for example:

----------------------
|SESSIONID|MODULECODE|
|---------|----------|
|    1    |    A     |
|    2    |    A     |
|    3    |    A     |
|    1    |    B     |
|    2    |    B     |
|    1    |    C     |
|    2    |    C     |
|--------------------|

I believe this is how AUTO_INCREMENT functions in MySQL but I suspect PostgreSQL doesn't work this way. How else would I achieve this in PostgreSQL?

7
  • 1
    MySQL's auto-increment doesn't work like that, and neither does Postgres' serial type. It would be a lot easier if you generate those numbers when displaying the data e.g. using row_number() and just keep the sessionid as an overall unique value. Commented Dec 3, 2015 at 19:36
  • I should add that session ID is not meant to be a unique value. It's unique only to the module, so that a module may have any number of sessions that can be differentiated between. Is there a way I can achieve this? Commented Dec 3, 2015 at 19:38
  • 2
    You could maybe do something with a BEFORE INSERT trigger...? Commented Dec 3, 2015 at 19:43
  • IMSoP, I'm pretty new to SQL I'll admit. I understand how triggers work but I'm not sure where I'd start in constructing one to achieve the functionality above? Any ideas would be awesome :) Commented Dec 3, 2015 at 19:52
  • 2
    @IMSoP the problem is will also need after update/delete triggers, and things can become messy. Commented Dec 3, 2015 at 19:59

2 Answers 2

2

Show the data as suggested by @Juan

select
    row_number() over (
        partition by modulecode order by modulecode
    ) as sessionid, 
    modulecode
from schedule

Then when the user asks for a certain sessionid from a certain module do:

select *
from schedule
where sessionid = (
    select sessionid
    from (
        select
            sessionid,
            row_number() over (order by sessionid) as module_sessionid
        from schedule
        where modulecode = 'B'
    ) s
    where module_sessionid = 2
)
Sign up to request clarification or add additional context in comments.

Comments

1

as hourse said you cant do it on your db. But you can asign those values in the select

 SELECT row_number() over (partition by MODULECODE order by MODULECODE) as SESSIONID, 
        MODULECODE
 FROM YourTable

2 Comments

Ah, I see. That gives me the data in the format I want, however, I want another table where a Student is able to select a particular SessionID for each module in the format that this gives. At the moment, SessionID is a unique index in the db which means that students aren't able to pick session 1 or 2 for module B and instead have to pick session 4 or 5. Any thoughts?
Sorry @Jamie4840, you lost me. Maybe if you update your question with some sample? For start what if you put this select on a table function? and use it instead of real table? But if that doesnt work for you I recommend you create a new question with the whole problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.