If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Linked Lists?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-02-11, 16:10
mikesmith1287 mikesmith1287 is offline
Registered User
 
Join Date: Sep 2011
Posts: 1
Linked Lists?

Is there an easy way to created linked lists on postgresql? Is there some resource someone can point me to because I can't seem to find one on google.

Basically I want to be able to store posts in a certain order. Users can add new posts in between any other 2 posts so the order will definitely behave like a list. I heard somewhere that mysql couldn't do it but pgsql could.

Thanks!!
Reply With Quote
  #2 (permalink)  
Old 09-02-11, 16:35
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
I don't understand why you need a "linked" list to store something with an order.

Just add an "sort" column to your posts table and you are all set.

Code:
CREATE TABLE posts
(
   id              serial not null primary key,
   thread_id       integer not null,
   posting_date    timestamp not null default current_timestamp,
   title           text not null,
   order_in_thread integer not null,
   constraint fk_post_thread
        foreign key (thread_id) references threads (id)
)

If you really insist on doing a linked list, you just need to add a foreign key pointing to the "previous" post.

Code:
CREATE TABLE posts
(
   id                serial not null primary key,
   thread_id         integer not null,
   posting_date      timestamp not null default current_timestamp,
   title             text not null,
   previous_post_id  integer not null, 
   constraint fk_prev_pos 
       foreign key (previous_post_id) references posts (id),
   constraint fk_post_thread
        foreign key (thread_id) references threads (id)
)
The second solution is actually a lot easier to deal with in Postgres than in MySQL because PostgreSQL supports recursive queries which make it very easy to retrieve such a structure.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On