![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I was considering using an array to hold catagorization information in my db. So to use say books as an example, Id have a record for each book and a text array column contain the list of catagories it belonged to, such as: {"sci-fi", "horror", "suspense"} Regularly Ill have to search this db for records containing a specific catagory. For example get all books belonging to the sci-fi catagory, for the most parts the searchs will only look for a single catagory. They would look like this : SELECT * FROM my_table WHERE 'sci-fi' = ANY (catagory); What would be a better method of doing this? |
#3
| |||
| |||
|
|
Regularly Ill have to search this db for records containing a specific catagory. For example get all books belonging to the sci-fi catagory, for the most parts the searchs will only look for a single catagory. They would look like this : SELECT * FROM my_table WHERE 'sci-fi' = ANY (catagory); Is that bad design? |
#4
| |||
| |||
|
|
On Aug 8, 2004, at 2:03 PM, A Gilmore wrote: Regularly Ill have to search this db for records containing a specific catagory. For example get all books belonging to the sci-fi catagory, for the most parts the searchs will only look for a single catagory. They would look like this : SELECT * FROM my_table WHERE 'sci-fi' = ANY (catagory); Is that bad design? Probably. I'd set up a categories table that lists the possible categories. Then create a books_categories table that lists the categories for each book. For example: create table books ( book_id serial not null unique , title text not null ); create table categories ( category_name text not null unique ); create table books_categories ( book_id integer not null references books (book_id) on update cascade on delete cascade , category_name text not null references categories (category_name) on update cascade on delete cascade , unique (book_id, category_name) ); Michael Glaesemann grzm myrealbox com |
#5
| |||
| |||
|
|
Is this correct, that transactions will not help, and if so what is the most common way of dealing with this issue (since I assume its common) ? |
#6
| |||
| |||
|
|
Thanks a lot of the detailed reply, its a huge help. Does bring me to one other question Id like to clarify. Using this method Ill be doing inserts into books_categories based on the ID of the latest INSERT into the books table. Since I cannot really have a INSERT return a value such as the serial used without getting into PL/pgSQL, how can I get the ID of the book just inserted? According to board threads I have found doing something like : select book_id from books where book_id = currval('book_id_seq'); Could lead to problems if multiple inserts are occuring at the same time since currval() may have changed since the insert, and transactions do not prevent that. Is this correct, that transactions will not help, and if so what is the most common way of dealing with this issue (since I assume its common) ? |
#7
| |||
| |||
|
|
Well, I assume that all your book titles are unique, |
#8
| |||
| |||
|
|
On Aug 9, 2004, at 6:42 PM, Oliver Fromme wrote: Well, I assume that all your book titles are unique, Realistically, probably not a good assumption. Not one I'd want to bank by db schema on. You might be luckier with something like ISSN, but I don't know enough about re-use of those types of numbers. |
#9
| |||
| |||
|
|
Michael Glaesemann wrote: On Aug 9, 2004, at 6:42 PM, Oliver Fromme wrote: Well, I assume that all your book titles are unique, Realistically, probably not a good assumption. Not one I'd want to bank by db schema on. You might be luckier with something like ISSN, but I don't know enough about re-use of those types of numbers. My example was a bit simplified for the matter. Probably those rows contain more information than just the title, but also the author, publisher, date, ISSN/ISBN etc. I think it is a reasonable assumption that the whole rows (excluding the serial IDs) are unique. Best regards Oliver -- Oliver Fromme, secnetix GmbH & Co KG, Oettingenstr. 2, 80538 München Any opinions expressed in this message may be personal to the author and may not necessarily reflect the opinions of secnetix in any way. "I have stopped reading Stephen King novels. Now I just read C code instead." -- Richard A. O'Keefe ---------------------------(end of broadcast)--------------------------- TIP 6: Have you searched our list archives? http://archives.postgresql.org |
#10
| |||
| |||
|
|
Michael Glaesemann wrote: On Aug 9, 2004, at 6:42 PM, Oliver Fromme wrote: My example was a bit simplified for the matter. |
![]() |
| Thread Tools | |
| Display Modes | |
| |