Search code examples
postgresqldrop-down-menupgadmin

How do I create a drop down list?


I'm new to PostgreSQL.

I was wondering, how do I make a column a drop down list.

So i've got a table called Student. There's a column in there called "student_type", which means whether the student is a part time student, full time student or is sandwich course student.

So I want to make "student_type" a drop down list with 3 choices: "part time" student, "full time" and "sandwich".

How do I do this?

(I'm using pgAdmin to create the databse, by the way.)


Solution

  • A drop-down is a client side thing and should be dealt with accordingly. But as far as a relational database is involved there should exist a student_type relation with the id and type columns which you would query like this:

    select st.id, st.type
    from student_type st
    inner join student s on s.type_id = st.id 
    group by st.id, st.type
    order by st.type
    

    The inner join is to make sure you don't show an option that does not exist in the student table and would therefore produce an empty result if chosen. In the client side the id should be the option value and the type the option text.

    If there is no student_type relation as a consequence of bad db design or if you are only allowed to query a denormalized view, you can still use the student relation:

    select distinct student_type
    from student
    order by student_type
    

    In this case the student_type will be both the option value and the option text.