pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedLink many attributes in one table to one attribute in another??
4+ messages / 4 participants
[nested] [flat]
* Link many attributes in one table to one attribute in another??
@ 2001-06-28 04:44 Christian Anton <christiananton@hotmail.com>
0 siblings, 3 replies; 4+ messages in thread
From: Christian Anton @ 2001-06-28 04:44 UTC (permalink / raw)
To: pgsql-sql
Hello all,
I am fairly new at db design, I have built a few simple in the past and have
never dealt with this type of problem before, any help would be appreciated.
I have three attributes in one table that should reference one, and only
one, attribute in another table. Here's an analogy of the problem:
I have a table with a list of marbles, each marble has three colors in it
(color1, color2, color3) and a serial number. In another table I have eight
colors to choose from but the list of colors grows regularly.
How do I associate a marble with three colors from the color table (a marble
may have three red sides or red-yellow-blue)?
Thanks,
Christian
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Link many attributes in one table to one attribute in another??
@ 2001-06-28 14:31 Richard Huxton <dev@archonet.com>
parent: Christian Anton <christiananton@hotmail.com>
2 siblings, 0 replies; 4+ messages in thread
From: Richard Huxton @ 2001-06-28 14:31 UTC (permalink / raw)
To: Christian Anton <christiananton@hotmail.com>; +Cc: pgsql-sql
"Christian Anton" <christiananton@hotmail.com> wrote in message
news:3b3ab3aa_1@newsa.ev1.net...
> I have a table with a list of marbles, each marble has three colors in it
> (color1, color2, color3) and a serial number. In another table I have
eight
> colors to choose from but the list of colors grows regularly.
> How do I associate a marble with three colors from the color table (a
marble
> may have three red sides or red-yellow-blue)?
Not sure what you mean exactly, but the following:
CREATE TABLE marbles (
m_id serial,
m_col1 int4,
m_col2 int4,
m_col3 int4
);
CREATE TABLE colours (
c_id serial,
c_name text
);
INSERT INTO colours (c_name) VALUES ('red');
INSERT INTO colours (c_name) VALUES ('green');
INSERT INTO colours (c_name) VALUES ('blue');
INSERT INTO marbles (m_col1,m_col2,m_col3) VALUES (1,1,1);
INSERT INTO marbles (m_col1,m_col2,m_col3) VALUES (1,2,3);
INSERT INTO marbles (m_col1,m_col2,m_col3) VALUES (3,2,1);
SELECT m.m_id, c1.c_name as colname1, c2.c_name as colname2, c3.c_name as
colname3
FROM marbles m
JOIN colours c1 ON m.m_col1 = c1.c_id
JOIN colours c2 ON m.m_col2 = c2.c_id
JOIN colours c3 ON m.m_col3 = c3.c_id;
Will produce:
m_id | colname1 | colname2 | colname3
------+----------+----------+----------
1 | red | red | red
3 | blue | green | red
2 | red | green | blue
We join the colours table to the marbles table three times and need to alias
it differently each time. Otherwise, we don't know which colour-name matches
which colour-code.
If it's not that simple, can we have table definitions and an example of the
sort of output you'd like?
- Richard Huxton
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Link many attributes in one table to one attribute in another??
@ 2001-06-28 14:45 Christof Glaser <gcg@gl.aser.de>
parent: Christian Anton <christiananton@hotmail.com>
2 siblings, 0 replies; 4+ messages in thread
From: Christof Glaser @ 2001-06-28 14:45 UTC (permalink / raw)
To: Christian Anton <christiananton@hotmail.com>; pgsql-sql
On Thursday, 28. June 2001 06:44, Christian Anton wrote:
> Hello all,
>
> I am fairly new at db design, I have built a few simple in the past
> and have never dealt with this type of problem before, any help would
> be appreciated.
>
> I have three attributes in one table that should reference one, and
> only one, attribute in another table. Here's an analogy of the
> problem:
>
> I have a table with a list of marbles, each marble has three colors
> in it (color1, color2, color3) and a serial number. In another table
> I have eight colors to choose from but the list of colors grows
> regularly. How do I associate a marble with three colors from the
> color table (a marble may have three red sides or red-yellow-blue)?
Quite easy:
-- The list of colors. id is a automatically assigned unique number.
-- You could use INT4 instead of SERIAL and make numbers of your own.
CREATE TABLE color (
id SERIAL PRIMARY KEY,
name text
);
-- The list of marbles. The colors reference to the table color.
-- Postgres makes sure that only existing colors are entered.
CREATE TABLE marble (
number SERIAL PRIMARY KEY,
color1 int4 REFERENCES color,
color2 int4 REFERENCES color,
color3 int4 REFERENCES color
);
-- But you want to see the color's name rather its id, isn't it?
CREATE VIEW v_marble AS
SELECT m.number,
c1.name as color1,
c2.name as color2,
c3.name as color3
FROM marble m, color c1, color c2, color c3
WHERE m.color1 = c1.id
AND m.color2 = c2.id
AND m.color3 = c3.id;
Hope that helps.
Christof
--
gl.aser . software engineering . internet service
http://gl.aser.de/ . Planckstraße 7 . D-39104 Magdeburg
Tel. +49.391.7 44 77 10 . Fax +49.391.7 44 77 13 . Mobil 0177.77 92 84 3
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Link many attributes in one table to one attribute in another??
@ 2001-06-28 15:10 Wei Weng <wweng@kencast.com>
parent: Christian Anton <christiananton@hotmail.com>
2 siblings, 0 replies; 4+ messages in thread
From: Wei Weng @ 2001-06-28 15:10 UTC (permalink / raw)
To: Christian Anton <christiananton@hotmail.com>; +Cc: pgsql-sql
A easy way to handle this:
create table marble
(
id int4 primary key,
color1 int4,
color2 int4,
color3 int4
);
create table color
(
id int4,
desc text
);
color1, color2, and color3 point to the id in color table.
This way you can expand your color table indefinitely.
On 27 Jun 2001 23:44:34 -0500, Christian Anton wrote:
> Hello all,
>
> I am fairly new at db design, I have built a few simple in the past and have
> never dealt with this type of problem before, any help would be appreciated.
>
> I have three attributes in one table that should reference one, and only
> one, attribute in another table. Here's an analogy of the problem:
>
> I have a table with a list of marbles, each marble has three colors in it
> (color1, color2, color3) and a serial number. In another table I have eight
> colors to choose from but the list of colors grows regularly.
> How do I associate a marble with three colors from the color table (a marble
> may have three red sides or red-yellow-blue)?
>
> Thanks,
> Christian
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl
>
--
Wei Weng
Network Software Engineer
KenCast Inc.
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2001-06-28 15:10 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-06-28 04:44 Link many attributes in one table to one attribute in another?? Christian Anton <christiananton@hotmail.com>
2001-06-28 14:31 ` Richard Huxton <dev@archonet.com>
2001-06-28 14:45 ` Christof Glaser <gcg@gl.aser.de>
2001-06-28 15:10 ` Wei Weng <wweng@kencast.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox