Re: New arrivals (II)
- From: Andy Burns <usenet.oct2006@xxxxxxxxxxxxxx>
- Date: Wed, 27 Jun 2007 21:38:30 +0100
On 27/06/2007 20:26, Richard Robinson wrote:
I need to represent a number of items which each contain a (variable) number
of key/value pairs (and some other stuff, but never mind that). The names of
the keys are not known in advance, and the values for each key can be
multiple; and what I need to get back is a list of items, selected according
to various wotnottery and sorted on the (possibly multiple) values of one of
the keys, ie multiply-valued items appearing in each of the possible
positions. Which last is the bit that becomes amusing.
I've kind of assumes you're using M$ access, so I've convertified from ORACLE/Postgres in my head, not checked for sillies.
so you want an items table
I'll assume you are going to assign the id values, rather than let the database generate them
primary key (i.e unique index) on item.id
indexes on anyotherstuff that's appropriate
you don't need your extra /two/ tables, but can collapse them into one keyval table
let the database automagically set the id or set a DEFAULT value from a SEQUENCE
primary key (i.e. unique index) on keyval.id
probably want an index on (keyval.item_id)
or on (keyval.item_id + keyval.key)
you'll survive without one, but speed might suffer.
=======================================
CREATE TABLE item (
id INTEGER,
colour TEXT,
weight FLOAT,
PRIMARY KEY (id));
CREATE TABLE keyval (
id COUNTER,
item_id INTEGER,
key TEXT,
value TEXT,
PRIMARY KEY(id));
INSERT INTO item(id, colour, weight)
VALUES(1001, "blue", 19.3);
INSERT INTO keyval(item_id, key, value)
VALUES (1001, "blue_alpha", "aaaa");
INSERT INTO keyval(item_id, key, value)
VALUES (1001, "blue_beta", "bbbb");
INSERT INTO item(id, colour, weight)
VALUES(999, "green", 24.5);
INSERT INTO keyval(item_id, key, value)
VALUES (999, "green_gamma", "gggg");
INSERT INTO keyval(item_id, key, value)
VALUES (999, "green_delta", "dddd");
INSERT INTO keyval(item_id, key, value)
VALUES (999, "green_epsilon", "eeee");
INSERT INTO keyval(item_id, key, value)
VALUES (999, "green_epsilon", "another_eeee");
then you can do queries like
SELECT item.id, item.colour, item.weight,
keyval.key, keyval.value
FROM item, keyval
WHERE item.id = 999
AND keyval.item_id = item.id
ORDER BY keyval.key;
For bonus marks you can force referential integrity of the item_id to the item.id
ALTER TABLE keyval
ADD CONSTRAINT FK_keyval_item
FOREIGN KEY (item_id) REFERENCES
items (id);
.
- Follow-Ups:
- Re: New arrivals (II)
- From: Richard Robinson
- Re: New arrivals (II)
- References:
- Re: New arrivals (II)
- From: Guy King
- Re: New arrivals (II)
- From: Mr Guest
- Re: New arrivals (II)
- From: Richard Robinson
- Re: New arrivals (II)
- From: Mr Guest
- Re: New arrivals (II)
- From: Richard Robinson
- Re: New arrivals (II)
- From: coj
- Re: New arrivals (II)
- From: Richard Robinson
- Re: New arrivals (II)
- From: Andy Burns
- Re: New arrivals (II)
- From: coj
- Re: New arrivals (II)
- From: Richard Robinson
- Re: New arrivals (II)
- Prev by Date: Re: Unidentified Flying Cookware
- Next by Date: Re: Any sheddi Pug experts?
- Previous by thread: Re: New arrivals (II)
- Next by thread: Re: New arrivals (II)
- Index(es):
Relevant Pages
|
Loading